1.Pulling into a dirty tree
When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing. When your local changes do not conflict with the changes in the upstream, a simple git pull
will let you move forward.
However, there are cases in which your local changes do conflict with the upstream changes, and git pull
refuses to overwrite your changes. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:
$ git pull ...file foobar not up to date, cannot merge.$ git stash$ git pull$ git stash pop
参考博文:
2.Interrupted workflow
When you are in the middle of something, your boss comes in and demands that you fix something immediately. Traditionally, you would make a commit to a temporary branch to store your changes away, and return to your original branch to make the emergency fix, like this:
# ... hack hack hack ...$ git checkout -b my_wip$ git commit -a -m "WIP"$ git checkout master$ edit emergency fix$ git commit -a -m "Fix in a hurry"$ git checkout my_wip$ git reset --soft HEAD^# ... continue hacking ...
You can use git stash to simplify the above, like this:
# ... hack hack hack ...$ git stash$ edit emergency fix$ git commit -a -m "Fix in a hurry"$ git stash pop# ... continue hacking ...
SYNOPSIS
git stash list []git stash show [ ]git stash drop [-q|--quiet] [ ]git stash ( pop | apply ) [--index] [-q|--quiet] [ ]git stash branch [ ]git stash [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet] [-u|--include-untracked] [-a|--all] [ ]]git stash cleargit stash create [ ]git stash store [-m|--message ] [-q|--quiet]
drop [-q|--quiet] [<stash>]
Remove a single stashed state from the stash list. When no <stash>
is given, it removes the latest one. i.e. stash@{0}
, otherwise <stash>
must be a valid stash log reference of the formstash@{<revision>}
.
list [<options>]
List the stashes that you currently have. Each stash is listed with its name (e.g. stash@{0}
is the latest stash, stash@{1}
is the one before, etc.), the name of the branch that was current when the stash was made, and a short description of the commit the stash was based on.