撤消 git pull,如何将 repos 恢复到旧状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1223354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:50:09  来源:igfitidea点击:

Undo git pull, how to bring repos to old state

gitversion-controlgit-merge

提问by seg.server.fault

Is there any way to revert or undo git pull so that my source/repos will come to old state that was before doing git pull ? I want to do this because it merged some files which I didn't want to do so, but only merge other remaining files. So, I want to get those files back, is that possible?

有什么方法可以恢复或撤消 git pull 以便我的源代码/存储库回到执行 git pull 之前的旧状态?我想这样做是因为它合并了一些我不想这样做的文件,而只合并了其他剩余的文件。所以,我想取回那些文件,这可能吗?

EDIT: I want to undo git merge for clarification. After seeing some answers, I did this

编辑:我想撤消 git merge 以进行澄清。看到一些答案后,我做了这个

git reflog
bb3139b... HEAD@{0}: pull : Fast forward
01b34fa... HEAD@{1}: clone: from ...name...

Now, what should I do ? Doing git reset --hardis OK ? I don't want to screw it again, so asking for detailed steps ?

现在,我该怎么办?这样做好git reset --hard吗?不想再搞砸了,求详细步骤?

回答by jkp

Running git pullperforms the following tasks, in order:

运行git pull按顺序执行以下任务:

  1. git fetch
  2. git merge
  1. git fetch
  2. git merge

The merge step combines branches that have been setup to be merged in your config. You want to undo the mergestep, but probably not the fetch(doesn't make a lot of sense and shouldn't be necessary).

合并步骤合并已设置为合并到您的配置中的分支。您想撤消合并步骤,但可能不是提取(没有多大意义,也没有必要)。

To undo the merge, use git reset --hardto reset the local repository to a previous state; use git-reflogto find the SHA-1 of the previous state and then reset to it.

要撤消合并,请使用git reset --hard将本地存储库重置为以前的状态;使用git-reflog查找先前状态的 SHA-1,然后重置为它。

Warning

警告

The commands listed in this section remove all uncommitted changes, potentially leading to a loss of work:

本节中列出的命令会删除所有未提交的更改,这可能会导致工作丢失:

git reset --hard

Alternatively, reset to a particular point in time, such as:

或者,重置到特定时间点,例如:

git reset --hard master@{"10 minutes ago"}

回答by Jeffrey Sun

Same as jkp's answer, but here's the full command:

与 jkp 的答案相同,但这是完整的命令:

git reset --hard a0d3fe6

where a0d3fe6 is found by doing

通过执行找到 a0d3fe6 的位置

git reflog

and looking at the point at which you want to undo to.

并查看您要撤消到的点。

回答by Martin G

A more modern way to undo a merge is:

撤消合并的更现代方法是:

git merge --abort

And the slightly older way:

而稍微旧的方式:

git reset --merge

The old-school way described in previous answers (warning: will discard all your local changes):

先前答案中描述的老派方式(警告:将丢弃所有本地更改):

git reset --hard

But actually, it is worth noticing that git merge --abortis only equivalent to git reset --mergegiven that MERGE_HEADis present. This can be read in the git help for merge command.

但实际上,值得注意的git merge --abort是,仅相当于给git reset --mergeMERGE_HEAD存在。这可以在合并命令的 git 帮助中阅读。

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --mergebut not necessarily with git merge --abort, so they are not only old and new syntax for the same thing. This is why i find git reset --mergeto be much more useful in everyday work.

合并失败后,如果没有MERGE_HEAD,失败的合并可以用 撤消,git reset --merge但不一定用git merge --abort因此它们不仅是同一事物的新旧语法。这就是为什么我发现git reset --merge它在日常工作中更有用。

回答by Ezequiel García

it works first use: git reflog

它首先使用: git reflog

find your SHA of your previus state and make (HEAD@{1} is an example)

找到您以前状态的 SHA 并制作(HEAD@{1} 是一个示例)

git reset --hard HEAD@{1}

回答by Samuel Carrijo

If you have gitk (try running "gitk --all from your git command line"), it's simple. Just run it, select the commit you want to rollback to (right-click), and select "Reset master branch to here". If you have no uncommited changes, chose the "hard" option.

如果您有 gitk(尝试从您的 git 命令行运行“gitk --all”),这很简单。只需运行它,选择要回滚到的提交(右键单击),然后选择“将主分支重置到此处”。如果您没有未提交的更改,请选择“硬”选项。

回答by Sazzad Hissain Khan

Suppose $COMMITwas the last commit id before you performed git pull. What you need to undo the last pull is

假设$COMMIT是您执行之前的最后一次提交 ID git pull。您需要撤消最后一次拉动的是

git reset --hard $COMMIT

git reset --hard $COMMIT

.

.

Bonus:

奖金:

In speaking of pull, I would like to share an interesting trick,

说到拉,我想分享一个有趣的技巧,

git pull --rebase

git pull --rebase

This above command is the most useful command in my git life which saved a lots of time.

上面这个命令是我 git 生活中最有用的命令,它节省了很多时间。

Before pushing your newly commit to server, try this command and it will automatically sync latest server changes (with a fetch + merge) and will place your commit at the top in git log. No need to worry about manual pull/merge.

在将您的新提交推送到服务器之前,请尝试此命令,它会自动同步最新的服务器更改(使用 fetch + 合并)并将您的提交放在 git log 的顶部。无需担心手动拉/合并。

Find details at: http://gitolite.com/git-pull--rebase

详情请见:http: //gitolite.com/git-pull--rebase

回答by Manish Goswami

This is the easiest way to revert you pull changes.

这是恢复拉取更改的最简单方法。

** Warning **

Please backup of your changed files because it will delete the newly created files and folders.

请备份您更改的文件,因为它会删除新创建的文件和文件夹

git reset --hard 9573e3e0

Where 9573e3e0is your {Commit id}

9573e3e0你的 {Commit id}在哪里

回答by Orlando

you can do git reset --hard ORIG_HEAD

你可以做 git reset --hard ORIG_HEAD

since "pull" or "merge" set ORIG_HEAD to be the current state before doing those actions.

由于“拉”或“合并”在执行这些操作之前将 ORIG_HEAD 设置为当前状态。

回答by GolamMazid Sajib

git pulldo below operation.

git pull做下面的操作。

i. git fetch

ii. git merge

一世。 git fetch

ii. git merge

To undo pull do any operation:

要撤消拉动,请执行任何操作:

i. git reset --hard--- its revert all local change also

or

ii. git reset --hard master@{5.days.ago}(like 10.minutes.ago, 1.hours.ago, 1.days.ago..) to get local changes.

or

iii. git reset --hard commitid

一世。git reset --hard--- 它也恢复所有本地更改

或者

ii. git reset --hard master@{5.days.ago}(如10.minutes.ago, 1.hours.ago, 1.days.ago..)以获取本地更改。

或者

三、 git reset --hard commitid

Improvement:

改进:

Next time use git pull --rebaseinstead of git pull.. its sync server change by doing ( fetch & merge).

下次使用git pull --rebase而不是git pull.. 它的同步服务器通过执行 ( fetch & merge ) 更改。

回答by Davide

If there is a failed merge, which is the most common reason for wanting to undo a git pull, running git reset --mergedoes exactly what one would expect: keep the fetched files, but undo the merge that git pullattempted to merge. Then one can decide what to do without the clutter that git mergesometimes generates. And it does not need one to find the exact commit ID which --hardmentioned in every other answer requires.

如果合并失败,这是想要撤消 a 的最常见原因git pull,则运行git reset --merge完全符合人们的预期:保留获取的文件,但撤消git pull尝试合并的合并。然后,您可以决定做什么,而不会出现git merge有时会产生的混乱。并且不需要找到--hard其他所有答案中提到的确切提交 ID 。