移动 git 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7949036/
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
Moving a git repository
提问by PortalHymaner
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'.
这个问题可能很愚蠢,但我已经想了一段时间了。这是关于 git 存储库,但我认为它与其他 DVCS 的本地存储库相同。
Lets say my project is like this when it starts:
假设我的项目开始时是这样的:
- Project
- .git
- all the other folders in the project
- 项目
- .git
- 项目中的所有其他文件夹
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.
假设我将 Project 文件夹移到其他地方,是否需要更改任何内容?或者 .git 文件夹中的所有存储库内容仅与 Project 文件夹相关,而忽略了 Project 上方的整个文件树。
I'm pretty sure moving Project wouldn't matter but I just wanted to make sure.
我很确定移动项目无关紧要,但我只是想确定一下。
采纳答案by David Z
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.
是的,里面的一切.git
都是相对的。如果您已将存储库作为命名远程添加到另一个存储库,则必须更改该其他存储库中的远程 URL。
回答by Brent Baccala
I've found that submodules are not relative in git.
我发现子模块在 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.
因此,如果要移动包含子模块的项目,则必须编辑.git
子模块中的文件,以及存储在父模块中的子模块配置文件中的“worktree”属性.git/modules/MODULENAME/config
。
回答by Eduardo Cuomo
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.
我对 Git 还很陌生,所以也许有更好的方法来做到这一点,但这对我有用。
回答by sehe
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 repointthem by using
git remote set-url [--push] origin user@yourhost:/home/user/newlocation/Project
- 假设没有脚本直接设置 GIT_DIR、GIT_WORKTREE 或 GIT_INDEX(不太可能)
您没有指向此副本的外部存储库
如果你这样做,你必须通过使用重新指向它们
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)
(origin 应该是远程的名称;origin 是从远程仓库克隆时的默认名称)
回答by Mansab Uppal
You can move the git directory from one machine to another or say, you want to migrate your project folder.
您可以将 git 目录从一台机器移动到另一台机器,或者说,您想要迁移您的项目文件夹。
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.
所有有关 origin 的信息都存储在“.git”文件夹中,该文件夹是在您初始化系统目录下的 git 存储库时创建的。
Procedure
程序
- : Move the project folder to other location.
- : If the project folder is being moved to a new machine, then create corresponding SSH key-pair.
- : Tell git who you are using: git config --global user.email "your_git_email_id@youremail"
- :将项目文件夹移动到其他位置。
- : 如果项目文件夹被移动到新机器,则创建相应的 SSH 密钥对。
- :告诉 git 你正在使用谁: git config --global user.email "your_git_email_id@youremail"