Use git stash save or git commit for local changes?

Balthier picture Balthier · Sep 11, 2013 · Viewed 36.1k times · Source

I changed some files in my repo, but don't want them to be pushed public or create any temporary branch to store them. I just want to save these changes in somewhere. So which command is better:

git stash save "save message" 

or

git commit -am "save message"

?

If I use git commit, is it true that all of my local commits will be pushed publicly by one git push command? What if I just want to push one specific commit among them?

Answer

functionpointer picture functionpointer · Sep 11, 2013

When pushing, you always push one specific commit (usually the commit at the tip of your currently checked out branch). However, as the commit's hash partly consists of the commits it bases on (its parent commits), you have to push all parent commits also. And by pushing the parent commits you also have to push their parent commits and so on. So, you can only push the whole history of a specific commit.

If you create a commit just to store something but not for pushing, you need to make sure that you never push that commit, nor any commits that base on that commit. To do that, after you have done your work that bases on the temporary commit, you need to squash the temporary commit into the new commit that you create to push it.

In other words, yes it is possible to use a commit for temporary, private storage. However, it is much easier to use the stash feature. In fact, the feature is made for this very use case.