Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the 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 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.

like image 327
sreginogemoh Avatar asked Feb 24 '16 02:02

sreginogemoh


People also ask

Can I undo fetch?

It is difficult1 to "undo" a git fetch , but there is never2 any reason to need to undo a git fetch .

How do I undo a git pull?

There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit. We're working on a project called ck-git. A collaborator has just pushed a commit to the remote version of the project that is stored on GitHub.

What does git fetch prune do?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

Are git fetch and git pull the same?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.


2 Answers

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> 
like image 184
qzb Avatar answered Sep 24 '22 18:09

qzb


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).

like image 25
torek Avatar answered Sep 22 '22 18:09

torek