How to undo 'git fetch'
I just added additional remote A
to my repo B
and then run git fetch A
How can I undo fetch? If I just remove remote A
: git remote remove A1
would it undo fetch?
UPDATE:
git remote add A path/to/A
git fetch A
The above is 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
You can undo all fetches from remote A
simply by removing this remote:
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>
It is difficult1 to "undo" a git fetch
, but there is never2 any reason to need to undo 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 do manage 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.
That said, time for footnotes. :-)
1It's not that difficult 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 all unreferenced objects, not just ones brought over by the most recent 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).
It sounds like you want to reset your HEAD to a previous version/branch.
Just do git checkout [branch_hash_number]
上一篇: 如何在返回到先前的提交后返回到unpushed提交
下一篇: 如何撤消'git fetch'