796

I cloned a Git repository and then tried to roll it back to a particular commit early on in the development process. Everything that was added to the repository after that point is unimportant to me so I want to omit all subsequent changes from my local source code.

However, when I try to roll back in the GUI tool it doesn't update my local file system - I always end up with the latest source code for the project.

What's the correct way to just get the source for a repository as of a particular commit in the project's history and omit all later updates?

2
  • 3
    git reset --hard aabb3434 / git push origin branch --force Oct 17, 2016 at 19:00
  • The most useful questions on stackoverflow are the ones most likely to be closed for being a duplicate
    – BigMistake
    Sep 25 at 1:08

4 Answers 4

1258
git reset --hard <tag/branch/commit id>

Notes:

  • git reset without the --hard option resets the commit history, but not the files. With the --hard option the files in working tree are also reset. (credited user)

  • If you wish to commit that state so that the remote repository also points to the rolled back commit do: git push <reponame> -f (credited user)

14
  • 4
    @MariuszNowak after doing git reset --hard <commit-id>, ( 2 back ) when doing "git push -f origin master" I get "remote: error: denying non-fast-forward refs/heads/master (you should pull first)" it is my repo and I want to take it back :)
    – peterk
    Mar 21, 2012 at 19:25
  • 17
    @peterk try --force at the end of your git push command
    – Arcolye
    Apr 3, 2012 at 10:07
  • 6
    Make sure you specify the branch name as well.
    – eipark
    Jul 14, 2012 at 8:06
  • 2
    In server end there is a configuration option for the repository if non-fast-forward pushes are allowed with --force. man git-config and check .git/config for denyNonFastForwards = true in repository.
    – kauppi
    Nov 7, 2012 at 17:24
  • 3
    The second option for me did not run: I had to do git push -f <remoterepo> master to restore the remote repository too. However, be aware that with the -f switch the commits after the restored commit are also LOST on the remote repo! (this is what I wanted but someone else may wish to preserve history).
    – Kounavi
    Feb 18, 2015 at 12:11
426

Update:

Because of changes to how tracking branches are created and pushed I no longer recommend renaming branches. This is what I recommend now:

Make a copy of the branch at its current state:

git branch crazyexperiment

(The git branch <name> command will leave you with your current branch still checked out.)

Reset your current branch to your desired commit with git reset:

git reset --hard c2e7af2b51

(Replace c2e7af2b51 with the commit that you want to go back to.)

When you decide that your crazy experiment branch doesn't contain anything useful, you can delete it with:

git branch -D crazyexperiment

It's always nice when you're starting out with history-modifying git commands (reset, rebase) to create backup branches before you run them. Eventually once you're comfortable you won't find it necessary. If you do modify your history in a way that you don't want and haven't created a backup branch, look into git reflog. Git keeps commits around for quite a while even if there are no branches or tags pointing to them.

Original answer:

A slightly less scary way to do this than the git reset --hard method is to create a new branch. Let's assume that you're on the master branch and the commit you want to go back to is c2e7af2b51.

Rename your current master branch:

git branch -m crazyexperiment

Check out your good commit:

git checkout c2e7af2b51

Make your new master branch here:

git checkout -b master

Now you still have your crazy experiment around if you want to look at it later, but your master branch is back at your last known good point, ready to be added to. If you really want to throw away your experiment, you can use:

git branch -D crazyexperiment
10
  • 32
    Thanks - might not have been what the poster asked for, but much less anxiety-inducing for me. Jun 22, 2010 at 17:12
  • 9
    Dealing with a remote repository (such as Github), you may have to do a 'git push -f origin master' because of the non-fastforwards to get master to look like it did at the specified commit, but this method is much cleaner than reset.
    – jcalvert
    Mar 9, 2011 at 16:04
  • 1
    git reset --hard is not that particularly dirty. It is introduced in nearly every git tutorial available as the proper method to undo changes in your own tree. If not in basic features, at least in intermediate ones. If you somehow get into an unknown state/HEAD, there's always git reflog which tells the history of HEAD regardless of branches. Pick a proper commitid from there, and branch from it or git reset --hard with that.
    – kauppi
    Jun 17, 2011 at 10:57
  • 3
    kauppi - wouldnt you then have issues pushing back to a remote repo?
    – HaveAGuess
    Nov 14, 2012 at 2:01
  • 2
    Oops, that should be git branch --set-upstream master <remote>/<branch> and git branch --set-upstream crazyexperiment <remote>/<branch> Jan 29, 2014 at 6:45
14

For those with a git gui bent, you can also use gitk.

Right click on the commit you want to return to and select "Reset master branch to here". Then choose hard from the next menu.

2
  • 1
    no workee when server (stash) disallows force on push. Cannot reset back there.
    – maxweber
    Jun 1, 2015 at 18:43
  • git hard reset "enter commit id here" : It will roll back to you till that stage May 11, 2022 at 10:42
1

When you say the 'GUI Tool', I assume you're using Git For Windows.

IMPORTANT, I would highly recommend creating a new branch to do this on if you haven't already. That way your master can remain the same while you test out your changes.

With the GUI you need to 'roll back this commit' like you have with the history on the right of your view. Then you will notice you have all the unwanted files as changes to commit on the left. Now you need to right click on the grey title above all the uncommited files and select 'disregard changes'. This will set your files back to how they were in this version.