Moving a git repository

梦想与她 提交于 2019-11-29 23:28:25

Yes, everything in .git is relative. If you have added the repository as a named remote to another repository, you would have to change the remote URL in that other repository, though.

I've found that submodules are not relative in git.

So, if you want to move a project that contains submodules, you have to edit the .git file in the submodule, as well as the "worktree" attribute in the submodule config file, which is stored in the parent's .git/modules/MODULENAME/config file.

Show current remote (this is optional step of course):

$ git remote show origin
* remote origin
URL: git@oldserver:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
Tracked remote branches
master

What we need to do is to remove current origin and add the new one:

$ git remote rm origin
$ git remote add origin git@newserver:project.git
$ git remote show origin
* remote origin
URL: git@newserver:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
error: refs/remotes/origin/HEAD points nowhere!
New remote branches (next fetch will store in remotes/origin)
master

Don’t worry about the error shown by the last command. The first pull from the origin will fix it:

$ git pull
From git@newserver:project.git
* [new branch]      master     -> origin/master
Already up-to-date.
$ git remote show origin
* remote origin
URL: git@newserver:project.git
Remote branch(es) merged with 'git pull' while on branch master
master
Tracked remote branches
master

I’m still fresh with Git so maybe thre’s a better way to do it but this worked for me.

No, you wouldn't have to change anything else. That is

  • assuming no scripts set GIT_DIR, GIT_WORKTREE or GIT_INDEX directly (unlikely)
  • you have no external repositories pointing to this copy

    • if you do you'll have to repoint them by using

      git remote set-url [--push] origin user@yourhost:/home/user/newlocation/Project
      

(origin should be the name of the remote; origin is the default name when cloning from a remote repo)

Mansab Uppal

You can move the git directory from one machine to another or say, you want to migrate your project folder.

All the information about origin is stored in '.git' folder, which is created when you initialize a git repository under a directory on your system.

Procedure

  1. : Move the project folder to other location.
  2. : If the project folder is being moved to a new machine, then create corresponding SSH key-pair.
  3. : Tell git who you are using: git config --global user.email "your_git_email_id@youremail"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!