1

I don't really understand git stash. According to the documentation, I may use stash to save my current repository state in the same say as if I backup/restore my repository somewhere.

Apparently what I did was something else...

I first wanted to completely save my work, even though the untracked files. So I did:

git stash -u 

Then I did some not very important work. At the end I wanted to come back to my original work so I used this command:

git stash pop 

The weird thing is that I got this message:

Auto-merging foo.c CONFLICT (content): Merge conflict in foo.c 

And inside my file I got this

++<<<<<<< Updated upstream ++======= + // something + ++>>>>>>> Stashed changes 

Actually, instead of using stash I could have used:

cp -R myproject myproject_backup 

And later restore my work:

rm myproject && mv myproject_backup myproject 

What did I misunderstand?

2 Answers 2

1

It appears that the work you did after pushing a stash affected the files you had in the stash (only you know what happened but I suspect some interaction with a remote repo), so it was unable to re-apply the change when you popped it out of the stash.

On the same page you linked it also says:

The working directory must match the index. Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.

There's a lot more information on a few other SO questions, e.g. this one, about getting back to a clean state with the stash removed and your changes just in your working tree.

Yes you could have copied the directory away and restored it, but you'd have lost the 'work' you did in between.

Sign up to request clarification or add additional context in comments.

2 Comments

"Yes you could have copied the directory away and restored it, but you'd have lost the 'work' you did in between." Not if you also backup the .git
it depends at what point you do the copy. in your description you say "instead of doing the stash..." from which I assumed you are doing the copy before doing the work, as described in the rest of the question, namely "so i did a stash... then I did some not very important work". If you replace that with "i did a copy, then some work", if you restore the copy, you won't have the work
1

Stash works by taking the difference between the checked-out revision and your current working directory, and storing this diff as a temporary commit in a separate location.

This allows you to stash your work, switch to a different branch, do something, switch back, and pop your stash.

However, if you try to pop your stash onto a different revision than the one you pushed it from, for example because you pulled the origin in-between, it can well happen that some of the changes in the temporary commit conflict with other commits that were added. In this case you have to merge.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.