for those have tried all the answers but still not succeeded, you can try to use my powershell script
$branchName = 'RemoteBranchNameToClone'git fetch origin "${branchName}:${branchName}"git checkout $branchName# Explicitly setup up the upstream to the right remote branchgit branch --set-upstream-to=$branchName $branchName
Reason analysis: why the common command sequence is not working?
git config --get remote.origin.fetch
will return: +refs/heads/master:refs/remotes/origin/master
it is stored in the .git/config file
[remote "origin"] url = https://abc-swt.visualstudio.com/abc/_git/efg fetch = +refs/heads/master:refs/remotes/origin/master
which happens when clone the repo with the --single-branchor when do shadow clone:
git clone --depth 100
but does not explicitly set the--no-single-branch
switch,because the--depth
implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches.If you want to clone submodules shallowly, also pass --shallow-submodules.
To save your life, do shadow clone like this:
git clone --no-single-branch --shallow-submodules --depth=1000 <repo_url>
orgit clone --no-single-branch --shallow-submodules --shallow-since=2023-08-08 <repo_url>
we need to change it with:git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
then we can do it simply:
git fetchgit checkout branchName