更改 Git 存储库目录位置。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11384928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Git repository directory location.
提问by ctor
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?
对于 Windows 的 Git/Github,如果我有一个包含此目录的存储库:C:\dir1\dir2
,我需要做什么才能将存储库文件移动到C:\dir1
?我显然可以物理复制和粘贴文件,但是在 Git 方面我需要做什么?
I have this repo on GitHub and I use Git Bash and GitHub for Windows.
我在 GitHub 上有这个 repo,我使用 Git Bash 和 GitHub for Windows。
回答by ctor
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.
只需复制整个工作目录内容(包括隐藏.git
目录)。这会将整个工作目录移动到新目录,并且不会影响 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.
如果您使用 GitHub for Windows,您可以使用上述方法移动存储库。但是,当您单击应用程序中的存储库时,它将无法找到它。要解决此问题,只需单击带有 ! 的蓝色圆圈,选择 Find It,然后浏览到新目录。
回答by Denys Séguret
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).
只需复制整个存储库(及其.git
目录)。
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.
.git
结构中没有绝对路径,也没有任何东西阻止它移动,因此移动后您无事可做。所有到 github 的链接(参见 参考资料.git/config
)都将像以前一样工作。
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.
只需移动文件。然后添加 中列出的更改git status
。接下来commit
会做必要的。您会很高兴地了解到不会复制任何文件:在 git 中移动文件几乎没有成本。
回答by Mikael Engver
If you are using GitHub Desktop
, then just do the following steps:
如果您正在使用GitHub Desktop
,那么只需执行以下步骤:
- Close
GitHub Desktop
and all other applications with open files to your current directory path. - Move the whole directory as mentioned above to the new directory location.
- 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.
- 将
GitHub Desktop
打开文件的所有其他应用程序关闭到当前目录路径。 - 将上面提到的整个目录移动到新目录位置。
- 打开
GitHub Desktop
并单击蓝色 (!)“未找到存储库”图标。然后将打开一个对话框,您将看到一个“定位...”按钮,该按钮将打开一个弹出窗口,允许您将其路径定向到新位置。
回答by Gerald Davis
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).
虽然问题涉及适用于 Windows 的 Git,但即使在搜索 Visual Studio Tools For Git(VS 2012 中的扩展,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.
使用上述解决方案作为指南,我确定 Visual Studio Git Tools 使本地移动存储库(甚至所有存储库的整个目录结构)变得非常容易。
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.
1) 关闭 Visual Studio。2) 将 Repo 文件夹移动到新位置。3) 打开 Visual Studio。打开团队资源管理器。切换到“连接”视图(顶部的插头图标)。3a) 如果 Repos 仍然显示旧路径,请单击刷新以强制更新。4) 在本地移动的存储库不应再显示在“本地 Git 存储库”中。5) 单击添加(不是新建或克隆)并选择要添加的 repo 文件夹。
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.
在第 5 步中,您实际上只是提供了一个搜索路径,搜索将自动包括所有子文件夹。如果您在单个根目录下组织了多个存储库(独立存储库仅具有相同的父文件夹),则选择父级将包括在其下方找到的所有存储库。
Example: E:\Repos\RepoA E:\Repos\RepoB E:\Repos\RepoC
示例: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.
在 Visual Studio 团队资源管理器中 [Add] > "E:\Repos\" > [Add] 将所有三个返回到本地存储库。
回答by Phil Rosenberg
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.
尽管之前的答案似乎都说您可以移动目录并且.git结构中没有绝对路径。在使用 Cygwin 的 git 时,我发现这是不正确的。
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
当我移动我的 git repo 时(实际上我从备份中恢复了它,但是随着我的驱动器结构在我的新系统上发生了变化,我将它恢复到了不同的驱动器)。我收到一条错误消息,例如
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.
我使用 grep 在 [core] 部分的 .git/config 文件中找到了一个工作树变量,它保存了我的 git repo 的绝对路径。改变这个解决了我的问题。
回答by Ben
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:
我使用 Visual Studio git 插件,并且我想移动一些在 IIS 上运行的网站。一种对我有用的简单方法:
Close Visual Studio.
Move the code (including git folder, etc)
Click on the solution file from the new location
关闭 Visual Studio。
移动代码(包括git文件夹等)
从新位置单击解决方案文件
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.
这将使用已移动的现有本地 git 文件刷新到新位置的映射。回到 Visual Studio 后,我的团队资源管理器窗口会在新位置显示存储库。
回答by Shim-Sao
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.
我使用 Windows 版 Github 桌面,我想移动存储库的位置。如果您移动目录并在软件中选择新位置,没问题。但是如果你设置了一个错误的目录,你会得到一个致命的错误并且没有第二次机会重新定位到好的目录。所以要修复它。您必须将坏目录中的项目文件复制,通过 Github Desktop 进行重新配置,然后您可以再次将您的项目移动到另一个文件夹中并在软件中重新定位。无需为此关闭 Github Desktop,它会实时检查文件夹。
Hoping this will help someone.
希望这会帮助某人。
回答by charles ross
Report from the future: April 2018.
来自未来的报告:2018 年 4 月。
I wanted to normalize my local repos on my Mac and my Windows, which had ended up in different local folders.
我想标准化我的 Mac 和 Windows 上的本地存储库,它们最终位于不同的本地文件夹中。
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.
Windows 10 客户端让我经历了“找不到”>“定位”例程,乏味但并不可怕。还需要更新选项中的本地“克隆路径”以备将来使用。
When I consolidated the mac folders, the Github client just found them again - I had to do nothing!
当我合并 mac 文件夹时,Github 客户端再次找到它们 - 我什么都不用做!
回答by BOHyman
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.
要为每次克隆其中一个存储库时设置默认本地路径,您只需克隆一次存储库并设置 Localpath:/your-path,然后 github 会自动检测该路径作为您的默认路径。
回答by Chris Oates
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.
这对我不起作用。我将一个 repo 从(例如) c:\project1\ 移动到 c:\repo\project1\ 并且 Git for windows 没有显示任何更改。
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)
git status 显示错误,因为其中一个子模块“不是 git 存储库”并显示旧路径。例如(名称更改以保护 IP)
fatal: Not a git repository: C:/project1/.git/modules/subproject/subproject2 fatal: 'git status --porcelain' failed in submodule subproject
致命:不是 git 存储库:C:/project1/.git/modules/subproject/subproject2 致命:'git status --porcelain' 在子模块子项目中失败
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)
我必须手动编辑子模块中的 .git 文件以指向子模块存储库的正确相对路径(在主存储库的 .git/modules 目录中)