Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep changes while doing a `git stash`

Tags:

git

Is there any way that I can run git stash have it create a stash of my files, but also keep them around so that I can continue editing them.

My motivation for this is that I would like to create a temporary snapshot of some work I'm doing, so that I can go back to it if I muck things up. However, my code is not currently in a state where I want to actually commit it, and if my test works as I expected, I want to throw the old version away without it ever being in my history.

Obviously I can just make a copy the directory my repo is in, and work in one of them, but is there a better way?

like image 862
Leif Andersen Avatar asked Dec 14 '15 21:12

Leif Andersen


People also ask

Does git stash committed 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.

Does git stash delete changes?

Git stash is a temporary storage. When you're ready to continue where you left off, you can restore the saved state easily: git stash pop . Popping your stash removes the changes from your stash and reapplies the last saved state.

How do you stash all staged changes?

Stage all your files that you need to stash. Run git stash --keep-index . This command will create a stash with ALL of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged). Now your "good stash" has ONLY staged files.


3 Answers

There is (as far as I am aware) not a way to do this with git stash with one command, but you can do it with two uses of git stash.

The commands you want to run are:

git stash
git stash apply

The first one takes your local changes and pushes them to the stash, thus removing them from HEAD. The second one takes the changes from the stack, and puts them back in HEAD, but also leaves a copy of them on your stash stack.

like image 169
Leif Andersen Avatar answered Oct 21 '22 05:10

Leif Andersen


I think a better solution in this situation is to create a topic branch.

In Git, branches are very cheap.

If you have work that you want to "stash" away to come back to later do this:

git checkout -b myStashBranch
git add .
git commit -m "savepoint when I foo..."

then, simply switch back to the branch you were currently on:

git checkout -

and continue on with the task at hand...

If you get to a point where you want to simply throw away that "stashed" work that you just saved do:

git branch -D myStashBranch
like image 42
Jonathan.Brink Avatar answered Oct 21 '22 06:10

Jonathan.Brink


You have two options:

git stash store -m "WIP on $(git rev-parse --abbrev-ref HEAD): $(git log -1 --format="%h %s")" $(git stash create)

or

git add -A; git stash --keep-index

There pros/cons to both. The first does not mess around with the state of your working directory at all, but is quite verbose (although turning it into an alias will mitigate this). The second one is simpler, but means that all your changes will be staged (this may or may not be desired).

The benefit of these over

git stash
git stash apply

is that the all the files will need to be modified twice. This can cause issues with speed if there are number of files, or if you are using an IDE that might attempt to rebuild after every time a file changes. If this is is not an issue than by all means use it.

It would have been nice of Git to provide

git stash --keep-working-dir

If you are going to do these with any regularity than you should make an alias.

like image 4
Joseph K. Strauss Avatar answered Oct 21 '22 07:10

Joseph K. Strauss