Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use git stash save or git commit for local changes?

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?

like image 249
Balthier Avatar asked Sep 11 '13 09:09

Balthier


People also ask

Does git stash save locally?

DESCRIPTION. Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Should I stash or commit?

Git stash vs.A commit creates a new save point on a branch; a stash reverts to a previous save point. A new commit leaves files in the working tree unchanged; a stash resets files in the working tree to the previous commit point. A commit is a public record of file changes; a stash is local.

Does git stash save commited changes?

Invoking git stash encodes any changes to tracked files as two new commits in your DAG: one for unstaged changes, and one for changes staged in the index. The special refs/stash ref is updated to point to them. Using the --include-untracked option also encodes any changes to untracked files as an additional commit.


1 Answers

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.

like image 69
functionpointer Avatar answered Oct 09 '22 18:10

functionpointer