1317

I have a project in a remote repository, synchronized with a local repository (development) and the server one (prod). I've been making some commited changes already pushed to remote and pulled from the server. Now, I want to undo those changes. So I could just git checkout to the commit before the changes and commit the new changes, but I'm guessing that there will be problems to push them again to remote. Any suggestion on how should I proceed?

0

14 Answers 14

1532

You can revert individual commits with:

git revert <commit_hash>

This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits that come after that. If you want to revert a range of commits, you can do it like this:

git revert <oldest_commit_hash>..<latest_commit_hash>

It reverts all the commits after <oldest_commit_hash> up to and including <latest_commit_hash>. On some versions of git it also reverts the <oldest_commit_hash>, so double check if that commit gets reverted or not. You can always drop the latest revert commit (which reverts the oldest commit) with g reset --hard HEAD~.

To find out the hash of the commit(s) you can use git log.

Look at the git-revert man page for more information about the git revert command. Also, look at this answer for more information about reverting commits.

19
  • 53
    In other words it reverts to the <oldest_commit_hash> ;)
    – David
    Jan 3, 2016 at 4:58
  • 4
    In git documentation, it says that revert command reverts commits between first and last commits (both first and last included). See Documentation
    – Onur Demir
    Aug 29, 2016 at 12:55
  • 7
    @aod is correct, this answer needs to be updated. the current git API for revert has <oldest_commit_hash> being included in the list of reverts
    – JHixson
    Jan 19, 2018 at 18:24
  • 10
    with git 2.17.2 revert <old>..<new> does not include old but includes <new>
    – chingis
    Dec 18, 2018 at 13:36
  • 14
    just tried....final step is missed, after "git revert <commit_hash>" use "git push origin <your_Branch>"
    – VicXj
    May 12, 2021 at 8:53
688

A solution that keeps no traces of the "undo".

NOTE: don't do this if someone already pulled your change (I would use this only on my personal repo.)

run:

git reset <previous label or sha1>

this will re-checkout all the updates locally (so git status will list all updated files)

then you "do your work" and re-commit your changes (Note: this step is optional)

git commit -am "blabla"

At this moment your local tree differs from the remote

git push -f <remote-name> <branch-name>

will force the remote branch to take this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).

!! watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag

17
  • 15
    -a all tracked files will be commited -m commit message follows
    – jo_
    Oct 12, 2015 at 15:52
  • 11
    Exactly what I searched for. Somebody did a faulty commit and push and reverted it again afterwards. The branches could not be merged because of this and I wanted to get the repository to be in the correct state again (and removing the commit from history as they were faulty ones anyway)
    – byemute
    Oct 30, 2015 at 15:27
  • 5
    Which "previous label or sha1" should I use?. Should I enter the last "correct" one or the one before that and re-do all the changes done by the last correct one?
    – elysch
    Mar 28, 2016 at 15:19
  • 21
    the one just before the eroneous commit
    – jo_
    Mar 30, 2016 at 17:07
  • 3
    Exactly what I need. I've pushed a branch to master by mistake. And as a result I had many rubbish commits during all commits history. I just done git push -f for last correct commit and remote history cleaned up! Thanks! Dec 16, 2016 at 13:04
437

What I do in these cases is:

  • In the server, move the cursor back to the last known good commit:

    git push -f origin <last_known_good_commit>:<branch_name>
    
  • Locally, do the same:

    git reset --hard <last_known_good_commit>
    #         ^^^^^^
    #         optional
    



See a full example on a branch my_new_branch that I created for this purpose:

$ git branch
my_new_branch

This is the recent history after adding some stuff to myfile.py:

$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date:   Wed Mar 23 12:48:03 2016 +0100

    Adding new stuff in myfile.py

commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date:   Tue Mar 18 12:46:59 2016 +0100

    Initial commit

I want to get rid of the last commit, which was already pushed, so I run:

$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
 + 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)

Nice! Now I see the file that was changed on that commit (myfile.py) shows in "not staged for commit":

$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   myfile.py

no changes added to commit (use "git add" and/or "git commit -a")

Since I don't want these changes, I just move the cursor back locally as well:

$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit

So now HEAD is in the previous commit, both in local and remote:

$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date:   Tue Mar 18 12:46:59 2016 +0100

    Initial commit
14
  • 31
    This is the correct answer! this is exactly what needed o be done in my case thanks learned something new today :) May 17, 2018 at 3:31
  • 3
    This is the correct answer! It is regarding pushed(!) commits!
    – powtac
    Jun 29, 2018 at 14:07
  • 12
    If you want to truly revert changes (as if you've never pushed them), this is the correct answer.
    – Jacob
    May 13, 2019 at 6:27
  • 1
    Perfect answer. Worked well as expected!
    – RafiAlhamd
    Feb 15, 2020 at 13:39
  • 1
    Perfect answer. Jun 2, 2020 at 9:28
255

This will remove your pushed commits

git reset --hard 'xxxxx'

git clean -f -d

git push -f

Make a backup just in case

Two notes bumped up from comments:

  • 'xxxxx' is the hash code of the last commit that you want to keep.

  • clean is dangerous. It removes files, not under version control!

16
  • 22
    This method properly rewrites the history to delete the commit. Awesome Aug 12, 2019 at 5:45
  • 7
    this answer is el magnifeco!
    – daparic
    Nov 19, 2019 at 11:50
  • 5
    Just to be clear (I wasn't sure): The commit will be the current commit after the operation. Not the last one to delete.
    – user1199624
    Apr 30, 2020 at 14:06
  • 2
    Thanks a million! Apr 15, 2021 at 17:36
  • 5
    clean is dangerous. It removes files, which not under version control! Sep 20, 2021 at 17:53
122

You can REVERT (or you can also call it DELETE) the Git Commit BOTH Locally and Remotely if you follow the steps as given below via git command line.

Run the following command to see the commit id that you want to revert

git log --oneline --decorate --graph

You will get like a following screenshot enter image description here

If you also check remote (via Web Interface) then you can see that this would be same as shown below

enter image description here

As per screenshot currently you are on commit id e110322 however you want to revert back to 030bbf6 BOTH LOCALLY and REMOTELY.

Perform the following steps to DELETE/REVERT Commits Locally+Remotely


First Locally Reverting to commit id 030bbf6

git reset --hard 030bbf6

followed by

git clean -f -d

These two commands clean force reset to commit stage 030bbf6 as shown below in snapshot

enter image description here

now if you run git status then you'll see that you are TWO Commits BEHIND from the remote branch as shown below enter image description here

Run following to update your indexes (if there are any updates). It is recommended that you ask all developers not to accept any pull requests on main remote branch.

git fetch --all

Once you are done with it then you are required to Push this commit forcefully by using + symbol in-front of branch as shown below. I have used here as master branch, you can replace it with any

enter image description here Code

git push -u origin +master

now if you see the web interface of remote then commit there should be reverted as well.

enter image description here

6
  • 1
    This is the best answer on here, because it actually explains with an example! Thanks for that. However, I get the following error: ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'gitlab.com:information-provision/collectors/deribit-collector.git' Despite having maintainer status etc Jul 19, 2021 at 8:09
  • 1
    @user6400946 are you using '+' (plus) symbol as shown in the last command ? git push -u origin +YourBrandName
    – vibs2006
    Jul 19, 2021 at 12:04
  • Yes, howeverI just solved it. It seems my master branch was protected from force push and I needed to unprotect it temporarily (under gitlab repository settings). Jul 19, 2021 at 12:56
  • 1
    works well in 2022 :D
    – danu
    Mar 2, 2022 at 1:44
  • 1
    These steps are easy to implement still in 2022 era. Good one! Thank you
    – V P
    Jun 7, 2022 at 6:03
115

Let's say 61234 is the sha-number of the last good commit you want to keep.

git reset --hard 61234
git push -f


 will remove completely all wrong commits without any trace.

Note: If you wanted to push (the commit you reset to) to a specific branch you'd use git push -f origin branch-name instead.

5
  • 2
    Thank you! And the commit history is innocent! To convenient getting sha-number I've used: $ git log --oneline <feature-branch-name>.
    – InaFK
    Apr 14, 2021 at 11:16
  • 2
    This should have ben the accepted answer!
    – Stone
    Sep 17, 2021 at 18:22
  • I got remote: GitLab: You are not allowed to force push code to a protected branch on this project. error for running git push -f origin master. I think I have to change some configuration on my gitlab server.
    – Brian
    Oct 29, 2021 at 9:15
  • I edited your answer because I almost ran git push -f origin master when I really wanted to reset a commit on a branch – glad it didn't go! Dec 23, 2021 at 17:27
  • 1
    I'm like you , always need push-back , use this way : git reset --hard <last-good-commit-hash> and after git push -f origin branch-name Mar 8, 2022 at 5:57
57

2020 Simple way :

git reset <commit_hash>

(The hash of the last commit you want to keep).

You will keep the now uncommitted changes locally.

If you want to push again, you have to do :

git push -f
2
  • 1
    It does not work always as the admin can block re-write history which is very common in the industry. So the solution mentioned by @gitaarik will work. git revert <commit_hash> git push origin <branch_name>
    – tejp124
    Oct 19, 2020 at 13:26
  • This is short, simple, and it does exactly what I need. Thanks!
    – RedGuy11
    Jul 8, 2021 at 15:55
31
git revert HEAD -m 1

In the above code line. "Last argument represents"

  • 1 - reverts one commits.

  • 2 - reverts last two commits.

  • n - reverts last n commits.

You need to push after this command to take the effect on remote. You have other options like specifying the range of commits to revert. This is one of the option.


Later use git commit -am "COMMIT_MESSAGE" then git push or git push -f

2
  • 9
    This is not true - the -m parameter specifies number of parent to revert to (typically 1 if you want to revert the incoming, "theirs", changes, as opposed to 2 for merged-to changes, "ours" - for merging 2 commits). It has nothing to do with number of commits reverted - if you want to revert a commit range, use git revert ref1..ref2 Oct 27, 2016 at 13:18
  • 2
    Didn't have the desired effect
    – Sam Tuke
    Jun 4, 2018 at 10:24
21

Here is my way:

Let's say the branch name is develop.

# Checkout a new temp branch based on one history commit
# (command to get history commit hashes: git log --oneline)
git checkout <last_known_good_commit_hash>

# Delete the original develop branch 
git branch -D develop
# Create a new develop branch based on the temp branch
git checkout -b develop

# Force update the remote with the new branch
git push -f origin develop

4
  • 2
    for me there were many things to learn from this answer, so I went with this.
    – AUR
    Oct 22, 2021 at 9:19
  • I faced with illegal symbols on mac and only this solution helped me save the project.
    – storenth
    Jan 11 at 17:55
  • It seems too risky to delete the develop branch just to undo a commit. What if it's a very busy project?
    – Nikita128
    Jan 25 at 17:35
  • @Nikita128 You are right, if it's a very busy project and this method will remove others commits for sure. Jan 25 at 21:20
19

The reset hard worked for me: Thanks @Mo D Genensis and @vibs2006


git reset --hard 'your last working commit hash'

git clean -f -d

git push -f
2
  • This overrides the files that are changed and deletes the files that are added.
    – Sahin
    Dec 15, 2021 at 9:58
  • This seems a duplicate of Mo D Genesis answer. May 3, 2022 at 15:10
7

git reset <commit_hash> (to get the<commit_hash> use git log --oneline)

git restore . to restore all the changed files to the version of yout target commit

git push origin master --force to force the push to your remote master branch. But be careful when using the force push if there is anyone working with you in the same branch

5

To do it cleanly:

git rebase -i <hash of last good commit, 9 chars is enough>

Now you will get a list of the commits from the last good commit to the HEAD with options what to do with each commit. DROP will throw away that commit. Save the file.

Now to fix the upstream do :

git push --force-with-lease

(With lease so you don't accidentally cause problems for someone else working on your pushed update)

This keeps the log clean by removing the wrong commit instead of introducing new commits fixing earlier erroneous commits.

2
  • 1
    This doesn't work as it creates a diverged branch and you can't push it (not even with -force). But would be the nicest solution if it was working...
    – rimes
    Jun 11, 2021 at 4:06
  • @rimes, I have used this type of construction, perhaps not often but, a number of times on code meant for production. A colleague (our resident git guru) uses it to squash code from PR's with a large number of commits to combine commits and keep the master branch log clean. Jun 16, 2021 at 8:47
3

You can do something like

git push origin +<short_commit_sha>^:<branch_name>
1
  • Could you elaborate on the +,^ and : characters in the command? Jul 1, 2021 at 19:23
-1

Another way to do this without revert (traces of undo):

Don't do it if someone else has pushed other commits

Create a backup of your branch, being in your branch my-branch. So in case something goes wrong, you can restart the process without losing any work done.

git checkout -b my-branch-temp

Go back to your branch.

git checkout my-branch

Reset, to discard your last commit (to undo it):

git reset --hard HEAD^

Remove the branch on remote (ex. origin remote).

git push origin :my-branch

Repush your branch (without the unwanted commit) to the remote.

git push origin my-branch

Done!

I hope that helps! ;)

Not the answer you're looking for? Browse other questions tagged or ask your own question.