If you're sure that neither soft reset nor creating multiple branches work for your use case, you could do

git diff HEAD commit_hash_to_go_to | git apply

This will create a diff of changes between the latest commit on your branch and the commit with the desired state and automatically apply it. That will simply change the files, it's your job to add them to staging and commit the result. Might be useful if you want to try out different solutions and keep the history of your changes WITHIN the same branch or avoid multiplying local branches.

If you encounter "cannot apply binary patch to without full index line" error, add --binary flag:

git diff HEAD commit_hash_to_go_to --binary | git apply

Before doing this ensure that you've got no uncommitted changes - otherwise the patch won't be applied (it's atomic so either all changes go through or none, so you won't end up in an inconsistent state)

NOTE: this simply changes the files and marks them as modified. It does NOT alter commit history or create new commits

HINT: If you just want undo the last commit, you can do

git diff HEAD HEAD~1 | git apply

The "~1" is how much commits you want to go back. In this case only 1 commit back.

Answer from wondersz1 on Stack Overflow
Top answer
1 of 5
36

If you're sure that neither soft reset nor creating multiple branches work for your use case, you could do

git diff HEAD commit_hash_to_go_to | git apply

This will create a diff of changes between the latest commit on your branch and the commit with the desired state and automatically apply it. That will simply change the files, it's your job to add them to staging and commit the result. Might be useful if you want to try out different solutions and keep the history of your changes WITHIN the same branch or avoid multiplying local branches.

If you encounter "cannot apply binary patch to without full index line" error, add --binary flag:

git diff HEAD commit_hash_to_go_to --binary | git apply

Before doing this ensure that you've got no uncommitted changes - otherwise the patch won't be applied (it's atomic so either all changes go through or none, so you won't end up in an inconsistent state)

NOTE: this simply changes the files and marks them as modified. It does NOT alter commit history or create new commits

HINT: If you just want undo the last commit, you can do

git diff HEAD HEAD~1 | git apply

The "~1" is how much commits you want to go back. In this case only 1 commit back.

2 of 5
14

The easiest thing to do, like you say, would be to simply create a new branch where HEAD is and then revert development to the commit you want to resume work from:

git checkout development   # Make HEAD point to the 'development' branch
git branch beforeRevert    # Create a new branch reference pointing to HEAD
git reset --hard c14809fa  # Move HEAD, the index and your working copy to c14809fa

Here's a graphical representation of what will happen:

    Step 1               Step 2                  Step 3

            develop    develop, beforeRevert   develop   beforeRevert
           /                    /             /         /
A-B-C-D-E-F          A-B-C-D-E-F             A-B-C-D-E-F
          ^                    ^             ^
         HEAD                 HEAD          HEAD

The important thing here is that HEAD is always pointing to the development branch, so that's the branch that gets moved when you run git reset --hard c14809fa. The new beforeRevert branch will still point to where HEAD was before the revert.

🌐
Reddit
reddit.com › r/git › new to git, advice needed. revert commit but keep changes
r/git on Reddit: New to git, advice needed. Revert commit but keep changes
November 18, 2023 -

Hello everyone! I'm working on a project in a team. I'm using GUI git client Fork, while also learning command line git.
I finished a task and made a commit (not pushed), but soon realized that I didn't get it done properly, and that I needed to put more work into it. So I left my initial commit as it was, and just kept on working on the task. Right now I'm finally finishing it, but I'm asked to make only one commit with the whole task in it.

When I try to Revert the initial commit, it gives me an error:

Your local changes to the following files would be overwritten by merge
Revert failed

What should I do to keep the changes in the initial commit and reapply the new ones? I may have rewritten some code from the initial commit, but not fully, so some parts must stay, and the new changes to be added.

Thank you for your attention!

Discussions

git checkout - How do I revert a Git repository to a previous commit? - Stack Overflow
It does not delete anything (pushed or upushed) from the history. It produces one clean commit which represents the state we want to revert back to. * by removing untracked but not ignored files (the ones specified in .gitignore) from working tree. The working tree is empty except for the ignored files which we wanted to keep ... More on stackoverflow.com
🌐 stackoverflow.com
I'm struggling to understand how to roll back to a previous point. I want to disregard the crossed out chain completely. What am I missing here?
Edit: That was definitely it. I undid the new commit and created a new branch and it is acting as I was expecting now. I decided the last 3 commits I did were just not going to work, so I wanted to roll back to my v0.1.5a commit. So I "Undo Last Commit" a few times to get back there, then tried committing to a chain. I didn't actually create a branch, maybe that was what I did wrong? More on reddit.com
🌐 r/git
12
0
December 11, 2024
how do I revert to a previous commit without changes for this scenario ?
I do not think revert does what you think it will do. revert doesn't "revert" back to a previous point in history; it makes a new commit, which "reverts" (undoes / specifically does the opposite of) the commits you've given it. If you have changes to files after the range you've given, you likely will have a merge conflict to fix. To go back in time, you normally would reset to that point; however, if those commits are already on a 'public' server, and you do not want to edit history; then using reverts is the best way. If you are happy with the difference, and are sure which you want to do. Now in this case, you have a dirty tree. You currently have changes that you have not commited. you need to commit them, or stash them, or discard them as the error suggests. git reset --hard commitEarlierNo will get you there directly, if you do not mind potentially changing "public" history on some external server. otherwise git reset --hard HEAD followed by your revert of commits. now if you want, you can still keep those changes with a commit (so long as you correctly change the end revert range reference) or stash them (git stash) and get them back after with a git stash pop be aware that you may have conflicts after that. More on reddit.com
🌐 r/git
6
3
August 4, 2022
I want to revert the code to a previous commit.
git checkout -f HEAD~16 -- . will make your code go back 16 commits in your local storage, without rewriting any history with those 16 commits. The "changes" (reverting the past 16 commits) should be at the staging area at this moment. Now you can git commit to commit those reverted code and then git push to update your Github repo. To be sure you did it right, try git diff HEAD..HEAD~17 to compare your branch to the branch 17 commits ago (the 16 commits you wanted to revert + the new commit that did the revert = 17 commits). If the command outputs nothing, you're all good. More on reddit.com
🌐 r/git
4
2
November 8, 2022
🌐
GitLab
docs.gitlab.com › topics › git › undo
Revert and undo changes | GitLab Docs
For more information about purging ... and you want to change it back to how it was in the previous commit, but keep the commit history, you can use git revert....
🌐
freeCodeCamp
freecodecamp.org › news › git-reverting-to-previous-commit-how-to-revert-to-last-commit
Git Reverting to Previous Commit – How to Revert to Last Commit
October 19, 2022 - I have already initialized the ... looks like: ... To revert to the to the previous commit, run the git revert command along with the commit ID of the current commit....
Top answer
1 of 16
12602

This depends a lot on what you mean by "revert".

Temporarily switch to a different commit

If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit:

# This will detach your HEAD, that is, leave you with no branch checked out:
git checkout 0d1d7fc32

Or if you want to make commits while you're there, go ahead and make a new branch while you're at it:

git checkout -b old-state 0d1d7fc32

To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.)

Hard delete unpublished commits

If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset:

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again.

Undo published commits with new commits

On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option.

With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history.

First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits.

# This lists all merge commits between 0d1d7fc and HEAD:
git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

# This lists all non merge commits between 0d1d7fc and HEAD:
git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' '

Note: if you revert multiple commits, the order matters. Start with the most recent commit.

# This will create three separate revert commits, use non merge commits only:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

# Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit. You can also use a range of merge commits here.
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

The git-revert manpage actually covers a lot of this in its description. Another useful link is this git-scm.com section discussing git-revert.

If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section).

You may also find this answer helpful in this case:
How can I move HEAD back to a previous location? (Detached head) & Undo commits

2 of 16
3981

Lots of complicated and dangerous answers here, but it's actually easy:

git revert --no-commit 0d1d7fc3..HEAD
git commit
git push

This will revert everything from the HEAD back to the commit hash (excluded), meaning it will recreate that commit state in the working tree as if every commit after 0d1d7fc3 had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to.

(The --no-commit flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.)

This is a safe and easy way to rollback to a previous state. No history is destroyed, so it can be used for commits that have already been made public.


Note on merge commits:
If one of the commits between 0766c053..HEAD (inclusive) is a merge then there will be an error popping up (to do with no -m specified). The following link may help those encountering that: Why does git revert complain about a missing -m option? (thanks @timhc22 for pointing out)

🌐
Stack Abuse
stackabuse.com › git-revert-to-a-previous-commit
Git: Revert to a Previous Commit
February 16, 2023 - The reset command has three different options, two of which we'll describe here: ... Using the --hard option, everything is reverted back to the specific commit. This includes the commit history reference pointers, the staging index, and your working directory.
Find elsewhere
🌐
CloudBees
cloudbees.com › blog › git-revert-explained
Git Revert Explained: Safely Undoing Your Changes
November 29, 2021 - Git revert undoes changes in a project commit history without tampering with it. When reverting, this operation takes the specific commit, inverts the changes from that commit, and implements a new reverse commit—only removing the changes ...
🌐
Reddit
reddit.com › r/git › i'm struggling to understand how to roll back to a previous point. i want to disregard the crossed out chain completely. what am i missing here?
r/git on Reddit: I'm struggling to understand how to roll back to a previous point. I want to disregard the crossed out chain completely. What am I missing here?
December 11, 2024 - If you absolutely want to roll-back your branch to a previous commit, forgetting all newer commits: git reset --hard <oldcommitID> ... This is great feedback, thank you! Very fair, I was just kinda clicking buttons in the GUI just hoping I hit ...
🌐
Reddit
reddit.com › r/git › how do i revert to a previous commit without changes for this scenario ?
r/git on Reddit: how do I revert to a previous commit without changes for this scenario ?
August 4, 2022 -

Hi guys,

I am stuck in another tricky situation.

When I discovered I have done certain commits that are wrong, then I did :

git reset --hard commitNo1

I went on to do the changes that I need to only to discover that the code is already mixed with some code-generation stuff. No choice, I then wanted to revert to a earlier version :

I did

git revert --no-commit commitEarlierNo..HEAD

following this suggestion :

https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit

and now I got this below message:

error: Your local changes to the following files would be overwritten by merge:

Please commit your changes or stash them before you merge
Aborting
fatal: revert failed

Please help me now what can i do by aborting all the changes and I just want to revert to that even earlier version, and I am ok to discard changes I have made so far after I did that reset Hard to that commitNo1

🌐
Git Tower
git-tower.com › learn › git faq › how to undo, revert, or delete a git commit
How to Undo, Revert, or Delete a Git Commit | Learn Version Control with Git
It creates a new commit that applies the exact opposite of the changes in the targeted commit: ... This makes git revert safe to use on shared branches — it never rewrites history, so your collaborators are not disrupted.
Published   6 days ago
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes
Undoing Changes in Git | Atlassian Git Tutorial
December 15, 2025 - Commits are snapshots of a point in time or points of interest along the timeline of a project's history. Additionally, multiple timelines can be managed through the use of branches. When 'undoing' in Git, you are usually moving back in time, or to another timeline where mistakes didn't happen. This tutorial provides all of the necessary skills to work with previous revisions of a software project. First, it shows you how to explore old commits, then it explains the difference between reverting public commits in the project history vs.
🌐
TechTarget
techtarget.com › searchitoperations › answer › How-to-roll-back-Git-code-to-a-previous-commit
How to roll back Git code to a previous commit | TechTarget
Admins can also use git revert. This command undoes the effects of a bad or incorrect commit by creating a commit that does the opposite of the commit in question. By reverting the commit in this manner, the history stays intact.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
It's important to understand that git revert undoes a single commit—it does not "revert" back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.
🌐
LabEx
labex.io › tutorials › git-how-to-revert-a-git-commit-without-losing-changes-415168
How to revert a Git commit without losing changes | LabEx
You practiced inspecting the commit history with git log, performing a standard git revert to create a new commit that undoes previous changes, and using the git revert --no-commit option to revert changes to your staging area, allowing you to modify them before creating a new commit.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › how to revert a git commit
Git Revert Commit | Solutions to Git Problems
February 5, 2024 - If you need to make revisions to a commit that is not your last commit, the best solution is to create a new commit by reverting the old commit. As a Git best practice, you should avoid doing anything that will require you to force push — ...
🌐
Medium
jeevabyte.medium.com › how-to-revert-to-a-previous-commit-in-git-and-make-it-the-latest-one-6ac766ff78f4
How to Revert to a Previous Commit in Git and Make It the Latest One | by Jeeva-AWSLabsJourney | Medium
March 9, 2025 - If you don’t want to delete commits but instead create new commits that undo the last five, use: ... This will create new commits that reverse the changes made in the last five commits while keeping your commit history intact.
🌐
Reddit
reddit.com › r/git › i want to revert the code to a previous commit.
r/git on Reddit: I want to revert the code to a previous commit.
November 8, 2022 -

I did some feature changes in my code but now I want it to be like the same code which was 16 commits ago. All my changes are pushed to GitHub and I do not want to lose my history or change my branch. How do I do this in the most minimum steps?

🌐
LabEx
labex.io › tutorials › git-how-to-undo-git-commit-but-keep-changes-392512
How to Undo Git Commit But Keep Changes | LabEx
In this example, the "Revert Commit 4" commit undoes the changes made in Commit 4, while preserving the rest of the commit history. If the changes introduced by the reverted commit conflict with other changes in your repository, Git will prompt ...
🌐
Quora
quora.com › Is-it-possible-to-undo-a-Git-commit-without-rewriting-history
Is it possible to undo a Git commit without rewriting history? - Quora
Answer (1 of 3): Mikko Rantalainen already replied the original question with precision. However, I’ll bend it a little. If by “rewriting history” we only mean rewriting remote history, it may be possible. Let me explain a bit further. Everything that happens in local, stays in local ...