git/list[1] front-page[2] threads[3] people[4] search[5] about
 

git-clone --single-branch clones objects outside of branch

From
Chris Jerdonek <chris.jerdonek@gmail.com>
Date
Jan 26, 2020, 12:39 UTC
Message-ID
<CAOTb1wfoMf338VPRYcjAv+EaMYQxKJ=kP6-qMykG-LfxPrWuag@mail.gmail.com>
Hi,

I'm reporting some git-clone behavior regarding --single-branch that I found unexpected after reading the docs. I'm using git 2.25.0.

The git-clone docs for --single-branch say:
> Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at.

(from: https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---no-single-branch )

However, when I attempted this with a local repo, I found that objects located only in branches other than the branch I specified are also cloned. Also, this is true even if the remote repo has only loose objects (i.e. no pack files). So it doesn't appear to be doing this only to avoid creating new files.

In contrast, git-fetch behaves as expected (including locally). git-fetch appears to fetch only objects in the given branch, when a branch is specified.

Below are some commands to assist with reproducing this situation (but you will need to update the path in the `git-remote add` invocation below). At the least, it seems like the docs should clarify the behavior. (The Python commands were for when I was doing some experiments with pack files.)

mkdir repo1 cd repo1 git init python -c "print('a\n' + 10 * 'x\n')" > a.txt git add a.txt git commit -m "add a" # Get object id to check existence with `git cat-file -t` below. git hash-object a.txt

git checkout -b dev python -c "print('b\n' + 10 * 'x\n')" > b.txt git add b.txt git commit -m "add b" # Get object id to check existence with `git cat-file -t` below. git hash-object b.txt

git checkout master

cd .. mkdir repo2 cd repo2 git init git remote add other file:///<path-to-repo1> git fetch other master

cd .. git clone --branch master --single-branch repo1 repo3

Thanks, --Chris

Next: Jeff King
Message 1 of 6 in “git-clone --single-branch clones objects outside of branch”
  1. Chris JerdonekJan 26, 2020
  2. Jeff KingJan 27, 2020
  3. Chris JerdonekJan 27, 2020
  4. Jeff KingJan 28, 2020
  5. Chris JerdonekJan 29, 2020
  6. Jeff KingJan 29, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.