85

I have a project with a simple local git repo, and I want to move this project (folders etc.) to another computer and work from there from now on. I don't want to have anything left on the old machine (except of course my other git projects). I want it to be as if I have been working from the new machine all along.

Can I simply move all the files over to that computer, or will there be a problem with keys? Should I have the same key across two machines? If simply moving all the folders can't be done, what should I do? I want to avoid the hassle of setting up and learning to use a server, since this seems complicated and I don't want to alter my workflow.

mk.
  • 11,360
  • 6
  • 40
  • 54

6 Answers6

77

For your case, the best way to do it is to copy over the folder (copy, scp, cp, robocopy - whichever) to the new computer and delete the old folder.

I completely disagree with @Pablo Santa Cruz that cloning is the paradigm for what you are doing. No it is not. You are moving a repo to a new computer.

Why I don't like clone for this purpose:

  • It creates remote-tracking branches for each branch in the cloned repository. You are moving, and the old repo is defunct.
  • Any remote branches and other refs are completely ignored.
  • You don't get your hooks if you had any and you might forget that you had them!
  • You cannot get "lost" commits etc using git reflog or other means. Might not be a huge issue, especially if the repo acted as a server but something to be aware of.

If you search for ways to backup a git repo, git clone wouldn't be in the top answers. So it shouldn't be used for moving a repo! I also feel that just a git clone cannot be a proper answer because git clone has the --mirror option, which preserves the repo, meaning that a git clone repo is different from git clone --mirror repo (apart from being bare, the differences are mostly those I mentioned above). I would do a copy because I know what I get with the copied repo - the same repo!

When to consider git clone:

  1. It is faster as git does some optimization while cloning
  2. You might have different git version on the new machine and a copy might make the repo unusable in the other version (not very common nowadays). But actually this can be another pro for copying, since this would inform you that that the new computer has a different git version.
mk.
  • 11,360
  • 6
  • 40
  • 54
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 19
    Everyone else is making this way too complicated. Take your entire project directory and copy it to the new machine. Done. – kubi May 02 '11 at 00:51
  • Sounds good. I recall having to generate a key or something. Will all of that work properly if I just copy it over and delete the old repo? – mk. May 02 '11 at 01:07
  • I've read that you can preserve remote branches using something like '\*:\*' but I'm not sure of the exact syntax and that's a bitch of a google keyword -- @manojlds do you know anything about that or could you elaborate? I feel as though, if branches were preserved, that would solve most of the important problems (except for hooks) – Robert Martin Sep 06 '11 at 17:17
  • a far less complicated answer: https://stackoverflow.com/a/26029296/2445763 – lasec0203 Mar 06 '18 at 01:02
  • Add to that list your local stashes and un-pushed branches, copying the folder over should preserve those too I believe. – NBcrafts Mar 05 '21 at 19:13
5

The git bundle subcommand is intended to transfer repositorys offline.

On the source machine, execute the following command in your Git workspace:

$ git bundle create --progress myproject.bundle --all

Then transfer the created file myproject.bundle to the target machine and execute there the following command:

$ git clone myproject.bundle myproject

For more, have a look at the man page of the bundle subcommand.

Oliver
  • 3,815
  • 8
  • 35
  • 63
  • 1
    Wow, this is perfect, and exactly what I needed. For future travelers: if you have two computers that are not on the internet (or perhaps, your origin/upstream are behind some VPN that's currently inaccessible), this is the perfect way to transfer repos. And, bonus, it cleanly works across PC/Mac respecting your git configurations for compatibility. – Zee2 Apr 25 '23 at 02:18
4

Yes it's enough to copy the data to the other machine. Using git clone is almost the same thing, but it will setup the computer you are cloning from as the remote origin, which might not be what you want.

rtn
  • 127,556
  • 20
  • 111
  • 121
  • Right, that is not what I want. I want to move the project over without a trace on the original machine. But will simply moving it mess up my keys? Will I need to set up new ones on the new machine? – mk. May 01 '11 at 23:26
  • No. It’s definitely not the same thing. Git clone will give you an exact copy of your remote repository. What the OP wants an exact copy of his local repo. The two things are definitely not the same thing. For instance, you might have extra resource folders & files, locally that you didn’t push to the remote. – Charles Robertson Nov 27 '22 at 11:37
1

While working on a project and you are forced to move to a new computer.

  1. Save files and copy files to new computer
  2. On new computer, install git and create ssh keys
  3. On each project folder run this

    git remote update

Raymond Tan
  • 146
  • 3
-2

Because every clone is also a repo itself, just clone the first one on the your second computer.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
-3

Instead of copying, I would recommend you to clone the repository. Cloning is the correct paradigm for what you are trying to accomplish. Cloned project will also contain all git metadata and information with it.

On the destination machine:

$ git clone git://your-repo/proj proj

Instead of using git:// you could also use ssh:// or http:// protocols to access the repository. Refer to git manual for further information.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Commit & push the repo on computer 1, clone on computer 2. Commit push on computer 2. Pull on computer 1. RTM. – RyanScottLewis May 01 '11 at 22:51
  • 1
    @coolryguy: This assumes, that there is a third "central" repository. – KingCrunch May 01 '11 at 22:54
  • I wasn't clear - I am not copying, I am moving. I don't want it to take up space on the original computer. Should I still do this, and then delete the old folder? – mk. May 01 '11 at 23:22
  • 1
    [This](http://stackoverflow.com/questions/5817095/what-tools-exist-for-simple-one-off-sharing-of-a-git-repo/5817284#5817284) explains how to temporarily make the original repo available over the `git://` protocol, in case your original machine doesn't have an ssh server running on it. – just_doug May 01 '11 at 23:28