What is git rebase? How to use git rebase? How to reset?
Concise Explanation for developers
What is git rebase? How to use git rebase?
・ What Rebase have in common with Merge
Rebasing is similar to merging in that it combines the contents of two branches into one branch.
Also, both can cause conflicts at run time.
・Difference from Merge
Merge merges the two branches like a river. I think it’s the easiest to understand intuitively without any particular twist.
Rebase is like rewriting the history of git and moving the point (base) where the two branches started to branch to the latest point, so that the fact that the branching itself did not exist as a result.
Case Study
・When you want to incorporate the contents of a tributary into the mainstream (feature-XXXXX-> develop, etc.)
For example, when the development with feature-XXXXX has been completed and the review has been completed, you want to incorporate it into the mainstream develop branch.
-> In this case, merge the tributary (feature-XXXXX) into the main stream (develop) and import it.
* Do not rebase the main stream (develop) into a tributary (feature-XXXXX). This is because develop is referenced by multiple people, but the history is rewritten.
・When you want to incorporate the contents of the mainstream into a tributary (develop-> feature-XXXXX, etc.)
For example, when you want to incorporate the important changes in develop into feature-XXXXX because you want to use them in your current working branch (feature-XXXXX).
-> Rebase the tributary (feature-XXXXX) on the mainstream (develop).
You can merge-> develop into feature-XXXX, but then in the commit history, the commits will be arranged in chronological order, making it difficult to distinguish between feature-XXXXX and develop. Only when Rebase will take too much time, you can think of using Merge instead.
rebase procedure
You want to rebase the feature-XXXX branch onto the develop branch.
0: Obviously, if you have a remote develop branch, you need to bring the latest changes locally (origin is the name of the remote git repository).
git pull origin develop
1: Duplicate the feature-XXXX branch. Because rebase rewrites the history, it will be troublesome to restore it when it fails. You can also use `git reflog` as a procedure to fix it, so it’s not mandatory. See the bottom of the article for git reflog.
git checkout -b feature-XXXX-copy
2: Check out to the feature-XXXX branch. You can rebase it all at once, but it’s often a hassle to resolve merge conflicts over and over again, as you’ll have to stack commits one by one on the develop branch. Therefore, first use `rebase -i` to combine the commits of the feature-XXXX branch into one.
git checkout feature-XXXX
git rebase -i 5634f2e
`5634f2e` is the hash value of the commit that is the common point (branch point) between feature-XXXX and the develop branch.
3.Then vi starts and the following is displayed.
The commit flow is in the order of `5634f2e` (branch point with the develop branch)->` 3bf6995`-> `15717d1`->` 6a36aed`.
pick 3bf6995 Entity and Repository have been modified.
pick 15717d1 Set the correct icon image.
pick 6a36aed Deleted unnecessary description.
# Rebase 5634f2e..6a36aed onto 5634f2e (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like “squash”, but discard this commit’s log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with ‘git rebase — continue’)
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit’s
As you can see on the screen, here we first combine the three `3bf6995`->` 15717d1`-> `6a36aed` into one. After `5634f2e`, which is the branch point with the develop branch, make sure that only one commit remains in the feature-XXXX branch.
If you want to quit, you can quit vi with `: q!`, And rebase will quit without any changes.
4: Put together commits
This operation method is vi instead of git. Press the `a` key,` i` key, etc. to enter the edit mode.
Change the character string as follows, just like a normal text editor.
pick 3bf6995 Entity and Repository have been modified.
squash 15717d1 Set the correct icon image.
squash 6a36aed Deleted unnecessary description.
# Rebase 5634f2e..6a36aed onto 5634f2e (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like “squash”, but discard this commit’s log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with ‘git rebase — continue’)
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit’s
Can you see that the beginnings of the 2nd and 3rd lines have been changed to “squash”? This means leaving the first commit “3bf6995” as is and merging the changes contained in “15717d1” and “6a36aed” into “3bf6995”.
After editing, save with `: wq!` And exit vi.
Then, this time, go to the commit message edit screen.
#This is a combination of 3 commits.
#This is the 1st commit message:
Entity and Repository have been modified.
#This is the commit message # 2:
I set the correct icon image.
#This is the commit message # 3:
I deleted unnecessary descriptions.
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# Date: Thu Jul 16 13:10:41 2020 +0900
#
# interactive rebase in progress; onto 5634f2e
Enter a message so that you can see that it was put together, and close it again with `: wq!`. This completes the commit summary.
5 Rebase on the develop branch
Rebase a single commit to develop.
git rebase develop
If a conflict occurs, resolve it. This completes the rebase.
rebase is complete, but I still want to restore it
If you’ve created a branch of a copy of feature-XXXX, you can restore it.
You can use `git reflog` even if you haven’t made a copy branch. `git reflog` keeps a log of where the HEAD was pointing, and of course it also keeps a record of where the HEAD moved in rebase. Therefore, rebase can also be reset.
git checkout feature-XXXX
git reflog feature-XXXX
Type the above command and you’ll see a reflog.
<commid id> (HEAD-> feature-XXXX) feature-XXXX @ {0}: rebase finished: refs / heads / feature-XXXX onto <commit id>
<commid id> feature-XXXX @ {1}: <commit message>
Here, `feature-XXXX @ {0}` is the log when the rebase is completed, and `feature-XXXX @ {1}` is the log before the rebase. Therefore, in this case, the rebase can be restored with the following command.
git reset — hard feature-XXXX @ {1}
I want to rebase including merge commit
Hit the following command.
git rebase — rebase-merge upstream