标签云

微信群

扫码加入我们

WeChat QR Code

How do I revert from my current state to a snapshot made on a certain commit?If I do git log, then I get the following output:$ git logcommit a867b4af366350be2e7c21b8de9cc6504678a61b`Author: Me <[email protected]>Date: Thu Nov 4 18:59:41 2010 -0400blah blah blah...commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4Author: Me <[email protected]>Date: Thu Nov 4 05:13:39 2010 -0400more blah blah blah...commit 0766c053c0ea2035e90f504928f8df3c9363b8bdAuthor: Me <[email protected]>Date: Thu Nov 4 00:55:06 2010 -0400And yet more blah blah...commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50Author: Me <[email protected]>Date: Wed Nov 3 23:56:08 2010 -0400Yep, more blah blah.How do revert to the commit from November 3, i.e. commit 0d1d7fc?


Related How to undo the last Git commit?.

2019年06月21日58分53秒

Here's a very clear and thorough post about undoing things in git, straight from Github.

2019年06月21日58分53秒

Related: Rollback to an old Git commit in a public repo. Note that that question adds a constraint that the repo is public.

2019年06月21日58分53秒

I love git, but the fact that there's 35 answers to something that should be incredibly simple exposes a huge issue with git. Or is it the docs?

2019年06月21日58分53秒

They could add a button functionality for that like SVN in history

2019年06月21日58分53秒

Rod's comment on git revert HEAD~3 as the best wat to revert back 3 commits is am important convention.

2019年06月21日58分53秒

Could you write the whole number? like: git reset --hard 0d1d7fc32e5a947fbd92ee598033d85bfc445a50

2019年06月20日58分53秒

MathiasMadsenStav Yes, you can of course specify commits by the full SHA1. I used abbreviated hashes to make the answer more readable, and you also tend to use them if you're typing out. If you're copying and pasting, by all means use the full hash. See Specifying Revisions in man git rev-parse for a full description of how you can name commits.

2019年06月21日58分53秒

To get back to current the command is 'git checkout master'

2019年06月21日58分53秒

You can use git revert --no-commit hash1 hash2 ... and after this just commit every single revert in one commit git commit -m "Message"

2019年06月21日58分53秒

I did that, but then I wasn't able to commit and push to the remote repository. I want a specific older commit to become HEAD...

2019年06月20日58分53秒

It means you have already pushed in the commits you wanna revert. It can create lot of problems for people who have checked out your code and working on it. Since they cannot apply your commit smoothly over theirs. In such case better do a git revert. If you are the only one using the repo. Do a git push -f (But think twice before doing that)

2019年06月20日58分53秒

Obligatory Warning: don't hard reset if you're sharing your branch with other people who have copies of the old commits, because using a hard reset like this will force them to have to resynchronize their work with the newly reset branch. The soft reset is safe though, as well as the last solutions in this answer.

2019年06月20日58分53秒

I just also want to point out that, alternatively for the soft reset solution, instead of doing a mixed reset first and a hard reset last, you can actually do the hard reset first, as follows: git reset --hard 56e05fc; git reset --soft HEAD{1}; git commit.

2019年06月21日58分53秒

nuton linus pauling himself, the creator of git, criticized it for being too complicated. He's on record saying that he was "shocked" git became so popular given its complexity

2019年06月21日58分53秒

If you really do want to have individual commits (instead of reverting everything with one big commit), then you can pass --no-edit instead of --no-commit, so that you don't have to edit a commit message for each reversion.

2019年06月21日58分53秒

If one of the commits between 0766c053..HEAD is a merge then there will be an error popping up (to do with no -m specified). This may help those encountering that: stackoverflow.com/questions/5970889/…

2019年06月21日58分53秒

To see the diffs before you commit use git diff --cached.

2019年06月21日58分53秒

$ git revert --no-commit 53742ae..HEAD returns fatal: empty commit set passed

2019年06月20日58分53秒

AlexG that's because you need to enter the hash one before the one that you want to go back to. In my case, hashes were like: 81bcc9e HEAD{0}; e475924 HEAD{1}, ... (from git reflog), and I wanted to undo what I did in 81bcc9e, then I had to do git revert e475924..HEAD

2019年06月21日58分53秒

Obligatory Warning: don't do this if you're sharing your branch with other people who have copies of the old commits, because using a hard reset like this will force them to have to resynchronize their work with the newly reset branch. For a solution that explains in detail how to safely revert commits without losing work with a hard reset, see this answer.

2019年06月20日58分53秒

I second Cupcake's warning... be very aware of the consequences.Note, however, that if your need really is to make those commits disappear from history forever, this reset+clean method will do it, and you'll need for force push your modified branches back to any and all remotes.

2019年06月21日58分53秒

git clean -f DANGER DANGER

2019年06月21日58分53秒

This sets the head of my local copy to the desired commit. But then I cannot push any changes because it is behind the remote. And if I pull from the remote it ends up back where it was at the latest commit on the remote branch.How do I completely obliterate (from everywhere) several commits both on my local copy that have been pushed?

2019年06月21日58分53秒

Ade .. You could use the git push -f flag.. But be careful, it will override the remote.. Be sure you know what you want to do.. .

2019年06月21日58分53秒

Excellent hint to git reflog, that's exactly what I needed

2019年06月21日58分53秒

Ouch! This all seems awfully complicated... isn't there a simple command that just takes you a step back in the process? Like going from version 1.1 in your project back to version 1.0? I'd expect something like: git stepback_one_commit or something....

2019年06月21日58分53秒

there is: git reset HEAD^ --hard`

2019年06月21日58分53秒

Kokodoko Yes, it is horribly complicated... and a perfect example of how little consideration experts have for people who are just starting out.Please refer to my answer here, and also to the book I recommend in it.Git is NOT something you can just pick up intuitively.And I can be absolutely certain CodeWizard didn't do so.

2019年06月21日58分53秒

This is soft and gently: risk free if you haven't pushed your work

2019年06月21日58分53秒

I tried 1/2 a dozen answers in this post until I got to this one .. all of those others, my git config kept giving me an error when trying to push.This answer worked.Thanks!

2019年06月21日58分53秒

One detail for me was that I lost the diffs .. which I wanted to keep to see what I had done in the commit that didn't work.So next time I would just save that data before issuing this reset command

2019年06月21日58分53秒

This was the only way for me to undo a bad merge, revert didn't work in that case.Thanks!

2019年06月21日58分53秒

This worked for me, but you need to be care because all the commit history after the hard reset are lost and this action is irreversible. You need to be sure what you doing.

2019年06月21日58分53秒

You could do a git revert HEAD~3 to remove the last 3 commits

2019年06月21日58分53秒

Rod - No, that's not right. That command will revert the commit that is the third grandparent of HEAD (not the last three commits).

2019年06月21日58分53秒

kflorence Ok thanks for the info. Would git revert -n master~3..master~1 work ? (As seen from kernel.org/pub/software/scm/git/docs/git-revert.html)

2019年06月21日58分53秒

Rod - That sounds right, sure is an ugly syntax though isn't it? I've always found checking out the commit I want to "revert" to and then committing that more intuitive.

2019年06月21日58分53秒

There's a much easier way to do this now than with a script like this, just use git revert --no-commit <start>..<end>, because git revert accepts a commit range in new (or all?) versions of Git. Note that the start of the range isn't included in the revert.

2019年06月21日58分53秒

About Alternative 1, one quick question: By doing so we don't loose in between commits right?

2019年06月21日58分53秒

in Alternative 2, dots stand for what in those commands?

2019年06月21日58分53秒

Bogac - the dots indicate a file path, in this case the current directory, so it's assumed you're running it from the root of your working copy.

2019年06月21日58分53秒

The warning is repeated several times in the answer, but could someone add why these are not the best way — compared, to something like git revert HEAD~2..HEAD from Cascabel's (Jefromi's) linked solution.I'm not seeing the problem.

2019年06月21日58分53秒

How is this answer any different than the myriad of others?

2019年06月21日58分53秒

The link is broken.

2019年06月21日58分53秒

That's unfortunate. I emailed the blogger - hopefully he still has it!

2019年06月21日58分53秒

Peter #brokenlink web.archive.org/web/20150411085146/http://lrotherfield.com/blog/…

2019年06月21日58分53秒

The push syntax is missing from most other suggestions for how to fix this. Worked great.

2019年06月21日58分53秒

didnt work, a git pull after this yields: error: Your local changes to the following files would be overwritten by merge:

2019年06月21日58分53秒

malhal That is because you've had uncommited changes. Stash/reset them and then it will work without that error.

2019年06月21日58分53秒

Obligatory Warning: don't do this if you're sharing your branch with other people who have copies of the old commits, because using a force push like this will force them to have to resynchronize their work. For a solution that explains in detail how to safely revert commits without losing work with a force push, see this answer.

2019年06月20日58分53秒

Sometimes this is what you want.Example: committed and pushed several commits to the wrong branch (branch A). After cherry-picking to branch B, I want these commits removed from branch A. I would not want to revert, since the revert would later get applied when branch A and B are merged together. Doing a reset --hard <commitId> in branch A followed by a force push removes these commits from the branch while preserving them in branch B. I can get away with this because I know nobody else is developing on branch A.

2019年06月21日58分53秒

Thanks! I couldn't figure out how to get the remote branch to match my local branch, Just needed to do a force push.

2019年06月21日58分53秒

Obligatory Warning: don't do this if you're sharing your branch with other people who have copies of the old commits, because using a hard reset like this will force them to have to resynchronize their work with the newly reset branch. For a solution that explains in detail how to safely revert commits without losing work with a hard reset, see this answer.

2019年06月20日58分53秒

Also, to be clear, git show HEAD is equivalent to just using git log HEAD -1.

2019年06月21日58分53秒

NOTE: New files not added in index are not stashed. You have too add them or manually delete them.

2019年06月21日58分53秒

Why oh why clearing stash? In addition to being a non-solution, this is actually harmful. Reading the very first sentence of the question immediately invalidates the stash solution (which could be useful ONLY to reset to the LAST commit).

2019年06月21日58分53秒