Git: How to revert everything exactly to the state of a prior commit?
If there have been commits and many changes since an earlier commit, is there a simple way to revert everything to the exact state of that earlier commit?
If so, is it possible to easily switch back to the current state if I should so desire as well?
I've realized that this old commit is actually the correct one, and want to maintain all changes since then only for reference, but all future work will be based 开发者_如何转开发on this old commit.
It sounds like you want to put your current work on a branch:
git branch saved_work
git reset --hard previouscommit
Now, your saved_work
branch contains everything after previouscommit
, and master
is rewound to previouscommit
.
Since you want to return to the earlier state:
git stash
git checkout -b new_branch <tree-ish>
The above will save your uncommitted changes and allow you to create a new branch at the specific commit. Work as you wish and later when you want to change to the earlier state:
git stash
git checkout earlier_branch
Commit your current changes:
git commit
Then checkout the old commit:
git checkout yourcommit
Adding a -f option will destroy any local changes.
精彩评论