简体   繁体   English

更改 Git 存储库目录位置。

[英]Change Git repository directory location.

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 ?对于 Windows 版 Git/Github,如果我有一个包含此目录的存储库: C:\dir1\dir2 ,我需要做什么才能将存储库文件移动到C:\dir1 I can obviously physically copy and paste the files, but what do I need to do on the Git side?我显然可以物理复制和粘贴文件,但我需要在 Git 端做什么?

I have this repo on GitHub and I use Git Bash and GitHub for Windows.我在 GitHub 上有这个 repo,我在 Windows 上使用 Git Bash 和 GitHub。

Simply copy the entire working directory contents (including the hidden .git directory).只需复制整个工作目录内容(包括隐藏的.git目录)。 This will move the entire working directory to the new directory and will not affect the remote repository on GitHub.这会将整个工作目录移动到新目录,并且不会影响 GitHub 上的远程存储库。

If you are using GitHub for Windows, you may move the repository using the method as above.如果您使用的是 GitHub for Windows,您可以使用上述方法移动存储库。 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.选择“查找”,然后浏览到新目录。

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. .git结构中没有绝对路径,也没有任何东西阻止它被移动,所以移动后你无事可做。 All the links to github (see in .git/config ) will work as before.所有指向 github 的链接(参见.git/config )将像以前一样工作。

If you want to move files inside the repository:如果要在存储库中移动文件:

Simply move the files.只需移动文件。 Then add the changes listed in git status .然后添加git status中列出的更改。 The next commit will do the necessary.下一次commit将做必要的。 You'll be happy to learn that no file will be duplicated: moving a file in git is almost costless.您会很高兴得知没有文件会被复制:在 git 中移动文件几乎是免费的。

If you are using GitHub Desktop , then just do the following steps:如果您使用的是GitHub Desktop ,那么只需执行以下步骤:

  1. Close GitHub Desktop and all other applications with open files to your current directory path.将打开文件的GitHub Desktop和所有其他应用程序关闭到当前目录路径。
  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.打开GitHub Desktop并单击蓝色 (.)“未找到存储库”图标。 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.然后将打开一个对话框,您将看到一个“定位...”按钮,该按钮将打开一个弹出窗口,允许您将其路径定向到新位置。

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.虽然之前的回答好像都是说移动目录就可以,.git结构中没有绝对路径。 I found this to be untrue when using git from Cygwin.当我使用来自 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).当我移动我的 git repo 时(实际上我从备份中恢复了它,但是由于我的驱动器结构在我的新系统上发生了变化而恢复到不同的驱动器)。 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.我使用 grep 发现在 [core] 部分的 my.git/config 文件中是一个 worktree 变量,它保存我的 git repo 的绝对路径。 Changing this fixed the problem for me.改变这个解决了我的问题。

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 工具可以非常轻松地在本地移动存储库(甚至所有存储库的整个目录结构)。

1) Close Visual Studio. 1) 关闭视觉工作室。 2) Move the Repo folder(s) to new location. 2) 将 Repo 文件夹移动到新位置。 3) Open Visual Studio. 3) 打开视觉工作室。 Open Team Explorer.打开团队资源管理器。 Switch to "Connect" view (plug icon at top).切换到“连接”视图(顶部的插头图标)。 3a) If Repos still show old path, click Refresh to force an update. 3a) 如果 Repos 仍然显示旧路径,请单击刷新以强制更新。 4) Repos that were moved locally should no longer be showing in " Local Git Repositories ". 4) 本地移动的回购不应再显示在“本地 Git 存储库”中。 5) Click Add (not new or clone) and select the repo folder to add. 5) 单击添加(不是新建或克隆)并选择要添加的 repo 文件夹。

In step 5 you really are just providing a search path and the search automatically includes all subfolders.在第 5 步中,您实际上只是提供了一个搜索路径,搜索会自动包括所有子文件夹。 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示例: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 Team Explorer 中 [Add] > "E:\Repos\" > [Add] 会将所有三个返回到本地存储库。

I use Visual Studio git plugin, and I have some websites running on IIS I wanted to move.我使用 Visual Studio git 插件,我有一些在 IIS 上运行的网站我想移动。 A simple way that worked for me:一种对我有用的简单方法:

  1. Close Visual Studio.关闭视觉工作室。

  2. Move the code (including git folder, etc)移动代码(包括git文件夹等)

  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.这将使用已移动的现有本地 git 文件刷新到新位置的映射。 Once i was back in Visual Studio, my Team Explorer window showed the repos in the new location.回到 Visual Studio 后,我的团队资源管理器窗口显示了新位置的存储库。

I use Github Desktop for Windows and I wanted to move location of a repository.我使用 Github Desktop for Windows,我想移动存储库的位置。 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.您必须将项目文件复制到坏目录中,通过 Github Desktop 对其进行 reconize,之后,您可以将您的项目再次移动到另一个文件夹中,并在软件中进行重新定位。 No need to close Github Desktop for that, it will check folders in live.无需为此关闭 Github Desktop,它会实时检查文件夹。

Hoping this will help someone.希望这会对某人有所帮助。

This did not work for me.这对我不起作用。 I moved a repo from (eg) c:\project1\ to c:\repo\project1\ and Git for windows does not show any changes.我将一个存储库从(例如)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. git status 显示错误,因为子模块之一“不是 git 存储库”并显示旧路径。 eg (names changed to protect IP)例如(更改名称以保护 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 目录中)

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. Windows 10 客户端让我经历了“找不到”>“定位”的例程,乏味但并不可怕。 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!当我合并 mac 文件夹时,Github 客户端又找到了它们——我什么都不用做!

If you've already moved/modified the code in the new destination, but want to keep using the same git repo...如果您已经移动/修改了新目标中的代码,但想继续使用相同的 git 存储库...

In Windows, you can just copy-paste the hidden .git folder from the old location to the new location.在 Windows 中,您只需将隐藏的.git文件夹从旧位置复制粘贴到新位置即可。

Then use git as per usual.然后像往常一样使用 git。

要在每次克隆其中一个时设置存储库的默认本地路径,您只需克隆一次存储库并设置Localpath:/ your-path,然后github会自动将该路径检测为默认路径。

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.一种更基于 Git 的方法是使用cd或复制和粘贴对本地副本进行更改,然后将这些更改从本地存储库推送到远程存储库。

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.希望能帮助到你。

-First check all the directories in current folder containing git repo $ ls -la Or ls -al -首先检查当前文件夹中包含git repo的所有目录 $ ls -lals -al

-Identify this folder - 识别这个文件夹

**.git**

-Use this command to move the folder to the location you need, - 使用此命令将文件夹移动到您需要的位置,

$ mv.git the directory you want here $ mv.git这里是你想要的目录

Note: >The directory wont affect git history neither Remote connection >Consider the tree ( path ) to the directory you're moving to注意:>该目录不会影响 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.如果您使用 Visual Studio,请使用 Teams Explorer > Connect 选项卡 > Local Git Repositories 中的 Add 将现有的本地 repo 引入您的可用 repos。 No fuss, no hassle.没有大惊小怪,没有麻烦。

Link a local repository to a different remote repository将本地存储库链接到不同的远程存储库

1- Delete all connection with the remote repository : Inside the project folder: 1-删除与远程存储库的所有连接:在项目文件夹内:

  • git rm.git (Remove all data from local repository) git rm.git (从本地存储库中删除所有数据)
  • git status (I must say that it is not linked to any) git status (我必须说它没有链接到任何)

2- Link to a new remote repository 2-链接到一个新的远程仓库

  • git init To start a local repository git init启动一个本地仓库
  • git remote add origin urlrepository.git To link with remote repository git remote add origin urlrepository.git连接远程仓库
  • git remote -v To confirm that it is linked to the remote repository git remote -v确认链接到远程仓库

3- Add changes to the local repository and push 3-将更改添加到本地存储库并推送

  • git pull or git pull origin master --allow-unrelated-histories if git history is different in both local and remote repo. git pullgit pull origin master --allow-unrelated-histories如果本地和远程仓库中的 git 历史不同。
  • git add.
  • git commit -m" Message "
  • git push -u origin master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM