Thursday, September 14, 2017

GIT: Merge several commits to one (Rewrite History)

There can be situations where you want to merge several commits into one commit and remove the old commits. So that they will look like only one commit.
Here is how,

  1. Issue following command by replacing n with the number of commits you need to merge together. git rebase -i HEAD~n
  2. You will get a prompt like below. Replace the word "pick" with "squash" on all commits other than the latest one.
  3. Then you will get an editor to add the commit message for the new commit.
  4. Once this is done there will be a new commit that is not pushed to the remote repo.
  5. Force push the commit to the remote with following command.  git push -f remote-repo branch 
Check the git log, you will have only one commit for all those commits.

No comments:

Post a Comment