While working on Master branch, I forgot to create new branch. Made changes to files then inadvertently reverted to the master, loosing all updates. I didn't commit the updated fil开发者_如何学JAVAes.
How can I retrieve them?
If you had not commited, staged, or stashed the changes you made, there is no way you can recover those changes.
EDIT: Recovering lost changes. Adding this on Mark Longair's suggestion (in the comment). This also includes a couple of SO links from his answer below(*), that I found quite informative.
If you have ever committed some change and have lost that commit (like committing in a detached state), you can find that commit using
reflog
. See this SO question*.If you have lost your last staged changes, you can also recover that. See this SO question*. (I have never used or tried it myself).
If you have stashed a change, you can also recover that using
pop
orapply
. (I am not sure if the popped/dropped stashes are also recoverable that were not committed). You may find this Recover dropped stash in git useful.
If there are any other methods that anyone can suggest, I'd edit this answer further to add them.
Two longshots: Some IDEs, such as Delphi, keep an editor history. You may have some recourse there.
Next, if you had your local working directory located in the MyDocuments folder, it may have been automatically backed up by Windows Home Server, Carbonite, MozyPro, etc.. these are usually 'set it and forget it'. maybe you forgot it?
I was stuck in the same situation.
Had a list of the changed files in the terminal before the --hard reset, went into my editor (Sublime 2, doesn't matter though), opened each file and hit cmd+z (undo) once, this effectively undid the changes done by the hard reset and I got my uncommitted changes back :)
The critical question here is what you did after making changes to the files. If you created a commit which contained the new state of the files, then you should be able to get them back by looking through the recent entries in git reflog
, finding the SHA1sum of the commit and then creating a new branch from that with git branch recovered <SHA1sum>
, or similar. There's an example of doing this in this answer.
If you did git add
on any of the files to stage them, you should also be able to get them back, but this is rather more work - Jakub describes how to do this in this answer.
If you happened to do a git stash
to give yourself a clean status, then of course you can get it back as you would any other stash.
Otherwise, I'm afraid that the news is not good.
I hope it's not infuriating to point this out post-hoc, but to just switch back to the master
branch, you shouldn't have needed to use any command that might lose you data - git checkout master
would have told you that you were already on the master branch, and show any uncommitted changes. (Arguably git reset --hard
should have a "Yes, I really mean this" confirmation if there are uncommitted changes, given how often people^W
I lose data this way.)
Use the reflog. git reflog
will show you a history of all the commits you have been on, in chronological order.
If you lost your changes by 'checking out master', then you were probably working headless. git status
will tell you if you are working without a head. (As does git branch
).
Working headless isn't that bad (I do it all the time, deliberately), but you will have a greater reliance on the reflog.
If you didn't commit your changes, in any way, however, then there is no way to retrieve those files. The only realistic way this could have happened is if you did a hard reset, or explicitly forced a checkout. Do not force changes unless you are sure that you are comfortable with losing data.
Usually, in git, 'forcing' is done by specifying -f
.
I've done the same thing, more than once unfortunately. Without committing anything git has no idea what you wrote. Even if you did commit then when backward I'm not sure reflex would help.
Most IDEs have a "Local History" feature, including all JetBrains products. You could also try ctrl + Z in all the affected files.
As a last resort, you can check Time Machine backups / any other backup software that runs periodically
精彩评论