ChatGPT解决这个技术问题 Extra ChatGPT

How can I rollback a git repository to a specific commit?

My repo has 100 commits in it right now. I need to rollback the repository to commit 80, and remove all the subsequent ones.

Why?

This repo is supposed to be for merging from miscellaneous users. A bunch of merges went in as commits from me, due to excessive editing. That was due to a mislabeling of my remote branches, where 3 developers were labeled as each other. I need to reset to that point, and then pull forwards.

I wanted to rebase, as in this example: How can I remove a commit on GitHub?

However, git wants me to do a lot of conflict management. Is there a simpler way?


S
Seldom 'Where's Monica' Needy
git reset --hard <old-commit-id>
git push -f <remote-name> <branch-name>

Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history


people, do git push -f origin branch. I just had a bad time, because I missed that.
BEWARE of this ! you will lose all your commits locally, and if you push there will be no way back
That is not true, you can get the old commits using git reflog
you may need to do git push origin HEAD --force instead of git push -f origin branch as that was giving me an error: src refspec branch does not match any. error
Yep, thank you guys. I managed to do what I wanted. I first created a separate branch from the one ahead of the commit I wanted to rollback. Cloned it locally. Then applied your recommendations on that copy, this way, I did not compromised my main branch...
N
Nick Res

To undo the most recent commit I do this:

First:

git log

get the very latest SHA id to undo.

git revert SHA

That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.

This is good for an immediate redo of something you just committed, which I find is more often the case for me.

As Mike metioned, you can also do this:

git revert HEAD

git revert HEAD will revert the last commit without having to look up the hash.
This is a solution if you don't want to delete commits at all. However, in this case, all the garbage commits will still be there, and allow a cloner to reset or checkout to them. If you want to prevent that, you'll HAVE to force-push.
Note: These reverts can stack, meaning if you accidentally revert you can revert the revert with another revert.
The accepted answer is fantastic, but I think this answer needs to be seen more b/c there is a difference between RESET and REVERT that may be very important for people if they are working in collaborative environments. Here is a good explanation: stackoverflow.com/questions/8358035/…
C
Captain Man

Another way:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree, I right-clicked on the and selected "Reset BRANCHNAME to this commit".

Then navigate to your repository's local directory and run this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f -- tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME 

This will erase all commits after the current one in your local repository but only for that one branch.


FYI "REPOSITORY_NAME" will be something like https:/me@github.com/me/repo_name.git
It'd be more helpful if you would break down that command and explain it.
I
Ion Lesan

Most suggestions are assuming that you need to somehow destroy the last 20 commits, which is why it means "rewriting history", but you don't have to.

Just create a new branch from the commit #80 and work on that branch going forward. The other 20 commits will stay on the old orphaned branch.

If you absolutely want your new branch to have the same name, remember that branch are basically just labels. Just rename your old branch to something else, then create the new branch at commit #80 with the name you want.


I'm guessing this also problematically messes with everyone's local versions of the history of the branch of interest.
if such big rollback is required, then certainly everyone's local versions need to pull the latest (prepared by the first person) again . It would be safer in case you want to "rollback the rollback".
R
Richard Nader

When doing branch updates from master, I notice that I sometimes over-click, and cause the branch to merge into the master, too. Found a way to undo that.

If your last commit was a merge, a little more love is needed:

git revert -m 1 HEAD


Worked nicely for me when I was reverting the commit on remote repo.
J
Jianwu Chen

In github, the easy way is to delete the remote branch in the github UI, under branches tab. You have to make sure remove following settings to make the branch deletable:

Not a default branch No opening poll requests. The branch is not protected.

Now recreate it in your local repository to point to the previous commit point. and add it back to remote repo.

git checkout -b master 734c2b9b   # replace with your commit point

Then push the local branch to remote

git push -u origin master

Add back the default branch and branch protection, etc.