Git 将现有仓库推送到新的不同远程仓库服务器?

Posted

技术标签:

【中文标题】Git 将现有仓库推送到新的不同远程仓库服务器?【英文标题】:Git push existing repo to a new and different remote repo server? 【发布时间】:2011-07-08 02:23:37 【问题描述】:

假设我在git.fedorahosted.org 上有一个存储库,我想将其克隆到我在 github 的帐户中,以便在 fedorahosted 上更“官方”的存储库之外拥有自己的游乐场。 最初复制它的步骤是什么? 在 github 中有一个不错的“fork”按钮,但由于明显的原因我不能使用它。

我如何将 fedorahosted 存储库中的更改跟踪到 github 中?

【问题讨论】:

【参考方案1】:
    在 github 上创建一个新的存储库。 将 repo 从 fedorahosted 克隆到您的本地计算机。 git remote rename origin upstream git remote add origin URL_TO_GITHUB_REPO git push origin master

现在您可以像使用任何其他 github 存储库一样使用它。要从上游拉取补丁,只需运行git pull upstream master && git push origin master

【讨论】:

没有理由重命名原始起源,只需将新的游乐场称为其他名称。 @tcaswell 从技术上讲不是,但origin 指向规范的远程位置是一个强有力的约定。在这种情况下,github 位置可能是规范的。 也许我应该发布一个不同的问题,但我认为你们正在讨论我想要什么。我想添加一个新的远程,而不是替换现有的远程,这样我就可以继续从旧存储库中提取任何更改,但将任何更改推送到新存储库。 @ThatAintWorking 您可能应该打开一个新问题,但简而言之 - 您可以使用命令 git remote add 添加任意数量的遥控器。然后,您可以通过在git push 中明确声明远程来推送到其中一个。例如。 git push foobar master 将当前分支推送到远程master 上的foobar 没有推送我所有的分支,只是大师。镜像答案适用于所有分支机构【参考方案2】:

关于这个问题的已删除答案有一个有用的链接:https://help.github.com/articles/duplicating-a-repository

要点是

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 的例子:

在您的本地计算机上

$ 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

【讨论】:

裸克隆和镜像推送的好处是什么?只需添加并推送到另一个遥控器?是否会推送所有分支而不仅仅是当前分支? (如果是这样,这似乎是应注意的已接受答案的限制。) 这正是我正在寻找的信息, --bare / --mirror 通常是人们想要执行的!这是一个重要的答案!谢谢 太棒了!绝对是主题问题的最正确答案。谢谢!如果我不这样做,我总是得到error: failed to push some refs to 'https://github.com/username/testrep.git'【参考方案3】:

要将现有的 repo 推送到不同的位置,您需要:

    先克隆原始仓库。

    git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
    

    将克隆的源推送到您的新存储库:

    cd rhq
    git push https://github.com/user/example master:master
    

您可以将master:master 更改为source:destination 分支。


如果你想推送特定的提交(分支),那么做:

    在原始仓库中,创建并签出一个新分支:

    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 以查看更改历史记录。

【讨论】:

git push ... old_branch_name:new_branch_name 允许您将旧存储库中的功能分支推送为新存储库中的主分支。有用! 这是我最简单的方法。【参考方案4】:

如果您有现有的 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

【讨论】:

澄清一下,--all 会推送所有分支【参考方案5】:

您真的想简单地将本地存储库(及其本地分支等)推送到新远程,还是真的想在新远程上镜像旧远程(及其所有分支、标签等) ?如果后者是 How to properly mirror a git repository 上的一个很棒的博客。

我强烈建议您阅读博客以了解一些非常重要的细节,但简短的版本是这样的:

在新目录中运行以下命令:

git clone --mirror git@example.com/upstream-repository.git
cd upstream-repository.git
git push --mirror git@example.com/new-location.git

【讨论】:

我认为这是链接:blog.plataformatec.com.br/2013/05/…【参考方案6】:

我找到了一个使用 set-url 的解决方案,它简洁且相当容易理解

    在 Github 上创建一个新的存储库 cd 到您本地机器上的现有存储库中(如果您尚未克隆它,请先执行此操作) git remote set-url origin https://github.com/user/example.git git push -u origin master

【讨论】:

【参考方案7】:

试试这个How to move a full Git repository

    使用以下命令在 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 存储库的完整副本。

【讨论】:

【参考方案8】:

只需使用以下命令更改 GIT 存储库 URL 即可指向新存储库:

git remote set-url origin [new repo URL]

示例:git remote set-url origin git@bitbucket.org:Batman/batmanRepoName.git

现在,推送和拉取链接到新的 REPO。

然后像这样正常推送:

git push -u origin master

【讨论】:

【参考方案9】:

这里是手动操作git remote set-url origin [new repo URL]

    克隆存储库:git clone &lt;old remote&gt; 创建 GitHub 存储库

    打开&lt;repository&gt;/.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
    

    并更改遥控器(url 选项)

    [remote "origin"]
        url = <new remote>
        fetch = +refs/heads/*:refs/remotes/origin/*
    

    将存储库推送到 GitHub:git push

你也可以同时使用/multiple remotes。

【讨论】:

【参考方案10】:

我也遇到了同样的问题。

就我而言,由于我的本地机器中有原始存储库,因此我在没有任何隐藏文件(.git、.gitignore)的新文件夹中制作了一个副本。

最后我将 .gitignore 文件添加到新创建的文件夹中。

然后我从本地路径创建并添加了新的存储库(在我的例子中使用 GitHub Desktop)。

【讨论】:

【参考方案11】:

将本地存储库链接到不同的远程存储库

1- 删除与远程存储库的所有连接: 项目文件夹内:

git rm .git(从本地存储库中删除所有数据) git status(我必须说它没有链接到任何东西,就像一个错误)

2- 链接到新的远程存储库

git init 启动本地仓库 git remote add origin urlrepository.git 链接远程仓库 git remote -v确认链接到远程仓库

3- 将更改添加到本地存储库并推送到远程存储库

git pullgit pull origin master --allow-unrelated-histories 如果本地和远程仓库中的 git 历史记录不同。 git add. git commit -m" Message " git push -u origin master

就是这样!

【讨论】:

【参考方案12】:

从命令行推送现有存储库

git remote add origin https://github.com/AyadiAkrem/teachandgo.git
git branch -M main
git push -u origin main

【讨论】:

【参考方案13】:

这有助于我将本地项目推送到 git 上的不同 repo 中

 git push https://github.com/yourusername/yourgithubproject.git master:master

【讨论】:

以上是关于Git 将现有仓库推送到新的不同远程仓库服务器?的主要内容,如果未能解决你的问题,请参考以下文章

Git学习——把文件推送到远程仓库

Git LFS 文件未推送到远程仓库

git怎么将本地仓库推送到一个新的远程分支包含所有历史提交信息

git能直接把本地仓库推送到远程仓库吗?

在自己的服务器上搭建git仓库服务器(本地推送到远程仓库远程仓库推送到本地)

如何在 Git 中创建新的本地分支?