-->

Change Git repository directory location.

2019-01-21 00:51发布

问题:

With Git/Github for Windows, if I have a repository with this directory: C:\dir1\dir2, what do I need to do to move the repo files to C:\dir1? I can obviously physically copy and paste the files, but what do I need to do on the Git side?

I have this repo on GitHub and I use Git Bash and GitHub for Windows.

回答1:

Simply copy the entire working directory contents (including the hidden .git directory). This will move the entire working directory to the new directory and will not affect the remote repository on GitHub.

If you are using GitHub for Windows, you may move the repository using the method as above. However, when you click on the repository in the application it will be unable to find it. To resolve this simply click on the blue circle with the !, select Find It and then browse to the new directory.



回答2:

I'm not sure of the question, so here are two answers :

If you want to move your repository :

Simply copy the whole repository (with its .git directory).

There is no absolute path in the .git structure and nothing preventing it to be moved so you have nothing to do after the move. All the links to github (see in .git/config) will work as before.

If you want to move files inside the repository :

Simply move the files. Then add the changes listed in git status. The next commit will do the necessary. You'll be happy to learn that no file will be duplicated : moving a file in git is almost costless.



回答3:

If you are using GitHub for Windows, then just do the following steps:

  1. Close GitHub for Windows and all other applications with open files to your current directory path.
  2. Move the whole directory as mentioned above to the new directory location.
  3. Open GitHub for Windows and click on the blue (!) "repository not found" icon. Then a dialog will open and you will see a "Find it" button where you can relocate the new repository path.


回答4:

While the question involves Git for Windows, this seems to be the top result even when searching for Visual Studio Tools For Git (extension in VS 2012, native support in VS 2013).

Using the solutions above as a guide I determined that Visual Studio Git Tools makes moving repos (or even entire directory structure for all repos) locally very easy.

1) Close Visual Studio. 2) Move the Repo folder(s) to new location. 3) Open Visual Studio. Open Team Explorer. Switch to "Connect" view (plug icon at top). 3a) If Repos still show old path, click Refresh to force an update. 4) Repos that were moved locally should no longer be showing in "Local Git Repositories". 5) Click Add (not new or clone) and select the repo folder to add.

In step 5 you really are just providing a search path and the search automatically includes all subfolders. If you have multiple repos organized under a single root (independent repos just having the same parent folder) then selecting the parent will include all repos found below that.

Example: E:\Repos\RepoA E:\Repos\RepoB E:\Repos\RepoC

In Visual Studio Team Explorer [Add] > "E:\Repos\" > [Add] will return all three to the Local Repositories.



回答5:

I use Visual Studio git plugin, and I have some websites running on IIS I wanted to move. A simple way that worked for me:

  1. Close Visual Studio.

  2. Move the code (including git folder, etc)

  3. Click on the solution file from the new location

This refreshes the mapping to the new location, using the existing local git files that were moved. Once i was back in Visual Studio, my Team Explorer window showed the repos in the new location.



回答6:

This did not work for me. I moved a repo from (e.g.) c:\project1\ to c:\repo\project1\ and Git for windows does not show any changes.

git status shows an error because one of the submodules "is not a git repository" and shows the old path. e.g. (names changed to protect IP)

fatal: Not a git repository: C:/project1/.git/modules/subproject/subproject2 fatal: 'git status --porcelain' failed in submodule subproject

I had to manually edit the .git files in the submodules to point to the correct relative path to the submodule's repo (in the main repo's .git/modules directory)



回答7:

Report from the future: April 2018.

I wanted to normalize my local repos on my Mac and my Windows, which had ended up in different local folders.

The Windows 10 client made me go through the "Can't Find" > "Locate" routine, tedious but not terrible. Also need to update the local "Clone path" in Options for future use.

When I consolidated the mac folders, the Github client just found them again - I had to do nothing!



回答8:

To set the default local path to your repositories for every time you clone one of them, you only must clone a repository once and set Localpath:/your-path, then github automatically detect that path as your default.



回答9:

I use Github Desktop for Windows and I wanted to move location of a repository. No problem if you move your directory and choose the new location in the software. But if you set a bad directory, you get a fatal error and no second chance to make a relocation to the good one. So to repair that. You must copy project files in the bad directory, make its reconize by Github Desktop, after that, you can move again your project in another folder and make a relocate in the software. No need to close Github Desktop for that, it will check folders in live.

Hoping this will help someone.



回答10:

Although the previous answers all seem to say that you can just move the directory and there are no absolute paths in the .git structure. I found this to be untrue when using git from Cygwin.

When I moved my git repo (in fact I restored it from a backup, but to a different drive as my drive structure changed on my new system). I got an error message like

fatal: Invalid path '<part_of_the_original_repo_path>': No such file or directory

I used grep to find that in my .git/config file in the [core] section is a worktree variable which holds the absolute path of my git repo. Changing this fixed the problem for me.