Fillip Peyton
Fillip Peyton

Reputation: 3657

Revert all changes to state of a previous commit

I am working in Git and want to know the best way to revert all of my changes to this Repo back to a specific commit. For example, here is an example of what my log looks like:

commit hash#1

commit hash#2

commit hash#3

commit hash#4

I am in my master branch and want to revert all changes made to this repo back to commit hash#4. So that means I want to undo the changes that were committed in hash#1,hash#2, and hash#3.

I do NOT want to rewrite/delete history, as this is a public repo. And I do want to (re)commit hash#4 once it has been reverted to.

Thanks!

Upvotes: 1

Views: 138

Answers (2)

Fillip Peyton
Fillip Peyton

Reputation: 3657

A great reset overview per (http://git-scm.com/2011/07/11/reset.html):

The reset command overwrites these three trees in a specific order, stopping when you tell it to.

1) Move whatever branch HEAD points to (stop if --soft)

2) THEN, make the Index look like that (stop here unless --hard)

3) THEN, make the Working Directory look like that

Just wanted to share for any other users that come across this question.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754763

Try the following

git reset --hard commit_hash_4
git reset --soft commit_hash_1

The first command will reset the working directory and head back to commit_hash_4. The second command will move the head back to commit_hash_1. Comitting at this point will preserve linear history but will give you the state of commit_hash_4

Upvotes: 4

Related Questions