Git将现有的repo推送到一个新的不同的远程repo服务器?

Hei*_*upp 454 git github

假设我在git.fedorahosted.org上有一个存储库,我想将它克隆到我在github的帐户中,除了fedorahosted上更"官方"的回购之外,还有我自己的游乐场.最初复制它的步骤是什么?在github中有一个很好的"fork"按钮,但由于显而易见的原因,我无法使用它.

我如何跟踪fedorahosted repo中的变化到github one?

tro*_*skn 699

  1. 在github上创建一个新的repo.
  2. 将repora从fedorahosted克隆到您的本地计算机.
  3. git remote rename origin upstream
  4. git remote add origin URL_TO_GITHUB_REPO
  5. git push origin master

现在你可以像任何其他github repo一样使用它.要从上游提取补丁,只需运行即可git pull upstream master && git push origin master.

  • @tcaswell技术上没有,但是`origin'指向规范的远程位置是一个强有力的约定.在这种情况下,github位置可能是规范的. (32认同)
  • 没有理由重命名原始原点,只需将其他内容称为新游戏. (10认同)
  • @ThatAintWorking您可能应该打开一个新问题,但简而言之 - 您可以使用命令`git remote add`添加任意数量的遥控器.然后,您可以通过在`git push`中明确声明遥控器来推送其中一个.例如`git push foobar master`将当前分支推送到远程`foobar`上的`master`. (9认同)
  • 也许我应该发布一个不同的问题,但我认为你们正在讨论我想要的东西.我想添加一个新的远程,而不是替换现有的远程,所以我可以继续从旧的存储库中提取,但是将任何更改推送到新的存储库. (3认同)
  • 没有推我所有的分支,只是master。镜像答案适用于所有分支机构 (3认同)
  • 当我这样做并且不使用[mob的答案](/sf/answers/1312632191/)中的 **"--mirror push"** 时,我总是得到`错误:失败将一些参考推送到 'https://github.com/username/testrep.git'` (2认同)
  • 另外......记得恢复标签:“git push --tags” (2认同)

mob*_*mob 100

这个问题有一个删除的答案,它有一个有用的链接: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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 裸克隆和镜像推送简单地添加和推送到另一个遥控器有什么好处?难道所有的分支都会被推,而不只是当前的分支?(如果是这样,这似乎是应该注意的已接受答案的限制.) (5认同)
  • 这正是我所寻找的信息, - bare/--mirror通常是人们想要执行的!这是一个重要的答案!谢谢 (2认同)
  • 伟大的!绝对是主题问题最正确的答案。谢谢!如果我不这样做,我总是会收到`错误:无法将一些引用推送到 'https://github.com/username/testrep.git'` (2认同)

ken*_*orb 40

要将现有的仓库推送到不同的仓库,您需要:

  1. 首先克隆原始回购.

    git clone https://git.fedorahosted.org/cgit/rhq/rhq.git
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将克隆的源推送到新的存储库:

    cd rhq
    git push https://github.com/user/example master:master
    
    Run Code Online (Sandbox Code Playgroud)

你可以master:master改成source:destination分支.


如果要推送特定的提交(分支),那么执行:

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

    git checkout -b new_branch
    
    Run Code Online (Sandbox Code Playgroud)
  2. 选择并重置为您要开始的点:

    git log # Find the interesting hash
    git reset 4b62bdc9087bf33cc01d0462bf16bbf396369c81 --hard
    
    Run Code Online (Sandbox Code Playgroud)

    或者,选择commit by git cherry-pick以附加到现有HEAD中.

  3. 然后推送到您的新回购:

    git push https://github.com/user/example new_branch:master
    
    Run Code Online (Sandbox Code Playgroud)

    如果您正在变基础,请使用-f强制推动(不推荐).运行git reflog以查看更改历史记录.

  • `git push ... old_branch_name:new_branch_name` 允许您将旧存储库中的功能分支作为新存储库中的主分支推送。有用! (2认同)

Hai*_*Dog 13

你真的想简单地将你的本地存储库(带有本地分支机构等)推送到新的遥控器,还是你真的想要在新的遥控器上镜像旧遥控器(及其所有分支,标签等)?如果后者在这里是关于如何正确镜像git存储库的好博客.

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

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

git clone --mirror git@example.com/upstream-repository.git
cd upstream-repository.git
git push --mirror git@example.com/new-location.git
Run Code Online (Sandbox Code Playgroud)


vm3*_*345 10

试试这个如何移动一个完整的Git存储库

  1. 使用以下命令在temp-dir目录中创建本地存储库:

    git clone temp-dir

  2. 进入temp-dir目录.

  3. 要查看ORI中不同分支的列表,请执行以下操作:

    git branch -a
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用以下命令检出要从ORI复制到NEW的所有分支:

    git checkout branch-name
    
    Run Code Online (Sandbox Code Playgroud)
  5. 现在使用以下方法从ORI获取所有标记:

    git fetch --tags
    
    Run Code Online (Sandbox Code Playgroud)
  6. 在执行下一步之前,请确保使用以下命令检查本地标记和分支:

    git tag
    
    
    git branch -a
    
    Run Code Online (Sandbox Code Playgroud)
  7. 现在使用以下命令清除指向ORI存储库的链接:

    git remote rm origin
    
    Run Code Online (Sandbox Code Playgroud)
  8. 现在使用以下命令将本地存储库链接到新创建的新存储库:

    git remote add origin <url to NEW repo>
    
    Run Code Online (Sandbox Code Playgroud)
  9. 现在使用以下命令推送所有分支和标记:

    git push origin --all
    
    
    git push --tags
    
    Run Code Online (Sandbox Code Playgroud)
  10. 您现在拥有ORI回购的完整副本.


小智 10

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

git remote set-url origin [new repo URL]
Run Code Online (Sandbox Code Playgroud)

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

现在,推和拉都与新的 REPO 相关联。

然后像这样正常推送:

git push -u origin master
Run Code Online (Sandbox Code Playgroud)


Kam*_*icz 9

如果您有现有的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
Run Code Online (Sandbox Code Playgroud)

  • 只是为了澄清,--all 将推送所有分支 (4认同)
  • 将本地存储库复制到远程的伟大且非常简单的方法。谢谢。确保远程文件夹存在并带有 git 存储库。我在远程服务器上使用了 git init --bare 。 (2认同)

Mob*_*ent 7

我找到了一个使用set-url的解决方案,它简洁易懂:

  1. 在Github创建一个新的回购
  2. cd 进入本地计算机上的现有存储库(如果尚未克隆它,请先执行此操作)
  3. git remote set-url origin https://github.com/user/example.git
  4. git push -u origin master


Den*_*ova 7

这帮助我将本地项目推送到 git 上的另一个存储库中

 git push https://github.com/yourusername/yourgithubproject.git master:master
Run Code Online (Sandbox Code Playgroud)


upe*_*upe 5

这是一种手动方法git remote set-url origin [new repo URL]

  1. 克隆存储库:git clone <old remote>
  2. 创建 GitHub 存储库
  3. 打开<repository>/.git/config

    $ git config -e
    
    Run Code Online (Sandbox Code Playgroud)
    [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
    
    Run Code Online (Sandbox Code Playgroud)

    并更改远程(url 选项)

    [remote "origin"]
        url = <new remote>
        fetch = +refs/heads/*:refs/remotes/origin/*
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将存储库推送到 GitHub:git push

您还可以使用两个/多个遥控器


小智 5

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

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

  • git rm .git(从本地存储库中删除所有数据)
  • git status(我必须说它与任何错误无关)

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

  • git init启动本地存储库
  • git remote add origin urlrepository.git与远程存储库链接
  • git remote -v确认它已链接到远程存储库

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

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

就是这样!

  • 小心删除 .git 目录。所有数据意味着所有 git 数据,甚至是 git 历史记录。我已经遇到过一些场景,我确实需要检查为什么要做某事。如果删除 .git 目录,这就不再可能了。 (3认同)

gab*_*sch 5

首先,在 Github 上创建您的存储库。然后将目录更改为已签出的源存储库 - 假设您想推送master分支。您需要执行 5 个简单步骤:

git remote add origin2 https://github.com/user/example.git
git checkout master
git pull
git push origin2 master
git remote remove origin2
Run Code Online (Sandbox Code Playgroud)

这将创建本地存储库和新远程存储库之间的链接,签出并拉取源分支(以确保它具有最新的),然后推送当前分支,最后取消本地存储库与远程存储库的链接。

此后您的本地存储库将完好无损,您可以像以前一样使用它。如果需要推送多个分支,请根据需要多次重复checkout- pull-步骤,只需相应更改分支名称即可。push