Cloning the diff files from 2 branches


I am wondering if it is possible to only clone the files that are different from two different branches (say branch 2 has an additional of 30 files compared to branch 1 and you only wish to clone those files using git)


Git does not work with files, at this level. Git works with commits. You clone a repository by copying commits, not files.

It’s true that commits contain files, but:

  • every commit contains a full snapshot of every file (plus metadata)
  • though the files are in a special Git-ized format where they’re de-duplicated within and across commits

Git is smart enough that when you clone a repository and pick up new commits, you already only get the new files. This smartness has certain limitations: in particular, if you make a shallow or partial clone, it becomes less effective.

In other words, just make a regular clone and you’ll likely get exactly what you want. If you fiddle around too much with shallow and/or the fancy new partial clone, you’ll actually make things worse. But if regular clones are too slow, try out the shallow and/or partial clone features to see if they help.

(If you’re making a lot of clones, consider using --reference. Note that --reference tries hard to share the object databases across the various clones by default, which means that the clone(s) you use as the reference clones must be especially stable. If they’re not so stable, add --dissociate as well, so that the new clone works by copying instead of sharing.)

Answered By – torek

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published