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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With