Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert to local commit?

Tags:

git

I've pulled code down from my repo which has messed things up. I'd like to revert my entire project to my last local commit. How would I do this?

like image 509
Skizit Avatar asked May 15 '11 10:05

Skizit


People also ask

How do I revert to a local commit?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

Does revert remove the commit?

Revert will create a new commit that undoes the specified commit. drop keyword is not defined. To delete a commit just remove the whole line.

How do I revert a local commit in Visual Studio?

To do the same in Visual Studio, right-click the commit you want to revert and then select Revert.


3 Answers

This will reset everything to your current commit (getting rid of all changes, staged or otherwise:

git reset HEAD --hard

This will reset everything to the previous commit (also getting rid of all changes, staged or otherwise)

git reset HEAD^ --hard

the ^ next to HEAD means one commit before HEAD, HEAD being where you are currently. You can go two commits back by using ^^, or three with ^^^. Additionally you can use a tilde to specify the number of commits: ~3 for three commits back.

git reset HEAD~3 --hard

Also keep in mind that the --hard option means that these commands will throw away any changes you have that are not stashed.

like image 151
Chris Rasys Avatar answered Oct 06 '22 21:10

Chris Rasys


Locate your last local commit in git log and run git reset --hard <commit sha1>.

It will delete all the local changes you haven't commited, and will move the HEAD to this commit.

like image 43
Michaël Witrant Avatar answered Oct 06 '22 19:10

Michaël Witrant


git pull can fetch and merge multiple commits. To go back to your previous local state (rather than back n-commits) you can use the reflog. git reset --hard @{1}

like image 4
Karl Avatar answered Oct 06 '22 19:10

Karl