Git: Command for cancel a commit

When I work, I often meet with this workflow:

  1. Doing something.
  2. Someone comes to my desk and ask something, I must move to another branch for checking.
  3. I commit code on this current branch and checkout another branch.
  4. When I finish, I checkout old branch and continue to work.

This workflow has a disadvantage: it generates a dummy commit in my system. My question is: how can I avoid this situation? For example, when I come back to work, I will cancel previous commit?

Jon Skeet
people
quotationmark

In addition to the two existing answers (git reset and git stash), I personally just ignore the extra commit (which almost has a commit message of wip...) until I'm ready to push the change somewhere remote, e.g. to github to create a pull request.

At that point, I use git rebase -i to look at all the commits I've got in that branch, and work out which commits I want to push - squashing commits together and rewording commit messages as required.

I've personally found this to be easier to use than git stash, as I typically find I lose track of what stashes are meant to be about. (My fault rather than a flaw in git, but the effect is still bad.) I suspect that git reset would be fine too, but as I often have to rebase anyway before a final merge - e.g. to react to code review comments - I have git rebase as a more mentally efficient part of my workflow.

people

See more on this question at Stackoverflow