GitHub - How to revert changes to previous state

72,903

Solution 1

You basically have two options to revert changes:

  1. create a new commit which applies reverse changes. This is the preferred option as it doesn't changes history on a public repository
  2. Remove the commits and force push them.

The first option can be achieved by using git revert

git-revert - Revert some existing commits

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them.

An example would be git revert -n HEAD~5..HEAD. This command creates 5 new commits, each of which undoes one of the last 5 commits of the currently checked out branch.

The second option would be to actually remove the commits. Note that this changes history in the repository. So anyone who has already pull the changes will probably be rather surprised and things can get messy quickly. That said, you can do

git reset --hard HEAD~5
git push --force

The first command will wipe any uncommitted changes in your current working copy. and reset your local repository to the state of the current HEAD - 5 commits. The second command will force-push to the default remote (i.e. GitHub) There, any changes diverging from your current local repository are overwritten.

A note of warning again: If you don't really know what you are doing, don't use this option as it can lead to data loss for you or others if not done right. Use the first option instead as it will transparently remove changes but without the nasty side-effects of history-rewriting.

Solution 2

You can do git revert <commit> to all the commits that have been made after your required state. (In the reverse order to avoid any conflicts.)

This is a clean way if there are other people sharing the repo, but a little effortsome. (You may automate though...?)

Solution 3

Do a git push -f. Not a good idea if there are other people using the same repo.

Solution 4

Do a git checkout, then commit it to the branch you want. This will make a new commit with the old code (so you'll have 6 commits).

git checkout HEAD~3, where 3 is the number of commits back you want to revert to.

Better yet, you can checkout a single file into the present HEAD:

git checkout 3425661dba2aadccdbab:path/to/file/from/base

This will reduce the likelihood of making other people angry with you pulling the proverbial rug out from under their feet.

EDIT:

There's a similar question here:

Checkout old commit and make it a new commit

Share:
72,903

Related videos on Youtube

Zhen
Author by

Zhen

Updated on July 09, 2022

Comments

  • Zhen
    Zhen almost 2 years

    I am using GitHub as my remote repository.

    I have already pushed 5 commits to the server and want to revert back to the state before the those commits.

    If the commit hash is 3425661dba2aadccdbab, how do I revert the entire local/remote back to that commit? I tried

    $ reset --hard 3425661dba2aadccdbab
    

    but that only resetted my working head to that branch and requires me to do a git pull again. I tried checkout, but this caused me to land in a "detached head" branch.

    • Nick
      Nick almost 13 years
      Does this need the github tag? It doesn't seem to be github specific.
  • meow
    meow almost 13 years
    i think the OP has other people using the same repo
  • beatgammit
    beatgammit almost 13 years
    Git rebase it too before you push, that will make them even more mad. =)
  • meow
    meow almost 13 years
    hmmm, but as the OP said, if the git checkout, he will end up in a "detached head" branch. i got this before too ;)
  • cynistersix
    cynistersix over 9 years
    Is it possible to force the push to only a specific branch?
  • Chandra Sekar
    Chandra Sekar over 9 years
    @cynistersix git push -f remote_name your_private_branch.
  • Rutger Hofste
    Rutger Hofste over 6 years
    Can you explain what HEAD~5..HEAD does?
  • Holger Just
    Holger Just over 6 years
    @RutgerHofste It described the range of commits between HEAD (i.e. the currently checkout commit) and 5 commits before HEAD. The git revert would thus create revert commits for the last 5 commits. Similarly, the git reset command would move the current branch 5 commits back. See this blog post for a nice explanation on how to specify relativ commits for commands.