将现有的代码仓库推送到一个新的不同的远程仓库服务器,如何操作Git?

718

假设我在 git.fedorahosted.org 上有一个代码库,我想将其克隆到我的github帐户中,以便于在fedorahosted上的官方代码之外拥有自己的“游乐场”。 最初复制这个仓库需要哪些步骤呢? 在github中有这个不错的“fork”按钮,但出于明显的原因我无法使用它。

如何跟踪fedorahosted代码库的更改并同步到github的代码库中?

16个回答

5

将本地仓库链接到其他远程仓库

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

就是这样了!


2
注意不要删除.git目录。所有数据意味着所有的git数据,甚至是git历史记录。我已经遇到过一些情况,在这些情况下我真的需要检查为什么做了某件事。如果删除了.git目录,则将无法再进行检查。 - Manuel

5

这里有一种手动的方法来执行 git remote set-url origin [新仓库URL]:

  1. Clone the repository: git clone <old remote>
  2. Create a GitHub repository
  3. 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/*
    
  4. Push the repository to GitHub: git push

您还可以使用两个或多个远程仓库/多个远程仓库


3

从命令行推送现有仓库

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

3
我实现这个的方式是:
  1. 在github上创建一个新的存储库(new-repo.git)
  2. 在本地机器上运行cd old-repo/ 命令并获取所有新更改
  3. 运行 git push -u https://github.com/[用户名]/new-repo.git main -f
  4. 将新的远程存储库https://github.com/[用户名]/new-repo.git克隆到本地环境中
我发现这是一种简单的方法,可以将旧的远程存储库复制到新的远程存储库。

2

我也遇到了同样的问题。

在我的情况下,由于我在本地机器上拥有原始存储库,我已经将其复制到一个新文件夹中,没有任何隐藏文件(.git,.gitignore)。

最后,我向新创建的文件夹添加了.gitignore文件。

然后,我使用本地路径创建并添加了新的存储库(在我的情况下使用GitHub Desktop)。


2
Visual Studio 2022和默认的Git扩展无需任何命令即可完美运行。
步骤1:进入Git设置。

enter image description here

步骤2:在git / azure中添加指向不同存储库的新起点。

enter image description here enter image description here

步骤三:现在您可以选择将其推送到Git/Azure中不同存储库的新起点。

enter image description here

现在有一个新的分支在新存储库中 在此输入图像描述

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接