How do I revert back to an older commit in Git?
I messed up pretty badly on this one. I was working on a branch 'feature1-branch' and commited the changes but did not merge them with master.
The git log shows something like:
git log
commit 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49
Date: Thu Apr 21 00:38:58 2011 -0700
feature1-branch lots and lots of changes
I am not sure what I was thinking but I did:
git checkout master
This deleted a bunch of new files added with the branch 'feature1-branch'.
Now git status shows:
git status
# On branch feature1-branch
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: app/controllers/activations_controller.rb
# deleted: app/controllers/settings_controller.rb
# modified: app/controllers/application_controller.rb
# deleted: app/controllers/products_controller.rb
The files mentioned as deleted are not there anymore. How do I get them back to the state where I was in with commit
0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49
I am worried if these files are gone forever.
Thanks for your help.
Adding additional information:
git log -1 feature1-branch
commit 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49
Date: Thu Apr 21 00:38:58 2011 -0700
feature1-branch lots and lots of changes
This is what git checkout returns:
git checkout feature1-branch
D app/controllers/activations_controller.rb
D app/controllers/products_controller.rb
M app/controllers/application_controller.rb
git status returns:
git status
# On branch feature1-branch
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkou开发者_Go百科t -- <file>..." to discard changes in working directory)
# # deleted: app/controllers/activations_controller.rb
# deleted: app/controllers/products_controller.rb
no changes added to commit (use "git add" and/or "git commit -a")
I don't understand the line in git status that says that you are on the feature1 branch. Can you however do this and paste the output? git log -1 feature1-branch
? If it tells you that it's pointing to 0829f9f0b68ce58ca37efd4e07cf3e8b3b67ed49
, you can just git checkout feature1-branch
to get to where you were before you switched to master
.
I suspect that what has happened is that you deleted these files at some point, but didn't commit their deletion. That means that when you switch branches they will still be shown as deleted, since git tries to preserve the modifications in your working tree when you change branch, unless that would lose data or create problems.
Just reading the output of git status
that you've quoted, it looks as if you can restore the deleted files by, for example:
git checkout -- app/controllers/activations_controller.rb app/controllers/settings_controller.rb
... while you're on feature1-branch
.
精彩评论