'git reset --hard Head〜1'足以解开git pull吗? [英] Is 'git reset --hard Head~1' enough to undo a git pull?
问题描述
我是一个git新手,我试图理解撤消git的变化。假设我有两个分行
<1> master:
2:work
我在工作分部工作,我想从工作分支中的队友那里得到最新的变化,所以我应该给命令
git pull origin work
但是,我给了命令
git pull原点大师
它从主分支中获取了一堆提交。现在,我想撤消最后一次提取(假设没有本地未改变的更改),这个命令是否足够?
git reset --hard HEAD〜1
请问上述命令完成撤消git pull?有没有任何情况下,它可能无法正常工作?
编辑
阅读答案后,Do HEAD〜1和ORIG-HEAD引用相同的提交后拉/合并?
git reset 不占用 - head
选项,我想你的意思是 - hard
。其次,不是,仅仅是 git reset --head HEAD〜1
会使你的版本库进入前一次提交的状态。如果 git pull
导致了很多新的提交,这还不够。您需要这样做: $ git reset --hard ORIG_HEAD
它的作用是因为在合并前,rebase和其他潜在危险的操作 Git
设置了一个特殊的引用,称为 ORIG_HEAD
等于此操作之前的当前HEAD的 SHA1
。下一次执行 git pull
注意 SHA1
当前 HEAD
>和之后 git pull
看看你自己, ORIG_HEAD
指向以前的 HEAD
:
$ cat .git / ORIG_HEAD
I am a git newbie, I am trying to understand to undo changes in git. Say I have two branches
1: master
2: work
I am working in work branch and I want to pull the latest changes from my teammates in work branch, so I am supposed to give the command
git pull origin work
But instead, I gave the command
git pull origin master
It fetches a bunch of commits from master branch.Now, I want to undo the last pull(assuming no local uncommited changes), will this command be sufficient
git reset --hard HEAD~1
Will the above command accomplish undoing git pull? Is there any scenario where it might not work?
Edit
After reading the answers, Do HEAD~1 and ORIG-HEAD refer to the same commit after a pull/merge?
First of all, git reset
does not take --head
option, I think you mean --hard
. Second, no, it's not enough as git reset --head HEAD~1
will take your repository to the state of the previous commit. If git pull
resulted in many new commits, it would not be enough. You need to do:
$ git reset --hard ORIG_HEAD
It works because before doing merge, rebase and other potentially dangerous operations Git
sets a special reference called ORIG_HEAD
equal to the SHA1
of current HEAD before this operation. Next time before doing git pull
note SHA1
of the current HEAD
and after git pull
see yourself that ORIG_HEAD
points the previous HEAD
:
$ cat .git/ORIG_HEAD
这篇关于'git reset --hard Head〜1'足以解开git pull吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!