Restore earlier state, keep untracked files
I need to restore all files in my local machine t开发者_高级运维o earlier revision and keep the untracked files as they are. I found this Rollback file to much earlier version using Git and did first
git reset <hash>
My files were still the same as before so I did
git checkout <hash>
This didn't help either. Then I noticed that git log didn't show up revisions which were done after revision.
Im confused what to do restore the state of earlier revision, any advice?
Both reset
and checkout
commands have several meanings depending on arguments and the meanings are slightly mixed.
- reset:
- with revision argument: Sets current branch to point to the revision specified and:
reset --soft <rev>
: leaves both index and files intactreset --mixed <rev>
(this is the default with rev): makes index reflect the state at revision, but leaves files intactreset --hard <rev>
: overwrites both index and the files with state of the revision specified.
- with path argument: makes index contain the checked in content of the files specified, but does not change files on disk (= undoes
git add
).
- with revision argument: Sets current branch to point to the revision specified and:
- checkout:
- with revision argument: Switches to branch or revision specified, but preserves local modifications. If some file is modified and differs between the current HEAD and the revision specified, refuses to run.
- with path argument: Modifies the files specified to match the index, i.e. reverts unadded changes.
It follows that:
git reset <rev>
git checkout <rev>
is equivalent to just
git reset --mixed <rev>
and does not modify the state on disk. You should have done either:
git reset --hard <rev>
or
git checkout <rev>
where the first would have modified where the current branch points, while the later would switch branches. Neither would touch nonversioned files.
If you did
git reset <rev>
already and want the effect of
git reset --hard <rev>
just follow it up with
git checkout .
(the pathspec form instead of the revision form).
Update: Note, that
git reset --hard <rev>
will make current branch point to <rev>
, so it will be like the commits after <rev>
never happened. This is called a "rewind" and will cause some trouble if somebody based their own work on the branch already, because their history will still have those revisions. If you instead want to create a new commit that undoes all changes since <rev>
, you can do:
git revert <rev>..
This command creates a new commit that undoes all changes done in commits specified. It can't revert a merge. To revert to content of revision before merge and keep history, you could try:
git reset --mixed <rev>
git reset --soft HEAD@{1}
git commit
The first reset will move HEAD and index and the second reset will return HEAD to where it has been one operation ago, so after that point you have index from <rev>
and HEAD back where it has been. So you can now commit. The work tree will be unchanged by the operation. You can either git checkout .
afterwards, or just make the first reset --hard
to get the work tree content from <rev>
too.
精彩评论