Cleaning up a local commit before pushing to GitHub

If you are doing development locally and like to make lots of commits as you code and test and test you know that pushing a branch with all those commits can sometimes m amke it hard for reviewers to understand what has changed and why.

Wouldn’t it be great if you could compress all of your smaller commits together but still preserve the comments you entered with each commit? It looks like it can be done!

Found this little tip here http://stackoverflow.com/questions/5189560/how-can-i-squash-my-last-x-commits-together-using-git.

# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12

# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Commit those squashed changes.  The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit

Leave a Reply

Your email address will not be published. Required fields are marked *