git Rebase(リベース)
【開発環境】
OS:Win11(64ビット)
git version 2.37.3.windows.1
VSCode1.72.2
【rebaseとは】
まずrebaseコマンドを一言で言い表せば「指定したコミットを、ブランチを変えて作り直したり、ひとまとめにしたりして、ログを綺麗にするコマンド」
例
・2つにブランチする
・マージンした場合
・リベースした場合
rebaseの方が、更新ログが一直線で見やすいですね!これなら過去の更新履歴を追う時も見やすい
■実行してみる
・thirdブランチを作る
PS D:\Git\Gitmanabe> git branch 'third'
PS D:\Git\Gitmanabe> git branch
* main
second
third
・カレントブランチをthirdに切り替える
PS D:\Git\Gitmanabe> git checkout third
Switched to branch 'third'
PS D:\Git\Gitmanabe> git branch
main
second
* third
・ファイルを作る
・mainブランチにもファイル「4_test.txt」を作り、プッシュする
PS D:\Git\Gitmanabe> git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
PS D:\Git\Gitmanabe> git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
PS D:\Git\Gitmanabe>
・カレントをthirdに戻し、mainとリベースする。
PS D:\Git\Gitmanabe> git checkout third
Switched to branch 'third'
Your branch is up to date with 'origin/third
PS D:\Git\Gitmanabe> git rebase main
Successfully rebased and updated refs/heads/third.
PS D:\Git\Gitmanabe>
PS D:\Git\Gitmanabe> git status
On branch third
Your branch and 'origin/third' have diverged,
and have 2 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
以上で
ただし、gitHubにはプッシュされない
mergeとrebaseの違い
具体的な使い分け方ですが、二つのコマンドの一番の違いは「既存のコミットへ影響を与えるか・与えないか」
「rebaseコマンド」は、処理を作り直すが、「mergeコマンド」は既存コミットに影響を与えずにマージコミットを作成する。この点がこの二つの一番の違い
最後に、mainブランチにマージする
PS D:\Git\Gitmanabe> git merge third
Updating af04f20..db3bef1
Fast-forward
3_test1.txt | 1 +
4_test.txt | 1 +
2 files changed, 2 insertions(+)
create mode 10064