Reputation: 1395
Is there currently anyway to undo the local changes from executing git checkout master -f
?
Being a fool I made a heap of changes locally in the wrong branch and stupidly misread a post and execute the said command, so all my local changes have reverted to 3 versions back :( (sad times).
Laughing and pointing welcome, but any advise would also be appreciated.
Upvotes: 0
Views: 306
Reputation: 142094
Yep.
You have several options to do it.
Read this answer for a full detailed about every option that you have:
How to move HEAD back to a previous location? (Detached head)
and more.
The best answer is to use the reflog
in order to "rollback" you changes, but again read all about it in the attached answer..
git reflog
git reflog
git checkout HEAD@{...}
This will get you back to your desired commit
Important notice:
git never throw away data unless gc was executed. so even if you don't see this commit its still in your local repository you just have to find it.
You can find it by executing git fsck which will "find" all the commits that are not reachable at this moment. Now you simply have to find teh right one and check it out
If you have commited your changes (true even if you just added your file to the stagings are without even committing it) you can restore the content.
Every time you add content to the staging area git starts to track it and its being stored in the .git/objects
folder.
Since you added the files and commited them they are already stored in there and once you have executed git checkout master -f
the files are still there but are "disconnected" from your repo.
In the git "language" its named dangling objects.
You can view those objects as long as you did not force git gc
which will remove them.
# scan your repo for dangeling file
git fsck --full
and then to view the content use:
git cat-file -p <sha-1>
Upvotes: 0
Reputation: 91907
It depends whether you committed these local changes, or had a dirty working tree with a bunch of uncommitted changes. In the latter case, there's not much you can do because you threw away changes without ever saving them. But in the former, you can recover it from the reflog. Look through the output of git reflog show HEAD
, and see if any of those look like the ocmmit you want to go back to. Then you can get it back with a checkout
or various flavors of reset
.
Upvotes: 2
Reputation: 346
Yes. try:
git reflog
to see a list of changes. Find the changes that you want and do:
git checkout <changeid>
What happened is that you changed your branch to point to somewhere else and these commits no longer have any branch pointing to them. However, the changes themselves the haven't disappeared you just can't see them. They will hang around in the reflog for up to 30 days before git removes them.
Upvotes: 1