问题
If I work on branch A and suddenly need to work on branch B before being ready with a commit on branch A, I stash my changes on A, checkout B, do my work there, then checkout A and apply the stash.
If I work on A and I want to stop working for the day, should I stash my work and then apply it the next day, when I resume my work, or should I just leave things as they are - uncommitted modified files in the working directory. I don't see why I would need to use stash in this case, except if there is some security benefit.
Also, another scenario - I work both at work and at home. If I am not ready with a commit when I want to go home, can I stash my work, push it to GitHub and then pull that stash at home?
回答1:
Stash is just a convenience method. Since branches are so cheap and easy to manage in git, I personally almost always prefer creating a new temporary branch than stashing, but it's a matter of taste mostly.
The one place I do like stashing is if I discover I forgot something in my last commit and have already started working on the next one in the same branch:
# Assume the latest commit was already done
# start working on the next patch, and discovered I was missing something
# stash away the current mess I made
git stash save
# some changes in the working dir
# and now add them to the last commit:
git add -u
git commit --ammend
# back to work!
git stash pop
回答2:
The stash command will stash any changes you have made since your last commit. In your case there is no reason to stash if you are gonna continue working on it the next day. I would only use stash to undo changes that you don't want to commit.
回答3:
If you hit git stash
when you have changes in the working copy (not in the staging area), git will create a stashed object and pushes onto the stack of stashes (just like you did git checkout -- .
but you won't lose changes). Later, you can pop from the top of the stack.
回答4:
I will break answer on three paragraphs.
Part 1:git stash
# for hide you changes without commit git checkout some_branch
# for work with other tasks and come back on your branchgit stash list
# for see your list with hidden changes
You can see:
stash@{0}: WIP on {branch_name}: {SHA-1 of last commit} {last commit of you branch}
stash@{0}: WIP on master: 085b095c6 modification for test
git stash apply
# for apply your last changes from stash listgit stash apply stash@{12}
# if you will have many stashes you can choose what stash will apply
git stash drop stash@{0}
# for remove from stash list
orgit stash pop stash@{1}
# for apply choosed stash and drop it from stash list
Part 2:
You can hide your changes with this command but it is not necessary.
You can continue on the next day without stash.
This commands for hide your changes and work on different branches or for implementation some realisation of your code and save in stashes without branches and commitsor your custom case!
And later you can use some of stashes and check wich is better.
Part 3:
Stash command for local hide your changes.
If you want work remotely you must commit and push.
回答5:
The main idea is
Stash the changes in a dirty working directory away
So Basicallly Stash command keep your some changes that you don't need them or want them at the moment; but you may need them.
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.
来源:https://stackoverflow.com/questions/20537223/when-should-i-use-git-stash