如何撤消“git fetch”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35591887/
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
How to undo 'git fetch'
提问by sreginogemoh
I just added additional remote A
to my repo B
and then run git fetch A
. How can I undo the fetch? If I just remove remote A
: git remote remove A1
would it undo fetch?
我只是remote A
在 my 中添加了额外的repo B
然后运行git fetch A
. 如何撤消提取?如果我只是删除remote A
:git remote remove A1
它会撤消提取吗?
UPDATE:
更新:
$ git remote add A path/to/A
$ git fetch A
The above are commands I run so in result I got all branches fetched to my repo B
however I just need one specific branch from repo A
and I need it to go in specific folder on repo B
but that is another story Merge code between two dfferent git repositories.
以上是我运行的命令,所以结果我将所有分支都提取到了我的,repo B
但是我只需要一个特定的分支repo A
,我需要它进入特定的文件夹,repo B
但这是另一个故事在两个不同的 git 存储库之间合并代码。
回答by qzb
You can undo allfetches from remote A
simply by removing this remote:
您可以通过删除此遥控器来撤消所有从遥控器获取的内容A
:
git remote remove A
Now just add it again and fetch only single branch:
现在只需再次添加它并只获取单个分支:
git remote add A <path/to/repository>
git fetch A <name of branch>
回答by torek
It is difficult1to "undo" a git fetch
, but there is never2any reason to needto undo a git fetch
.
“撤消” a很难1git fetch
,但从来没有2任何理由需要撤消 a git fetch
。
Remember, what git fetch
does is call up the remote, get a list of branch-name to SHA-1 mappings, bring over commits and other objects you need in order to store those in your repository, and then update your remote-tracking branches so that they point to the remote's current (as of the time you just now phoned it up) branch tips. This has no effect on any of your work-tree files, and if you run git fetch
again tomorrow, any work done today can be skipped tomorrow. If you domanage to undo the fetch
, the one run tomorrow will have to re-do the work done today, so this is a net loss: you just spent some effort so that your git will have to bring more code over the network tomorrow.
请记住,什么git fetch
是调用远程,获取分支名称到 SHA-1 映射的列表,带来提交和其他您需要的对象以将它们存储在您的存储库中,然后更新您的远程跟踪分支,以便它们指向遥控器的当前(截至您刚刚打电话时)分支提示。这对您的任何工作树文件都没有影响,如果您git fetch
明天再次运行,明天可以跳过今天完成的任何工作。如果您确实设法撤消了fetch
,那么明天运行的将不得不重新执行今天完成的工作,所以这是一个净损失:您只是花费了一些精力,因此您的 git 明天将不得不通过网络带来更多代码。
That said, time for footnotes. :-)
也就是说,是时候做脚注了。:-)
1It's not thatdifficult if you have remote reflogs (which you probably do): just use the remote reflogs to find remote-tracking branches updated in the most recent fetch—this same information may also still be available in the FETCH_HEAD
file—and then use git update-ref
to point those references back to their previous reflog entries. But this will still leave the fetched objects in your repository, so to really clear them out, you must then also delete the intermediate reflog entries, and then run git gc --prune=now
, which requires a lot of care and will discard allunreferenced objects, not just ones brought over by the most recent fetch
.
1如果您有远程引用日志(您可能会这样做),这并不难:只需使用远程引用日志查找在最近一次获取中更新的远程跟踪分支——同样的信息也可能在FETCH_HEAD
文件中可用——然后使用git update-ref
将这些引用指向它们以前的 reflog 条目。但这仍然会将获取的对象留在您的存储库中,因此要真正清除它们,您还必须删除中间的 reflog 条目,然后运行git gc --prune=now
,这需要非常小心并且会丢弃所有未引用的对象,而不仅仅是带来的对象由最近的结束fetch
。
2I think one could argue that "running low on disk space" might be a reason to do this, especially if a large object was accidentally pushed to the remote and will be removed from the remote by the next fetch
. Working in a file system that is out of space is tricky in general, though, and I'm not sure I would want to do much here other than move the entire repository elsewhere (somewhere without the disk space issues).
2我认为有人可能会争辩说“磁盘空间不足”可能是这样做的一个原因,特别是如果一个大对象被意外推送到远程并且将在下一个fetch
. 但是,在空间不足的文件系统中工作通常很棘手,而且我不确定除了将整个存储库移动到其他地方(没有磁盘空间问题的地方)之外,我还想在这里做很多事情。
回答by The Godfather
I really liked answer from torek, but it doesn't provide you commands to "update reflogs", which is exactly what I was looking for. I found this in other answer reverse a git fetch, so I just leave it here to simplify searching for others.
我真的很喜欢torek 的回答,但它没有为您提供“更新引用日志”的命令,这正是我正在寻找的。我在其他答案中发现了这个反向 git fetch,所以我把它留在这里以简化对其他人的搜索。
Let's assume that you have your origin
remote and your local develop
branch is based on master
, but your local master
is behind origin/master
(but you don't see it yet, because you haven't fetched origin
yet).
让我们假设你有你的origin
遥控器,你的本地develop
分支基于master
,但你的本地master
在后面origin/master
(但你还没有看到它,因为你还没有获取origin
)。
So performing git fetch
will add new commits from origin/master
and tags to your local repo. In some cases you don't want to seethose new commits at all and they will reduce readability of your history.
因此,执行git fetch
会将新提交origin/master
和标签添加到您的本地存储库。在某些情况下,您根本不想看到这些新提交,它们会降低您的历史记录的可读性。
Having said that, you can "undo" git fetch
by doing:
话虽如此,您可以git fetch
通过执行以下操作来“撤消” :
git update-ref refs/remotes/origin/master refs/remotes/origin/master@{1}
All this does is changes the origin/master
pointer to the previous state. Maybe you'll have to play with {n}
a bit to get the exact state, but in general it may help.
所有这些都是将origin/master
指针更改为先前的状态。也许您需要{n}
稍微尝试一下才能获得确切的状态,但总的来说它可能会有所帮助。
回答by Olimon F.
You can delete whatever branches you accidentally fetched that you don't want to keep with the following command:
您可以使用以下命令删除您不小心获取的不想保留的任何分支:
git update-ref -d refs/remotes/YOUR_REMOTE_NAME/BRANCH_NAME_TO_DELETE
git update-ref -d refs/remotes/YOUR_REMOTE_NAME/BRANCH_NAME_TO_DELETE
To prevent them from getting fetched again, set your git remote to only fetch specified branches: git remote set-branches YOUR_REMOTE_NAME desired_branch1 [desired_branch2, desired_branch3, etc]
, e.g. git remote set-branches origin master
.
为了防止它们再次被提取,请将您的 git remote 设置为仅提取指定的分支:git remote set-branches YOUR_REMOTE_NAME desired_branch1 [desired_branch2, desired_branch3, etc]
,例如git remote set-branches origin master
.
The default behaviour of set-branches is to always replace the list. If you want to append, use the --add
option: git remote set-branches --add origin another_branch
set-branches 的默认行为是始终替换列表。如果要附加,请使用以下--add
选项:git remote set-branches --add origin another_branch