撤消 git pull

发布于 2024-10-12 05:21:36 字数 260 浏览 1 评论 0原文

我见过的撤消 git pull 的问题与我的略有不同。

这就是我所做的:

目录 A 中有一个项目(不是存储库)。我在其中初始化了一个存储库,添加了文件,但没有提交任何内容。然后我从存储库 B 中提取数据,它覆盖了我的一堆暂存文件。

我的印象是我可以使用 git reset --hard 来撤消合并。当然,这只是检查了我刚刚拉入的提交的 HEAD。

我应该在进行拉入之前分支并提交一些东西,事后看来很好。有什么方法可以恢复我未暂存的旧文件吗?

The questions I've seen for undoing a git pull a slightly different to mine.

This is what I've done:

There is a project in directory A (not a repo). I initialized a repository in it, added the files, but did not commit anything. Then I pulled from repository B, which overwrote a bunch of my staged files.

I was under the impression I could use git reset --hard to undo the merge. Of course that just checked out the HEAD of the commits I had just pulled in.

I should have branched and commited something before I did this pull, hindsight is nice. Is there some way I can get my old unstaged files back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扛刀软妹 2024-10-19 05:21:36

git pullgit fetch + git merge 相同。这是合并步骤覆盖了您的更改。要恢复到合并之前的状态,请使用 git log 查找最新提交,然后使用 git reset --hard 1234abcd ,其中 1234abcd 是所需提交的哈希值。

请注意,重置后 git pull 将再次合并更改。要永久恢复更改,请使用 git revert ,这将创建一个额外的提交来反转更改。

A git pull is the same as git fetch + git merge. It is the merge step that overwrote your changes. To revert to the state before the merge, use git log to find your latest commit and then use git reset --hard 1234abcd where 1234abcd is the hash of the desired commit.

Note that after the reset git pull will merge the changes again. To revert the changes for good, use git revert which will create an additional commit reversing the changes.

九局 2024-10-19 05:21:36

似乎这里有恢复暂存文件的答案:

恢复添加执行 git reset --hard HEAD 后的文件^

It seems there is an answer to recovering staged files here:

Recovering added file after doing git reset --hard HEAD^

梦幻的味道 2024-10-19 05:21:36

要么只是重置到以前的 HEAD(即您在存储库中的提交,未从远程更新),要么使用 git reflog 。后者显示在存储库中执行的操作列表,git pull 命令应该是其中的一部分。只需将 git reset --hard X 恢复到该命令之前的状态即可。

或者只是从以前的 HEAD 分支(您可以删除/覆盖以前的分支),在那里进行更改并再次 git pull ...

Either just reset to the previous HEAD (i.e. the commit you had in your repository, that wasn't updated from the remote), or use git reflog. The latter displays a list of actions that were made in your repository, the git pull command should be part of that. Simply git reset --hard X to a state before that command.

Or just branch off the previous HEAD (you may delete/overwrite the previous branch), do your changes there and git pull again...

风追烟花雨 2024-10-19 05:21:36

我想你现在还不能完全康复。您能做的最好的事情就是使用 git status 检查哪些文件仍然完好无损。如果未提交的文件不在这里,它们就会丢失。

下次,请考虑在 git pull 之前使用 git stash,或者更好的是,使用 git fetch


如果您是高级 GIT 用户,并且文件确实很重要:

如果文件已暂存,您也许能够从对象存储库中提取它们。但这些都直接进入了 git 的内部,我认为你不应该尝试。

如果您确实想要,请阅读以下内容:

I think you cannot full recover now. The best you can do is git status to check which file(s) are still intact. If the uncommitted files are not here, they are lost.

Next time, consider using git stash before git pull, or, better yet, use git fetch.


IF YOU ARE A ADVANCED GIT USER, AND THE FILES ARE REALLY IMPORTANT:

If the files are staged, you may be able to extract them from the object repository. But these go right into git guts, which I don't think you should try.

If you really want, read these:

⒈起吃苦の倖褔 2024-10-19 05:21:36

更新时间:2011 年 1 月 17 日下午 6:53

重新阅读您的问题。我忽略了您是从项目 b 中提取数据,而不是将文件复制到项目中。不确定我的答案是否仍然适用于您的情况。我认为你最好的选择是遵循geoffreyd的答案并查看答案StackOverflow问题在执行 git reset --hard HEAD 后恢复添加的文件^

原始答案

只需执行 git checkout -- 即可。

这是我为了确认上述内容而运行的示例...

# Create a test directory and some test files
$ mkdir blah
$ echo "I've been overwritten" > test1.txt
$ cd blah
$ echo "I'm the real test1.txt" > test1.txt
$ git init .
Initialized empty Git repository in /Users/matthew/blah/.git/
$ git add .
$ cp ~/test1.txt .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   test1.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test1.txt
#
$ cat test1.txt
I've been overwritten
$ git checkout -- test1.txt
$ cat test1.txt
I'm the real test1.txt

当您执行 git status 时,它会告诉您使用 git checkout --来放弃工作中的更改目录。如果我正确地阅读你的问题,那就是你想做的。

Update January 17, 2011, 6:53 PM

Just reread your question. I overlooked that you were pulling from project b as opposed to copying files into your project. Not sure if my answer still holds given your scenario. I think your best bet is to follow geoffreyd's answer and checkout the answer to the StackOverflow question Recovering added file after doing git reset --hard HEAD^.

Original Answer

Just do a git checkout -- <file>.

Here's the example I ran to confirm the above...

# Create a test directory and some test files
$ mkdir blah
$ echo "I've been overwritten" > test1.txt
$ cd blah
$ echo "I'm the real test1.txt" > test1.txt
$ git init .
Initialized empty Git repository in /Users/matthew/blah/.git/
$ git add .
$ cp ~/test1.txt .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   test1.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test1.txt
#
$ cat test1.txt
I've been overwritten
$ git checkout -- test1.txt
$ cat test1.txt
I'm the real test1.txt

When you perform git status it tells you to use git checkout -- <file> to discard changes in the working directory. If I'm reading your question correctly, that's what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文