Moving a git repo to a second computer?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmk.View Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - GitralphtheninjaView Answer on Stackoverflow
Solution 3 - GitRaymond TanView Answer on Stackoverflow
Solution 4 - GitKingCrunchView Answer on Stackoverflow
Solution 5 - GitPablo Santa CruzView Answer on Stackoverflow