Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rollback a git repository to a specific commit?

Tags:

git

github

My repo has 100 commits in it right now. I need to rollback the repository to commit 80, and remove all the subsequent ones.

Why?

This repo is supposed to be for merging from miscellaneous users. A bunch of merges went in as commits from me, due to excessive editing. That was due to a mislabeling of my remote branches, where 3 developers were labeled as each other. I need to reset to that point, and then pull forwards.

I wanted to rebase, as in this example: How can I remove a commit on GitHub?

However, git wants me to do a lot of conflict management. Is there a simpler way?

like image 636
Jonathan Vanasco Avatar asked Dec 07 '10 00:12

Jonathan Vanasco


People also ask

Can we rollback commit in git?

The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset , move the HEAD and branch ref pointers to a specified commit. Git revert also takes a specified commit, however, git revert does not move ref pointers to this commit.


2 Answers

git reset --hard <old-commit-id> git push -f <remote-name> <branch-name> 

Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history

like image 55
jtdubs Avatar answered Oct 02 '22 00:10

jtdubs


To undo the most recent commit I do this:

First:

git log 

get the very latest SHA id to undo.

git revert SHA 

That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.

This is good for an immediate redo of something you just committed, which I find is more often the case for me.

As Mike metioned, you can also do this:

git revert HEAD 
like image 25
Nick Res Avatar answered Oct 02 '22 01:10

Nick Res