-
Notifications
You must be signed in to change notification settings - Fork 0
Home
First get the sample project :
git clone git://github.com/lalyos-trainings/rebase-sample.git
before you start rebase let's see the sha1 codes: git log --oneline
0888237 1.line fixed (for real)
a8ed5ab 1.line fixed
4b57a82 2.line added to file2
3d9d9ab 3. line adde to file AND file2 added
429cabe target added
e613ac9 2.line added
e411a5e added
Let's say you were in a hurry a made those changes, but before push, you would make them look more nice:
- delete 429cabe as target directory was added by a mistake
- squash a 2 last commit (0888237 + a8ed5ab) into one
- as 3d9d9ab is doing 2 different things, it should be split up
Let's start with the command:
git rebase -i --onto e411a5e e411a5e 0888237
Rebase have 3 options, in case you want to rebase into the same branch you are in:
- --onto tells where you want to join the new commit ladder.
- from where to cut, but note that this is exclusive so in the sample the rebase starts with e613ac9
- where is the end of the rebase
So yoy will see the following screen:
pick e613ac9 2.line added
pick 429cabe target added
pick 3d9d9ab 3. line adde to file AND file2 added
pick 4b57a82 2.line added to file2
pick a8ed5ab 1.line fixed
pick 0888237 1.line fixed (for real)
# Rebase e411a5e..0888237 onto e411a5e
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
...
You have change 3 line according to the tasks:
- delete 429cabe : Just simple delete the whole line
- squash last two (0888237 + a8ed5ab): Change last command to squash (or s as a shorthand), which means 0888237 shouldn't exist in a separate commit.
- split 3d9d9ab : Change the command to Edit (or e as a shorthand)
So you will have the following lines:
pick e613ac9 2.line added
e 3d9d9ab 3. line adde to file AND file2 added
pick 4b57a82 2.line added to file2
pick a8ed5ab 1.line fixed
s 0888237 1.line fixed (for real)
After that, rebase starts to do the job and place the first picks the first commit. The next line is edit so it will stop, and give you a change to amend/comit.
Stopped at 3d9d9ab... 3. line adde to file AND file2 added
You can amend the commit now, with
So it means rebase stopped after the mixed-up commit. So the first thing to do is step back 1, but leave the working are as it is. You do that with git reset --mixed HEAD^
or for short: git reset HEAD^
After that you are in a state before the commit "3. line adde to file AND file2 added". You can check it by git status
We can simple split the original commit, by commit twice with smaller parts:
git add file1
git commit -m "3. line adde"
now you can the second subcommit :
git add file2
git commit -m "file2 added"
No you can tell rebase to continue: git rebase --continue
The next time rebase stops, it provides you an editor, where you can write a new commit message to the combined commit. So you change it to something like:
1.line fixed correctly
# Please enter the commit message for your changes. Lines starting
...
Congrats: you have just finished your first interactive rebase! The next one will be a breeze. btw: the fixup is almost the same as squash, but the rebase concatenates the commit messages fo you, and continues, without stopping.
So the final result will look similar like this:
832e09f 1.line fixed correctly
dc1a49a 2.line added to file2
9abc8f5 file2 added
b394cbc 3. line added
e613ac9 2.line added
e411a5e added
Now as you are a rebase master (padavan), you can:
- squash the "2. line added" and "3.line added" changes into a single commit
- squash the same with file2
- reorder commits, so that "1.line fixed" comes to the begining of the history
Good luck!
In case you mess up a the rebase, you can abort it, and go back to the startin point:
git rebase --abort
git reset --hard start-here