GIT Rebase Branch To Single Commit - gecko-8/devwiki GitHub Wiki
IMPORTANT: Don't do this if anyone else is working on the same branch!
- Get the commit SHA1 for the commit you originally checked out from (or you can just use "master" if that's your goal)
- Execute the following command:
git rebase -i <SHA1 from above>
or
git rebase -i master
- An editor will open with something like the following:
0 pick 60042c8 Add nav to application
1 pick 83c9bbb Add list items to nav
2 pick 14a64ff Add background color to nav and body
3 pick c289adc Create div for user data
4 pick 97178bd User data display WIP
5 pick 76e5811 Add padding to nav list items
6 pick a135e04 Implement search input for user display
7 pick 1bd999c Styling user data WIP. Add margin
8 pick 44d2565 Create nav border
9
10 # Rebase c0fa78d..44d2565 onto c0fa78d (9 command(s))
11 #
12 # Commands:
13 # p, pick = use commit
14 # r, reword = use commit, but edit the commit message
15 # e, edit = use commit, but stop for amending
16 # s, squash = use commit, but meld into previous commit
17 # f, fixup = like "squash", but discard this commit's log message
18 # x, exec = run command (the rest of the line) using shell
19 # d, drop = remove commit
20 #
21 # These lines can be re-ordered; they are executed from top to bottom.
22 #
23 # If you remove a line here THAT COMMIT WILL BE LOST.
24 #
25 # However, if you remove everything, the rebase will be aborted.
26 #
27 # Note that empty commits are commented out
- Now, assuming you want just a single commit message
- Replace the first "pick" with "reword"
- Replace the rest of the "pick" words with "fixup" IMPORTANT: If you don't do this, you'll end up with a commit for each one
- Save the changes
- An editor will open where you can update the commit message for the "reword" one
- Update the message
- Save the changes
- If there were any conflicts, you may get a message as such. If not continue to the next major step.
- Fix the conflict(s) in your editor
- Add the file(s) to the changeset
git add <file>
- Continue rebasing with the command
git rebase --continue
- An editor will open with the final git commit message
- Assuming your happy with your changes from above, save the changes.
- Otherwise make any changes and save them.
- The rebase should now finish.
- Push to remote with the following command:
git push --force-with-lease
NOTE: --force-with-lease is better than --force since it checks for changes by anyone else first. Noone else SHOULD be changing your branch but you never know. - If the above push doesn't work (maybe blocked by server), you can try the following instead:
git push origin --delete myFeature git push origin myFeature