Git 将现有存储库推送到新的和不同的远程存储库服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5181845/
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
Git push existing repo to a new and different remote repo server?
提问by Heiko Rupp
Say I have a repository on git.fedorahosted.organd I want to clone this into my account at github to have my own playground aside from the more "official" repo on fedorahosted. What would be the steps to initially copy that over? Within github there is this nice "fork" button, but I can't use this for obvious reasons.
假设我在git.fedorahosted.org上有一个存储库,并且我想将它克隆到我在 github 的帐户中,以便在 Fedorahosted 上拥有更“官方”的存储库之外拥有自己的游乐场。最初复制它的步骤是什么?在 github 中有一个很好的“fork”按钮,但由于显而易见的原因我不能使用它。
And how would I track changes in the fedorahosted repo into the github one?
我如何将 Fedorahosted 存储库中的更改跟踪到 github 存储库中?
回答by troelskn
- Create a new repo at github.
- Clone the repo from fedorahosted to your local machine.
git remote rename origin upstream
git remote add origin URL_TO_GITHUB_REPO
git push origin master
- 在 github 创建一个新的 repo。
- 将存储库从 fedorahosted 克隆到本地机器。
git remote rename origin upstream
git remote add origin URL_TO_GITHUB_REPO
git push origin master
Now you can work with it just like any other github repo. To pull in patches from upstream, simply run git pull upstream master && git push origin master
.
现在您可以像任何其他 github 存储库一样使用它。要从上游提取补丁,只需运行git pull upstream master && git push origin master
.
回答by mob
There is a deleted answer on this question that had a useful link: https://help.github.com/articles/duplicating-a-repository
这个问题有一个删除的答案,有一个有用的链接:https: //help.github.com/articles/duplicating-a-repository
The gist is
要点是
0. create the new empty repository (say, on github)
1. make a bare clone of the repository in some temporary location
2. change to the temporary location
3. perform a mirror-push to the new repository
4. change to another location and delete the temporary location
OP's example:
OP的例子:
On your local machine
在您的本地机器上
$ cd $HOME
$ git clone --bare https://git.fedorahosted.org/the/path/to/my_repo.git
$ cd my_repo.git
$ git push --mirror https://github.com/my_username/my_repo.git
$ cd ..
$ rm -rf my_repo.git
回答by kenorb
To push your existing repo into different, you need to:
要将现有的 repo 推送到不同的位置,您需要:
Clone the original repo first.
git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
Push the cloned sources to your new repository:
cd rhq git push https://github.com/user/example master:master
首先克隆原始 repo。
git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
将克隆的源推送到您的新存储库:
cd rhq git push https://github.com/user/example master:master
You may change master:master
into source:destination
branch.
您可以更改master:master
为source:destination
分支。
If you want to push specific commit (branch), then do:
如果要推送特定提交(分支),请执行以下操作:
On the original repo, create and checkout a new branch:
git checkout -b new_branch
Choose and reset to the point which you want to start with:
git log # Find the interesting hash git reset 4b62bdc9087bf33cc01d0462bf16bbf396369c81 --hard
Alternatively select the commit by
git cherry-pick
to append into existing HEAD.Then push to your new repo:
git push https://github.com/user/example new_branch:master
If you're rebasing, use
-f
for force push (not recommended). Rungit reflog
to see history of changes.
在原始 repo 上,创建并签出一个新分支:
git checkout -b new_branch
选择并重置到您想要开始的点:
git log # Find the interesting hash git reset 4b62bdc9087bf33cc01d0462bf16bbf396369c81 --hard
或者选择提交方式
git cherry-pick
以附加到现有的 HEAD 中。然后推送到你的新仓库:
git push https://github.com/user/example new_branch:master
如果您正在变基,请使用
-f
强制推送(不推荐)。运行git reflog
以查看更改历史记录。
回答by HairOfTheDog
Do you really want to simply push your local repository (with its local branches, etc.) to the new remote or do you really want to mirror the old remote (with all its branches, tags, etc) on the new remote? If the latter here's a great blog on How to properly mirror a git repository.
您真的想简单地将本地存储库(及其本地分支等)推送到新的远程,还是真的想在新的远程上镜像旧的远程(及其所有分支、标签等)?如果后者是关于如何正确镜像 git 存储库的很棒的博客。
I strongly encourage you to read the blog for some very important details, but the short version is this:
我强烈建议您阅读博客以了解一些非常重要的细节,但简短的版本是这样的:
In a new directory run these commands:
在新目录中运行以下命令:
git clone --mirror [email protected]/upstream-repository.git
cd upstream-repository.git
git push --mirror [email protected]/new-location.git
回答by vm345
Try this How to move a full Git repository
Create a local repository in the temp-dir directory using:
git clone temp-dir
Go into the temp-dir directory.
To see a list of the different branches in ORI do:
git branch -a
Checkout all the branches that you want to copy from ORI to NEW using:
git checkout branch-name
Now fetch all the tags from ORI using:
git fetch --tags
Before doing the next step make sure to check your local tags and branches using the following commands:
git tag git branch -a
Now clear the link to the ORI repository with the following command:
git remote rm origin
Now link your local repository to your newly created NEW repository using the following command:
git remote add origin <url to NEW repo>
Now push all your branches and tags with these commands:
git push origin --all git push --tags
You now have a full copy from your ORI repo.
使用以下命令在 temp-dir 目录中创建本地存储库:
git clone 临时目录
进入 temp-dir 目录。
要查看 ORI 中不同分支的列表,请执行以下操作:
git branch -a
使用以下命令检查要从 ORI 复制到 NEW 的所有分支:
git checkout branch-name
现在使用以下命令从 ORI 获取所有标签:
git fetch --tags
在执行下一步之前,请确保使用以下命令检查本地标签和分支:
git tag git branch -a
现在使用以下命令清除指向 ORI 存储库的链接:
git remote rm origin
现在使用以下命令将您的本地存储库链接到新创建的新存储库:
git remote add origin <url to NEW repo>
现在使用以下命令推送所有分支和标签:
git push origin --all git push --tags
您现在拥有来自 ORI 存储库的完整副本。
回答by Mobiletainment
I found a solution using set-urlwhich is concise and fairly easy to understand:
我找到了一个使用set-url的解决方案,它简洁且易于理解:
- create a new repo at Github
cd
into the existing repository on your local machine (if you haven't cloned it yet, then do this first)git remote set-url origin https://github.com/user/example.git
git push -u origin master
- 在 Github 创建一个新的仓库
cd
到您本地机器上的现有存储库中(如果您还没有克隆它,请先执行此操作)git remote set-url origin https://github.com/user/example.git
git push -u origin master
回答by Kamil Nekanowicz
If you have Existing Git repository:
如果您有现有的 Git 存储库:
cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.com/newproject
git push -u origin --all
git push -u origin --tags
回答by Abdel Ourimchi
Simply point the new repo by changing the GIT repo URL with this command:
只需使用以下命令更改 GIT 存储库 URL 即可指向新存储库:
git remote set-url origin [new repo URL]
Example: git remote set-url origin [email protected]:Batman/batmanRepoName.git
例子: git remote set-url origin [email protected]:Batman/batmanRepoName.git
Now, pushing and pulling are linked to the new REPO.
现在,推和拉都与新的 REPO 相关联。
Then push normally like so:
然后像这样正常推送:
git push -u origin master
回答by itrascastro
I have had the same problem.
我曾经也有过一样的问题。
In my case, since I have the original repository in my local machine, I have made a copy in a new folder without any hidden file (.git, .gitignore).
就我而言,由于我在本地计算机中有原始存储库,因此我在没有任何隐藏文件(.git、.gitignore)的新文件夹中复制了一份。
Finally I have added the .gitignore file to the new created folder.
最后,我将 .gitignore 文件添加到新创建的文件夹中。
Then I have created and added the new repository from the local path (in my case using GitHub Desktop).
然后我从本地路径创建并添加了新的存储库(在我的例子中使用 GitHub Desktop)。
回答by upe
Here is a manual way to do git remote set-url origin [new repo URL]
:
这是一种手动方法git remote set-url origin [new repo URL]
:
- Clone the repository:
git clone <old remote>
- Create a GitHub repository
Open
<repository>/.git/config
$ git config -e
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = <old remote> fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master
and change the remote (the url option)
[remote "origin"] url = <new remote> fetch = +refs/heads/*:refs/remotes/origin/*
Push the repository to GitHub:
git push
- 克隆存储库:
git clone <old remote>
- 创建一个 GitHub 存储库
打开
<repository>/.git/config
$ git config -e
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = <old remote> fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master
并更改遥控器(网址选项)
[remote "origin"] url = <new remote> fetch = +refs/heads/*:refs/remotes/origin/*
将存储库推送到 GitHub:
git push
You can also use both/multiple remotes.
您也可以使用两个/多个遥控器。