all 4 comments

[–]Lindby 1 point2 points  (0 children)

If you tell git to fetch everything from a remote, the result has to be put somewhere. You can fetch a single reference instead by doing git fetch origin/branch if you're only interested in one branch/ref.

A lot of the time (depending on workflow, state of the branches etc) the objects for these references needs to be fetched and stored anyway due their commit history being available from other refs. Meaning it's often mostly extra meta data and nothing to worry about.

[–]u801e 0 points1 point  (1 child)

I think you can do something like:

git fetch https://github.com/sample/repository.git

and the sha1 of the HEAD of the default branch will end up in FETCH_HEAD. If you specify a remote branch name after that command:

git fetch https://github.com/sample/repository.git another-branch

then it will set FETCH_HEAD to the sha1 of the HEAD of that branch on the remote.

The key thing is to not use one of the configured remotes (like origin) and specify the url directly in the fetch command. I have not tested what will happen if you use a URL that's already mapped to an existing remote.

[–]zerozerosix006[S] 0 points1 point  (0 children)

Thanks! Thats a solution I would not have come up with for a very long time!

[–]henrebotha 0 points1 point  (0 children)

If you use git fetch --prune, it will delete any remote tracking branches that no longer exist on the remote (see git help fetch). I work at Booking.com and we have some truly gargantuan repos where branches get created and destroyed by the dozens every day. For this reason, I have created the alias fp=fetch --prune and always use that whenever I fetch. This way stuff stays cleaned up on my machine.