tags:

views:

156

answers:

3

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.

+1  A: 

Commit your current changes:

git commit

Then checkout the old commit:

git checkout yourcommit

Adding a -f option will destroy any local changes.

mjaz
+1  A: 

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
Alan Haggai Alavi
How would I go about setting this checked out branch to be the master? 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.
WIlliam Jones
+2  A: 

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.

Greg Hewgill