If you wish to rewind the master branch of the repository to a previous pushed commit, simply run this command — of course, with the appropriate commit hash:

git reset --hard a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9
git push --force

This may have unintended consequences if, for instance, there are collaborators on your repository. But, I am sure you know what you're doing 😉

Answer from GirkovArpa on Stack Overflow
🌐
GitHub
docs.github.com › en › desktop › managing-commits › undoing-a-commit-in-github-desktop
Undoing a commit in GitHub Desktop - GitHub Docs
If you want to edit your most recent ... GitHub Desktop. In the left sidebar, ensure you are on the Changes tab. At the bottom of the sidebar, click Undo....
Discussions

How to undo the last commit on GitHub? - Stack Overflow
I want to undo the last commit pushed to GitHub on the master branch of a repository, and make it so that it's as if this commit never existed and doesn't appear in the commit history. How do I go ... More on stackoverflow.com
🌐 stackoverflow.com
version control - How do I undo the most recent local commits in Git? - Stack Overflow
Probably worth mentioning that instead of HEAD~1 you could use the actual hash as displayed by git log --stat or by git reflog - useful when you need to 'undo' more than one commit. 2014-12-07T00:38:49.55Z+00:00 ... Save this answer. ... Show activity on this post. On SourceTree (GUI for GitHub), ... More on stackoverflow.com
🌐 stackoverflow.com
How do I revert a commit in a GitHub repository?
This command creates a new commit that undoes the changes made by the specified commit. More on github.com
🌐 github.com
3
2
March 12, 2024
git - Can I revert commits directly on GitHub? - Stack Overflow
You can delete the commit by using ... with the commit you don't want. This could also mess up histories of other people. People usually don't know git well enough to do git reset --hard origin/main so I'd not recommend it. git rebase HEAD^1 -i # do stuff git push origin main -f # absolutely playing with fire here · On PRs there's a revert button that creates as revert PR to undo all the changes ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/git › revert to previous commit on github?
r/git on Reddit: revert to previous commit on github?
January 17, 2025 -

Hi, I would like to revert to a previous commit. I believe I did this correctly on my local repository using git reset --hard commit-hash. I would like to push this to github but of this results in

error: failed to push some refs to 'https://github.com/shmish/smartmark.git'
hint: Updates were rejected because the tip of your current branch is behind

I am the only working on this repository, so I am not worried about conflicts, I am trying to move back one commit.

Top answer
1 of 2
14
As a general rule, once you push your repository to a remote repository, your commits should be considered permanent. By resetting your repository to an earlier state, you made it impossible to push to the remote repository. You have a couple options: git push -f will override that rule and allow you to push. I don't recommend this option unless you're the only user of that remote repository. If other people have been pulling from that repository, you will very likely mess them up. You also might not have the necessary permissions to do this if this is someone else's repository. Go back to where you before you executed git reset --hard and do it right. You could execute these steps: commands: git branch foo (makes a placeholder to where you are now) git pull (get your local repository back in sync with the remote) git revert git push What git revert does is construct a whole new commit that un-does the one you want to get rid of. The old one remains in the git history and then there's a new commit that reverses it. There's no way to really get rid of the old one since it's there in the remote repository and in the repositories of anybody who's pulled from it. If what you want to do something more complicated than just reverting one commit, then there is where branch "foo" comes in: git branch foo (makes a placeholder to where you are now) git pull (get your local repository back in sync with the remote) git diff HEAD foo > /tmp/patch patch -p1 < /tmp/patch git commit -a git push If it's not obvious, what this does is construct a patch that will change all the files in your current branch to what they were in your "reverted" branch. Then you apply that patch and commit the changes. My notes on the subject: http://www.efalk.org/Docs/Git/merging.html Disclaimer: these are Unix commands. If you're on Windows, you're on your own here.
2 of 2
4
I am the only working on this repository, so I am not worried about conflicts In this case, a git reset … followed by a git push --force-with-lease is totally fine. (note: a simple push -f would also work, but you should get into the habit of using force-with-lease for later.
🌐
YouTube
youtube.com › cameron mckenzie
How to Undo a Pushed Git Commit - Reset & Revert a Git Commit After Push - YouTube
Need to undo a pushed Git commit from GitHub, GitLab, Bitbucket or CodeCommit? Well, there are two ways to revert a pushed commit in git.You can revert a com...
Published   January 31, 2024
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-undo-a-commit-in-git
Undo a Commit in Git - GeeksforGeeks
April 7, 2026 - GitHub · Branch · Merge · WorkFlow · Hooks · LFS · Rebase · Last Updated : 7 Apr, 2026 · Undoing a commit in Git allows you to reverse changes made in previous commits while preserving or modifying the project history as needed. git reset is ...
Top answer
1 of 16
30141

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
# === If you just want to undo the commit, stop here! ===
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
  2. Make corrections to working tree files.
  3. git add anything that you want to include in your new commit.
  4. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

2 of 16
13009

Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand. I'll show you the 4 different ways you can undo a commit.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master

Option 1: git reset --hard

You want to destroy commit C and also throw away any uncommitted changes. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

Option 2: git reset

Maybe commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master

Do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

Option 3: git reset --soft

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

Option 4: you did git reset --hard and need to get that code back

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

Nope, there's still a way to get it back. Type this

git reflog

and you'll see a list of (partial) commit SHAs (that is, hashes) that you've moved around in. Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed

You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

🌐
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
If you'd prefer to unstage the changes but keep them in your working directory, omit the flag (the default is --mixed): ... In case you're using the Tower Git client, you can simply hit CMD+Z (or CTRL+Z on Windows) to undo the last commit:
Published   1 day ago
🌐
SheCanCode
shecancode.io › home › news & articles › how to undo a commit in github
How to undo a commit in GitHub - SheCanCode
May 27, 2025 - Reverting is a safer way to undo changes because it creates a new commit to “cancel out” a previous one without deleting it: git revert <commit-hash> The <commit-hash> is the unique identifier of the commit you want to revert.
🌐
Atlassian
atlassian.com › git › tutorials › undoing changes › git revert
How to Revert a Commit in Git? | Atlassian Git Tutorial
Instead of manually going in, fixing it, and committing a new snapshot, you can use git revert to automatically do all of this for you. The git revert command is used for undoing changes to a repository's commit history.
🌐
Augustana University
lovelace.augustana.edu › q2a › index.php › 7249 › how-can-i-undo-the-latest-3-commit-in-github
How can I undo the latest 3 commit in GitHub - Augustana CSC Q&A
Hi yall, I'm finishing my Step 7, adding a new choice of color in the ComboBox, I'm the ... know how to redo some of my latest commits. Thank you
🌐
CloudBees
cloudbees.com › blog › git-undo-commit
How to Undo Changes in Git with Git Undo Commit
November 25, 2020 - Need to fix a mistake, remove changes, revert to a previous state, or modify a message? Learn various ways to make changes in Git by using undo commit.
🌐
Git
git-scm.com › cheat-sheet
Git Cheat Sheet
Every time we say <commit>, you can use any of these: a branch · main · a tag · v0.1 · a commit ID · 3e887ab · a remote branch · origin/main · current commit · HEAD · 3 commits ago · HEAD^^^ or HEAD~3 · git restore <file> OR git checkout <file> git restore --staged --worktree <file> ...
🌐
freeCodeCamp
freecodecamp.org › news › git-remove-last-commit-how-to-undo-a-commit-in-git
Git Remove Last Commit – How to Undo a Commit in Git
September 21, 2022 - Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.md no changes added to commit (use "git add" and/or "git commit -a")