1

My project's git repo is currently located on my org's local network. The org asked me to move the project's repo to a different local network. My goal is to keep all of the all the commit's history and branches and not just copy-paste the files to a new repo.

Both networks have a GitLab clone on them if it helps somehow.

How can I achieve this?

Any help would be much appreciated.

  • 4
    Why doesn't copy paste work? As long as the `.git` folder is kept then all is good. Also, what about a mirror clone? – evolutionxbox Mar 15 '21 at 12:04
  • @evolutionxbox Correct me if I'm wrong but different branches on the repo have different files and I can only checkout one branch at a time. Copy-pasting feels too clumsy to not miss something. – Maor Magori Mar 15 '21 at 12:08
  • 2
    That's not how git works. You could copy just the `.git` folder and you would have all the files ever committed to the repo (almost). Branches don't contain files. – evolutionxbox Mar 15 '21 at 12:11

1 Answers1

2

There are a couple of ways to move a local repository to a new location:

  1. Using simple copy-paste

This won't hurt you since, as long as you copy your .git folder properly. Git won't get confused if you move it to a new place since it has a "relative" notion of where the project is. However, in the unlikely event of you having git version 1.7.8 or 1.7.9 AND also have submodules in your project, then you should be careful with this method, as it may lead to problems. Read more about it here.

  1. Using git clone --mirror

This is a more "git" way of doing things. As described in the documentation

--mirror

Set up a mirror of the source repository. This implies --bare. Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote-tracking branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.

  1. Using git bundle

This is another "git" way of doing this. This would produce a single file that you can later move to your new destination and apply it there. If in your case, you don't have a repo at the new destination, I suggest you follow the answer from VonC about moving everything with git bundle.

You would want to do something like

$ git bundle create /tmp/foo master
$ git bundle create /tmp/foo-all --all
$ git bundle list-heads /tmp/foo
$ git bundle list-heads /tmp/foo-all

I think the easiest is to just copy-paste your project if you have easy access to the new location.

mnestorov
  • 4,116
  • 2
  • 14
  • 24
  • Question, will the new repo contain all the commits history using these methods? – Maor Magori Mar 15 '21 at 15:25
  • Yes. For example, if you copy `.git` folder to the new destination, you would still have your history, just try it and see what happens. Copying won't hurt you. Make sure that after the copy you have what you want and then you can delete the old repo. Don't be destructive unless you really need to. – mnestorov Mar 15 '21 at 15:31
  • Thank you! I Will try the mirror/bundle methods. – Maor Magori Mar 15 '21 at 15:34