Reputation: 83
I want my branch to be up to date (not behind by any commits) without making any new commits and leaving my tracked and modified file alone. I want to keep the changes in this file. I accidentally ran "git reset HEAD~" to see what it did and now I can't undo "git reset HEAD~".
In short, below is the order of events and my failed attempt at correcting it.
<modified file>
git reset --soft HEAD~
git stash
git merge --ff-only
git stash pop
After the "git reset --soft HEAD~" command, I expected the following three commands to effectively reverse it. What I actually got back was...
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you can merge.
Aborting
Any help is greatly appreciated!
Upvotes: 0
Views: 71
Reputation: 28258
You will be much better off by abandoning ever using git stash
and instead just always checking in changes on temporary branches. Things get much clearer and more robust as well as making things that git stash cannot do possible.
From your current state run
git checkout -b stash-branch-001
git commit -am stash
# Now all your pending changes are saved, like git stash does, but properly saved
# on a branch with all the benefits of a branch that stash cannot provide.
git checkout master
git fetch origin # Get latest and greatest from upstream
git status # Always run git status and verify before reset --hard
git reset --hard origin/master
# At this point git status should say "Your branch is up-to-date with
# 'origin/master'"
Then to bring back the saved changes run (in the general case):
git rebase master stash-branch-001 # Implicit changes the checked out branch...
git checkout master # ... so change back
git reset orign/master
# At this point git status should say "modified: README.md"
or in this particular case where there is only one commit on the temporary branch you could run just
git cherry-pick stash-branch-001
git reset HEAD^
instead. When satisfied just delete the temporary branch.
I stopped using git stash
around ten years ago I think. It provides zero benefits that checking in on temporary branches does not provide. Maybe there is a few less characters to type, but it is conceptually much simpler to just always check in and rebase like you always do instead of doing something differently for saving changes temporarily.
Viewing the changes then is just git log -p ...
, git diff ...
, gitk
or any of your regular git command instead of having one single, special case git stash show
command.
If you have multiple temporary changes ongoing, bringing them in and out in any order becomes straight forward, instead of being forced to pop them in sequence like stash does.
Having the changes checked in on a branch provides great flexibility in allowing you to bring back the temporary changes in any branch (and possibly multiple) whereas git stash pop
is very limited in its requirement: The working directory must match the index.
.
So all in all, do yourself a favour and stop using git stash
.
Upvotes: 1