Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git doesn't clone all branches on subsequent clones?

Tags:

git

branch

clone

I have some problems with Git using cloned repositories and branches and it's somehow not possible for me to find an answer to this. Let me describe: we have a bare master Git repository here we all pull from and push to, located on a local linux machine and reachable with ssh. I made a clone of this to my usb thumb drive like this:

git clone ssh://adahl@gollum//net/repos/netcube/patches.git

This gives me of course a local clone with a working copy on my thumb drive. I cd to this and see some branches in this clone then:

cd patches
git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/stable

So far so good, however if I clone the repository on my thumb drive another time to my notebook the stable branch is lost. See:

cd ..
git clone patches patches2

cd patches2

git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

I tried several options when cloning or a git fetch after cloning, nothing brings the stable branch to the patches2 repository. I assume I have a lack of understandig git here and simply use it the wrong way. Could someone please point me to my error in usage and/or understanding?

like image 225
LeSpocky Avatar asked Apr 06 '11 08:04

LeSpocky


People also ask

Does git clone clones all branches?

git clone downloads all remote branches but still considers them "remote", even though the files are located in your new repository. There's one exception to this, which is that the cloning process creates a local branch called "master" from the remote branch called "master".

Why git branch is not showing all branches?

This can happen if your repo has 0 commits. If you make a commit, your current branch will appear when you do: git branch . Show activity on this post. Sounds like you have a permission issue.

Does git clone only one branch?

There are two ways to clone a specific branch. You can either: Clone the repository, fetch all branches, and checkout to a specific branch immediately. Clone the repository and fetch only a single branch.


Video Answer


1 Answers

In addition to @ThiefMaster:

I like to

git clone --mirror

or

git push --mirror 

to update all (local & remote) branch refs and tags

Additional info As noted, --mirror will really replicate the repo as is, thus overwritany changes in the destination. Branches that do not exist in the source will get pruned unconditionally.

Essentially, it is like working with a remote and doing 'git remote update --prune', the difference being that the branches affected can be local branches as well as 'remote' refs[1]

@LeSpocky (and others?)

Now if changes disappear, they will never generate merge problems, so that's easy.

--mirror is named after the real-life concept, so it was designed to pave over any differences in the target. If the target is non-bare, and you had local changes committed, you can always get them back via the reflog of the target's local branch (git log -g, git reflog).

As a general safety measure you could have a hook to 'git stash save' in the target.

Keep in mind though, that --mirror was designed to, well, mirror and this question was in fact on how to replicate all branches to a bare remote. :)

[1] (the refs are there, but the remote definitions don't get copied; if you want that, do a manual copy from .git/config to .git/config on the push destination)

like image 183
sehe Avatar answered Sep 22 '22 21:09

sehe