Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clone a git repository and keep remotes?

Tags:

git

So I've created a git repository, set up some tags and remotes on my working copy, and then pushed the repository to my server with

git push --mirror my.remote.repository

Supposedly, pushing with git --mirror will also push the remotes that I've set up.

However, if I do

git clone my.remote.repository

I don't see the remotes that I created originally. Do I need special syntax to also clone the remotes? Does mirror not actually send remotes? Where did I go wrong?

Edit: Maybe my workflow is weird: Here's what I'm trying to accomplish. The repo is initially a clone of another project(A) that I don't control, where I've renamed the remote to 'upstream'. I've made a new 'master' branch, and intend to make changes to my working copy and deploy them. I will occasionally pull updates from the original repository (A) (switching to the original, unmodified branch and pulling from upstream). However, I'm working with other people, all of whom should be able to do this as well. The goal is to update the master branch with bug fixes from the initial repository(A) and commit the changes to the 'master' branch. Because of this, when a developer clones the repository, they should be inheriting the upstream branch so that they can actually do this. In short, I'm trying to use an 'upstream' branch to pull in updates from a different project. I haven't used a submodule to do this since the 'upstream' branch actually is responsible for 95% of the files in the repository and the entire project makes no sense without the files from upstream.

like image 654
Zxaos Avatar asked Oct 11 '11 19:10

Zxaos


People also ask

Does git clone add remote?

When you clone a repository with git clone , it automatically creates a remote connection called origin pointing back to the cloned repository. This is useful for developers creating a local copy of a central repository, since it provides an easy way to pull upstream changes or publish local commits.

Does cloning repo clone all branches?

The idea is to use the git-clone to clone the repository. This will automatically fetch all the branches and tags in the cloned repository. To check out the specific branch, you can use the git-checkout command to create a local tracking branch.

Does git clone Get all branches?

While you can clone repositories with the git clone command, keep in mind that this clones the branch and the remote HEAD . This is usually master by default and includes all other branches in the repository. So when you clone a repository, you clone the master and all other branches.


1 Answers

(This isn't a typical workflow, but I'll try to answer the direct question anyway...)

git push --mirror will indeed have pushed your remote-tracking branches to the same name on the remote. However, when you clone, you by default only get the refs under refs/heads, which are mapped to remote-tracking branches under refs/remotes/origin/. You could clone with git clone --mirror to get the remote-tracking branches from the remote as well, but that option implies --bare.

If you want a repository with a working tree whose branches are mirrored from the remote, you could always change the refspec in the config option remote.origin.fetch to fetch every ref to the same name, but again I suspect that the real problem is that you're using a very unusual workflow.

Incidentally, even if you use one of those techniques to change the mapping of the refs that are fetched, this won't clone the remotes themselves, which are defined in the repository's git config - git clone doesn't clone anything from .git/config, which is considered to contain private information.

like image 148
Mark Longair Avatar answered Sep 24 '22 08:09

Mark Longair