开发者

How can I download only the necessary parts of a remote project in Git?

开发者 https://www.devze.com 2023-01-25 01:01 出处:网络
If you are working on a large remote repository and you want to restrict the download to开发者_如何转开发 the few branches you are working on, how do you configure the git-clone command, assuming that

If you are working on a large remote repository and you want to restrict the download to开发者_如何转开发 the few branches you are working on, how do you configure the git-clone command, assuming that it is the right command in this case?


Answer to the real question

Local clones with git don't generally take up a ton of extra space, because git will use hard links to share the object files. This is hard to notice - if you run du on each repo, you'll get the full size, but if you run it on the two together, you should see the savings. I'm going to assume you've already for some reason decided that this isn't good enough. Perhaps you're on a filesystem that doesn't support hardlinks, or the clones are on separate drives or something... who knows.

In any case, if you're looking to create a lightweight clone, saving some space, why not save all the space? There's a lovely script in git's contrib directory called git-new-workdir (the link is to the current version in git.git). It creates a new work directory from a repo, with the .git directory essentially all shared via symlinks - pretty much the only thing that isn't is HEAD. Drop the script somewhere in your path, and you'll be able to run it like a normal git command:

git new-workdir <original-repo> <new-workdir-path>

Voila! You now have two work trees, with a shared .git directory, so the only extra space you're taking up is the work tree files. No way around that if you want to be able to work!

The one thing you must be careful about is checking out the same branch in both repos. If you then commit to that branch in one repo, the other one will become out of sync - the work tree and index won't match the commit that the branch is now at. Otherwise, you can happily work away in both repositories!

Original answer

Let me first state that there is essentially no chance you want to do this. I'm serious. It's barely going to save you any disk space, while repositories with hard-linked objects (which is the default! you don't even have to do anything to get that!) will save you a ton.

In virtually every case, branches share most of their history. The potential for saving space is only in the small recent part in which they've diverged. Look at git log branchA..branchB. Those commits are the ones whose objects you will avoid copying. Are there any enormous binary files in there? Any 1000-line diffs? No? Then don't bother with this. It's not going to help you.

Still reading? Okay, well, I don't think git-clone lets you mess with the refspec (with the exception of --mirror, but that's obviously not what we're after here). If it's really important to do this, you could manage it by creating an empty repository and pulling, then carefully doing the rest of the setup the clone would've done:

mkdir foo && cd foo && git init
git remote add origin <url>
# set up a refspec to get the branch(es) you want
git config remote.origin.fetch "+refs/heads/foo:refs/remotes/origin/foo ..."
git fetch origin

You've still got some config missing - in particular, you have a local master branch which isn't tracking anything.

This is a pretty strange setup, not grabbing all the branches from origin, but I suppose it should work. Of course, like I said in my comment, you may not be saving yourself a whole lot of trouble. Fetching other remote branches doesn't mean you have to create corresponding local branches, and unless those excluded branches diverge extremely from the ones you've grabbed (i.e. contain lots of unique content), you're not saving much bandwidth or disk space.


If you're working on two branches in two separate directories, then you can set up one to be a clone of the other:

git clone http://remote/repo.git branch-a
git clone branch-a branch-b

Then, fix the origin remote in branch-b:

cd branch-b
git remote add origin http://remote/repo.git

(you may have to remove the previous origin first). This way, the local repository information will be shared by hard links between the two directories, saving you some space compared to making two separate clones of the remote.

Or, go buy a 1 TB drive, they're cheap.

0

精彩评论

暂无评论...
验证码 换一张
取 消