[GitHub]How to undo the most recent commits in Git?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[GitHub]How to undo the most recent commits in Git?相关的知识,希望对你有一定的参考价值。
原文地址:https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git/927386#927386
git reset最详尽介绍:https://git-scm.com/docs/git-reset
总结一下:
git reset --hard HEAD~1
git push --force
值得注意的是,这类操作比较比较危险,例如:在你的commit
之后别人又提交了新的commit
,那在你强制推送之后,那位仁兄的commit
也跟着一起消失了。
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.
Say you have this, where C is your HEAD and (F) is the state of your files.
(F)
A-B-C
↑
master
You want to nuke commit C and never see it again. 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.
Ah, but suppose 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
You can 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!
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.
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 git reflog
and you‘ll see a list of (partial) commit shas 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.
以上是关于[GitHub]How to undo the most recent commits in Git?的主要内容,如果未能解决你的问题,请参考以下文章
How to solve the problem that Github can't visit in China?
How to fix use the cURL to connect to GitHub with a 443 HTTPS error All In One
The best manual of how to use "The easiest Xdebug" addon for Firefox
Xcode iOS app build for latest iPhone (How to make Xcode to deploy to the latest iOS without update
[Git] Undo a commit that has already been pushed to the remote repository