Skip to content

Instantly share code, notes, and snippets.

@BROWNPROTON
Last active June 16, 2018 03:49
Show Gist options
  • Save BROWNPROTON/2e6b5853ba21f4c10abe040bfb680730 to your computer and use it in GitHub Desktop.
Save BROWNPROTON/2e6b5853ba21f4c10abe040bfb680730 to your computer and use it in GitHub Desktop.
Cloning from a local repo will not work with git clone & git fetch: a lot of branches/tags will remain unfetched.
To get a clone with all branches and tags.
git clone --mirror git://example.com/myproject myproject-local-bare-repo.git
To get a clone with all branches and tags but also with a working copy:
git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master
Git usually (when not specified) fetches all branches and/or tags (refs, see: git ls-refs) from one or more other repositories along with the objects necessary to complete their histories. In other words it fetches the objects which are reachable by the objects that are already downloaded. See: What does git fetch really do?
Sometimes you may have branches/tags which aren't directly connected to the current one, so git pull --all/git fetch --all won't help in that case, but you can list them by:
git ls-remote -h -t origin
and fetch them manually by knowing the ref names.
So to fetch them all, try:
git fetch origin --depth=10000 $(git ls-remote -h -t origin)
The --depth=10000 parameter may help if you've shallowed repository.
Then check all your branches again:
git branch -avv
If above won't help, you need to add missing branches manually to the tracked list (as they got lost somehow):
$ git remote -v show origin
...
Remote branches:
master tracked
by git remote set-branches like:
git remote set-branches --add origin missing_branch
so it may appear under remotes/origin after fetch:
$ git remote -v show origin
...
Remote branches:
missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
* [new branch] missing_branch -> origin/missing_branch
Troubleshooting
If you still cannot get anything other than the master branch, check the followings:
Double check your remotes (git remote -v), e.g.
Validate that git config branch.master.remote is origin.
Check if origin points to the right URL via: git remote show origin (see this post).
Method 1:
git clone --mirror OLD_REPO_URL
cd new-cloned-project
mkdir .git
mv * .git
git config --local --bool core.bare false
git reset --hard HEAD
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
Method 2:
git config --global alias.clone-branches '! git branch -a | sed -n "/\/HEAD /d; /\/master$/d; /remotes/p;" | xargs -L1 git checkout -t'
git clone OLD_REPO_URL
cd new-cloned-project
git clone-branches
git remote add newrepo NEW_REPO_URL
git push --all newrepo
git push --tags newrepo
@BROWNPROTON
Copy link
Author

Excerpts from this Stackoverflow thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment