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>. Some versions of git also revert the <oldest_commit_hash> itself, 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.
Extra note for re-applying reverted commits:
Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.
To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.
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>. Some versions of git also revert the <oldest_commit_hash> itself, 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.
Extra note for re-applying reverted commits:
Usually you revert commits because you discovered the commits that you pushed turn out to have an issue. Then you first want to restore the repo to a stable state, before you continue to fix the issue. So then you would first do the git revert, as described before. Then push those revert commits, so the remote is stable. After that you would want to re-apply the reverted commits locally, so your local repo's files are back to the state before the revert. Then you can fix the issue.
To re-apply the reverted commits, you have to revert the revert commits. So what you would do, is to again execute the git revert command, but then with the range of commits of the revert commits. So your new revert commits will revert the previous revert commits. Then your files are back to the state before the first revert. Then you can fix the issue, create a new commit with the fix, and then push all the new commits.
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>
Note: previous means the commit before the erroneous commit
This will re-checkout all the updates locally (so git status will list all updated files, meaning, the files you changed/added/.. and were committed).
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
Videos
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID> to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
The above answer is not quite correct - git revert <ID> does not set your repository to that commit -- git revert <ID> creates a new commit that undoes the changes introduced by commit <ID>. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>, or git reset --hard <commit> The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>.
A note of warning that git reset will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!
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?
-
One of our artists managed to push 1 month of progress without merging
-
As you can see I tried to undo the commits one by one in reverse order
-
I couldn't undo the merge commit of Arvincle because it's empty
-
When I try to revert the commit before that I get one file I need to merge first, but the rest of the changes are not undone
Is there a way to reset the HEAD of main to the last valid commit?
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.
When trying to revert to a previous commit (for instance 123abc) ...
The emphasis I added here on the preposition "to", as in revert to, is crucial. The accepted answer at your linked question (How do I revert a Git repository to a previous commit?) calls this out:
This depends a lot on what you mean by "revert".
Running git revert on a single commit may not suffice. See the answers there about reverting multiple commits, if needed.
Running git reset --hard may suffice, but introduces the very problem you've encountered. The things to understand here are:
A branch name simply lets Git find one particular commit. We say that the branch name points to the commit. Using
git reset, we can change the one particular commit to which some branch name points. Nothing else happens in the repository yet, although depending on the kind ofgit resetyou run, something else might happen beforegit resetfinishes: the first step of thisgit resetis just to change one of your own branch names. Since you're considering (or have used)--hard, some more things will change in your repository, but they're less important at the moment.Every Git repository has its own branch names. Moving one of your branch names has no effect on any other repository.
What you need to do is to get some other Git repository to change one of its branch names. That's where you run into this problem:
I cannot push this (I need to pull before and pulling moves me forward).
The
git pushcommand is how you ask—or, using--force, command—some other Git repository to change or create or delete some of its names (branch names, tag names, and other such names). But each Git repository is set up to easily accept new incoming commits, yet at the same time, resist the suggestion to throw away any existing commits.
When you use git reset to move one of your branch names "backwards", so as to revert to (not revert as in add-a-commit-that-backs-out) some previous commit, you're deliberately throwing away some existing commit(s). Since you control your own Git repository, you can definitely do this to your own repository. Since the git reset command is meant to do this kind of throwing-away,1 it does so without complaint. But git push isn't, and their Git complains.
As soon as you use git pull, their repository has you put back all the commits you snipped out of your own repository. Git is, after all, designed to add commits as easily as possible! That leaves you with the situation you're in.
You now have a choice:
Force the other Git repository to discard some commits. This requires sufficient permissions. It also means that everyone else who is using that other Git repository need to take action with any clones they have made, because their clones will enthusiastically put back all the commits you are attempting to prune. So this is kind of mean to those other people.
Or, use
git revertor some other command to add some commit(s) to your repository that, in the end, result in putting the files back, but don't remove any old commits. The new commits simply add on to the old ones. The old ones are still there for anyone who would like to ask about them (git log) or use them (git switch --detach hash-id, for instance).
Adding new commit(s) is what Git is designed to do, so the latter is the way to go unless there's some really strong reason to ditch the old commits.
As the question you linked notes, all of this is a lot easier with unpublished commits: a commit that only you have, in your own private repository, is simply not in any other Git repository. If you shave those commits off your own branches, nobody will ever know. They won't be able to object that a git push shaves those commits off their branches, because those commits aren't on their branches. (Again, each branch name is local to each Git repository. One repository can show another one the commit hash ID stored in its branch name, but each repository takes responsibility in keeping its own hash IDs in its own branch names. The commits get shared; the branch names do not.)
Since the commits you're looking at are published you cannot use this short-cut.
1This makes git reset a very-high-powered tool, like some sort of flame-throwing chainsaw, or industrial steel-cutting laser, or something. This over-powered-ness is part of the reason that Git 2.23 now has git restore: some of the things you can do, that used to require using git reset, can now be done with the rather gentler git restore. Both commands will throw away in-progress work if you ask them, but git restore is more like a hand-saw, or bolt cutters, or something along these lines.
You should try using git revert <commit hash>. This will revert the selected commit and you can then push the changes
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
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)