Home Explore Blog CI



git

5th chunk of `Documentation/git-stash.adoc`
9c0106784ecb4d7d303c9c08836827bea5f63407102f48d200000001000007d3
 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
----------------------------------------------------------------

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 switch -c my_wip
$ git commit -a -m "WIP"
$ git switch master
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git switch 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 ...
----------------------------------------------------------------

Testing partial commits::

You can use `git stash push --keep-index` when you want to make two or
more commits out of the changes in the work tree, and you want to test
each change before committing:
+
----------------------------------------------------------------
# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash push --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop        

Title: Using Git Stash in Various Scenarios
Summary
The git stash command can be used to simplify workflows such as pulling into a dirty tree, interrupting a workflow to make an emergency fix, and testing partial commits, allowing users to temporarily store changes and switch between different versions of their code, making it easier to manage complex development tasks.