65

This question may be dumb, but I've been wondering it for a while. It's about git repositories, but I assume it's the same for local repositories for other DVCS'.

Lets say my project is like this when it starts:

  • Project
    • .git
    • all the other folders in the project

So that's how it would work when you set it up right?

Lets say I move the Project folder somewhere else, would I have to change anything? Or is all the repository stuff in the .git folder relative only to the Project folder ignoring the whole file tree above Project.

I'm pretty sure moving Project wouldn't matter but I just wanted to make sure.

sehe
  • 374,641
  • 47
  • 450
  • 633
Portaljacker
  • 3,102
  • 5
  • 25
  • 33

5 Answers5

65

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.

Brent Baccala
  • 987
  • 1
  • 6
  • 18
  • 3
    I also had to edit `MODULENAME/.git` which contained a key-value-pair for `gitdir` – Linus Unnebäck Aug 20 '12 at 14:14
  • I actually changed these values to relative paths, using ../.. (however many as needed) back to the project's root directory. – Uilleann Jun 20 '13 at 15:33
  • 3
    I just tested this by creating a new git repository (as well as looking at a couple existing git repos I had lying around) and this does not appear to be correct. `SUBMODULE/.git` contains a relative path for `gitdir`, and the `worktree` attribute is also a relative path. I was able to move the super-repository without editing any files and as far as I can tell, everything still works. – David Z Jul 12 '13 at 20:35
  • 3
    It seems that whether sub-module paths are relative or not depends on the git version. See https://stackoverflow.com/questions/17568543/git-add-doesnt-work/17747571#17747571 – AXO Jan 02 '16 at 03:12
26

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.

David Z
  • 128,184
  • 27
  • 255
  • 279
10

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.

Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
  • 1
    Great advice. However the local branches don't get their upstream set to the remote tracking branches of the new `origin`. But, there are the messages: `There is no tracking information for the current branch. Please specify which branch you want to merge with.` and a suggestion: `If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/ master` – Tomasz Gandor Nov 14 '16 at 08:54
8

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)

sehe
  • 374,641
  • 47
  • 450
  • 633
1

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"
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Mansab Uppal
  • 653
  • 5
  • 4