Thursday, April 7, 2016

Squashing a commit


  • You just sent a pull request to GitHub.
  • Someone commented on it.
  • You fixed the errors. You added them, commited them and pushed them again. 

This actually adds another commit to that pull request and makes it really hard to read for the person who is trying to accept that commit.

What you can do to combine these commits into one, easy to read commit is to squash them together.

This is officially called squashing a commit.

https://github.com/ginatrapani/todo.txt-android/wiki/Squash-All-Commits-Related-to-a-Single-Issue-into-a-Single-Commit

Squash a commit:
$ git rebase -i HEAD~3

You'll see your commits all mashed together

pick Add to README
pick Add .gitignore
pick Edit README

and a list of options

what you want to do is Put the commit you want at the top and squash the other ones into it by removing the work pick in front of it and replacing that with the letter 's' for squash.

So it looks like this

pick Add to README
s Add .gitignore
s Edit README

save it and force push it to GitHub like this:

$ git push origin branch-name --force


Then instead of doing the whole thing again if you make another change just
git add  
git commit --amend
git push origin branch-name --force

and you can add to you former commit

No comments:

Post a Comment