0

as in the title, I’m using git sparse-checkout, and I’m missing commits between the last git tag and the last commit I’m on. It creates a problem that I will try to describe it in a few points below:

  1. I use git sparse checkout to get only a small part of my repository
  2. I use git fetch origin --tags to fetch all tags to my local repository
  3. I'm trying to get the data required for my script - branches, commits, and tags associated with the commit. Below powershell script I am using to get the data:
& git tag | ForEach-Object {
        $commit = (git rev-list -n 1 $_)
        [PSCustomObject]@{
            tag    = $_
            commit = $commit
            branch = (git branch --contains $commit)
        }
} | ForEach-Object { Write-Host $_ }

Everything looks fine after running this script, except for the last tag, for my last tag I did not receive any linked branches. However, I should receive at least 1 branch (the one I am currently on) Script result:

@{tag=1; commit=0000000000000000000000000000000000000001; branch=System.Object[]}
@{tag=2; commit=0000000000000000000000000000000000000002; branch=System.Object[]}
@{tag=3; commit=0000000000000000000000000000000000000003; branch=}

0000000000000000000000000000000000000001- represents the commit where the tag was created, the same rule for the other 2 tags. Currently I am on commit 0000000000000000000000000000000000000004, which was created from commit 0000000000000000000000000000000000000003 - so the tag, should be the same (tag=3) for both these commits.

The problem only occure with git sparse-checkout, when I clone the whole repo with git clone develop, I am getting all data needed:

Data when git clone were used

@{tag=1; commit=0000000000000000000000000000000000000001; branch=System.Object[]}
@{tag=2; commit=0000000000000000000000000000000000000002; branch=System.Object[]}
@{tag=3; commit=0000000000000000000000000000000000000003; branch=System.Object[]}

2nd example:

Command and result after regular git clone: git branch --contains 0000000000000000000000000000000000000003 returns * develop

Command and result after git sparse: git branch --contains 0000000000000000000000000000000000000003 returns <empty>

Do you have any idea, why I am not getting any branches? What is the diffrence between regular clone and sparse? Any idea how to fix the difference between sparse-checkout and clone?

  • 1
    `git fetch origin --tags` does not fetch and create local branches. `git branch --contains $commit` lists only local branches. You could try `git branch -a --contains $commit`. If it does not print any branch related with the expected branch, you could run `git fetch origin +refs/heads/*:refs/remotes/origin/*` to fetch and create remote tracking branches first. – ElpieKay Mar 07 '23 at 01:54
  • `git branch -a --contains $commit` return null value as well. I run the `git fetch origin +refs/heads/*:refs/remotes/origin/*` and the strange thing is that I download all tags, without the last one `tag=3` - any idea why my last tag is missing? – Sebastian Błaszczyk Mar 07 '23 at 07:05
  • 1
    A tag does not have to be attached to a commit contained by a branch. For example, you can create a detached HEAD (by `git checkout somecommit`) and make a new commit. The new commit does not belong to any branch. You could create a tag from the commit and push the tag. This way, the tag points at a commit contained by no branch. – ElpieKay Mar 07 '23 at 07:12
  • Additionally, when I run `git ls-remote --tags origin` my tag=3 is displayed with the appropriate commit. – Sebastian Błaszczyk Mar 07 '23 at 07:13
  • ok, but in this case, why I am getting branches when I use regular `git clone`? In this case in both cases(sparse-checkout & git clone) the results should be empte, but it's only empty when I use sparse-checkout. – Sebastian Błaszczyk Mar 07 '23 at 12:18
  • I don't think it's caused by sparse-checkout. Sparse-checkout affects the checked-out files only. The branches, tags and commits are metadata stored in the repository's database. When you use `git clone`, by default all branches and tags are fetched. However, in your sparse-checkout procedure, by some unknown step the repository does not get some branches. The branch that contains the tag is not fetched. But it's strange the tag is missing after you fetch all branches in your first comment. – ElpieKay Mar 07 '23 at 13:15

0 Answers0