GIT Rebase Branch To Single Commit - gecko-8/devwiki GitHub Wiki

Up

IMPORTANT: Don't do this if anyone else is working on the same branch!

  1. Get the commit SHA1 for the commit you originally checked out from (or you can just use "master" if that's your goal)
  2. Execute the following command:
    git rebase -i <SHA1 from above>
    or
    git rebase -i master
  3. 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
  1. Now, assuming you want just a single commit message
    1. Replace the first "pick" with "reword"
    2. 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
    3. Save the changes
  2. An editor will open where you can update the commit message for the "reword" one
    1. Update the message
    2. Save the changes
  3. If there were any conflicts, you may get a message as such. If not continue to the next major step.
    1. Fix the conflict(s) in your editor
    2. Add the file(s) to the changeset
      git add <file>
    3. Continue rebasing with the command
      git rebase --continue
  4. An editor will open with the final git commit message
    1. Assuming your happy with your changes from above, save the changes.
    2. Otherwise make any changes and save them.
  5. The rebase should now finish.
  6. 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.
  7. 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
    

Source

⚠️ **GitHub.com Fallback** ⚠️