Change Git repository directory location.

GitGithubGithub for-Windows

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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

  1. Close GitHub Desktop 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 Desktop and click on the blue (!) "repository not found" icon. Then a dialog will open and you will see a "Locate..." button which will open a popup allowing you to direct its path to a new location.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Solution 7 - Git

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.

Solution 8 - Git

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)

Solution 9 - Git

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!

Solution 10 - Git

A more Git based approach would be to make the changes to your local copy using cd or copy and pasting and then pushing these changes from local to remote repository.

If you try checking status of your local repo, it may show "untracked changes" which are actually the relocated files. To push these changes forcefully, you need to stage these files/directories by using

$ git add -A
#And commiting them
$ git commit -m "Relocating image demo files"
#And finally, push
$ git push -u local_repo -f HEAD:master

Hope it helps.

Solution 11 - Git

-First check all the directories in current folder containing git repo $ ls -la Or ls -al

-Identify this folder

**.git**

-Use this command to move the folder to the location you need,

$ mv .git the directory you want here

Note: >The directory wont affect git history neither Remote connection >Consider the tree (path) to the directory you're moving to

Solution 12 - Git

If you use Visual Studio, use Add which is in Teams Explorer > Connect tab > Local Git Repositories to bring an existing local repo to your available repos. No fuss, no hassle.

Solution 13 - Git

1- Delete all connection with the remote repository: Inside the project folder:

  • git rm .git (Remove all data from local repository)
  • git status (I must say that it is not linked to any)

2- Link to a new remote repository

  • git init To start a local repository
  • git remote add origin urlrepository.git To link with remote repository
  • git remote -v To confirm that it is linked to the remote repository

3- Add changes to the local repository and push

  • git pull or git pull origin master --allow-unrelated-histories if git history is different in both local and remote repo.
  • git add.
  • git commit -m" Message "
  • git push -u origin master

Solution 14 - Git

If you've already moved/modified the code in the new destination, but want to keep using the same git repo...

In Windows, you can just copy-paste the hidden .git folder from the old location to the new location.

Then use git as per usual.

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
Questionuser596075View Question on Stackoverflow
Solution 1 - GitctorView Answer on Stackoverflow
Solution 2 - GitDenys SéguretView Answer on Stackoverflow
Solution 3 - GitMikael EngverView Answer on Stackoverflow
Solution 4 - GitPhil RosenbergView Answer on Stackoverflow
Solution 5 - GitGerald DavisView Answer on Stackoverflow
Solution 6 - GitBenView Answer on Stackoverflow
Solution 7 - GitShim-SaoView Answer on Stackoverflow
Solution 8 - GitChris OatesView Answer on Stackoverflow
Solution 9 - Gitcharles rossView Answer on Stackoverflow
Solution 10 - GitPe DroView Answer on Stackoverflow
Solution 11 - Gitrabin computersView Answer on Stackoverflow
Solution 12 - GitSouthSunView Answer on Stackoverflow
Solution 13 - GitCaro PérezView Answer on Stackoverflow
Solution 14 - GitAndrewView Answer on Stackoverflow