tags:

views:

99

answers:

5

I'm cloning a git repository from a computer that's going to be wiped.

Is it possible to clone a repository without making the original repository origin/master? Or do I need to clone it, and then delete the remote branch (which is done with git remote rm origin)?

Edit: The repository has only one branch, and no tags.

+1  A: 

I do believe you would need to delete the remote branch.

tr4656
If you want it name something else, you could do git clone -o not_origin
Kyle Butt
The problem is that when you delete the remote (not remote branch), you delete all the remote/<remote-name> branches as well.
ebneter
@ebneter: what do you mean by "delete the remote (not remote branch)"?
Andrew Grimm
@Andrew Grimm: The "remote" is what's configured by 'git remote'; I think of the "remote branch" as being the corresponding branch on the remote repo. That is, something you'd delete with 'git push origin :branch', which actually removes the branch on the remote.git's terminology is a little confusing in this regard.
ebneter
+2  A: 

It is not necessary to make the original repository the "origin" remote to clone the master branch.

On the new machine, create a new repository:

git init foo

Then pull the old repository into the new one, without creating a remote:

cd foo
git pull <reference to old repository>

However, it should be noted that the easiest way to save off a repository would be to just zip the repository's directory up and move the files to a new machine. That will preserve any and all remotes, tags, etc.

As noted below, when copying the repository, be careful when going from case-sensitive file systems (eg. Linux, Mac, NTFS) to non-case sensitive (eg. Fat32).

Jess Bowers
That's cool, I didn't know you could do that. Can you clone non-master branches that way as well?
ebneter
If applying the latter, don't copy something from a non-FAT32 to a FAT32 partition and back again. http://stackoverflow.com/questions/1754108/i-backed-up-a-git-project-and-got-fatal-not-a-git-repository
Andrew Grimm
Would you do the former with or without `file:///`, ie using `git pull /home/username/blah` versus `git pull file:///home/username/blah`?
Andrew Grimm
If it's a local repository, you can just use /home/username/blah
Jess Bowers
And yes, ebneter, you can clone non-branches that way as well. Like this: git pull <ref to repo> <name of branch>. eg: git pull /home/jess/bar-repository my_alternate_branch
Jess Bowers
A: 

Does the remote repo have more branches than just master? It's trickier if it does; you need to make local copies of all of the branches as well because when you delete the remote, all the remote/origin branches will go away. (I just tried it!)

So, you can do something like this:

git clone <remote_url>
cd <repo>
for b in $(git branch -r | grep -v HEAD | grep -v master);do
    git branch $(basename $b) $b
done
git remote rm origin

Of course, as Jess Bowers said, it's even easier to just tar or zip it up and move the files, if you have access to the machine to do that.

You can also just make a bare clone:

git clone --bare <remote_url>

and then make new clones from that.

ebneter
A: 

The general way to handle such a trasfer would be git bundle.

See Backup of github repo.

From your second (new) desktop:

 git bundle create file:///\\old_desktop/share/my_git_repo --all

to create one local file from which you will be able to clone a local git repo.

Note: the file:/// protocol will support UNC (Universal Naming Convention) Windows path, with the rest of the path using '/' instead of '\'.
See Git on a Windows Lan.

VonC
A: 

First, you can use --origin <name> option of git clone

--origin <name>, -o <name>

Instead of using the remote name origin to keep track of the upstream repository, use <name>.

Second, you can use git remote add to add repository to fetch from to existing repository (so you can use git init, then git remote add <name> <url>).

Jakub Narębski