假设我在 git.fedorahosted.org 上有一个代码库,我想将其克隆到我的github帐户中,以便于在fedorahosted上的官方代码之外拥有自己的“游乐场”。 最初复制这个仓库需要哪些步骤呢? 在github中有这个不错的“fork”按钮,但出于明显的原因我无法使用它。
如何跟踪fedorahosted代码库的更改并同步到github的代码库中?
假设我在 git.fedorahosted.org 上有一个代码库,我想将其克隆到我的github帐户中,以便于在fedorahosted上的官方代码之外拥有自己的“游乐场”。 最初复制这个仓库需要哪些步骤呢? 在github中有这个不错的“fork”按钮,但出于明显的原因我无法使用它。
如何跟踪fedorahosted代码库的更改并同步到github的代码库中?
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
。
GitHub最近将master
分支更名为main
,所以(根据你的分支是master
还是main
)在第5步中,你可能需要使用git push origin main
,而对于从上游拉取补丁,使用git pull upstream main && git push origin main
,否则你将收到错误消息。
这个问题中有一个被删除的回答,其中包含一个有用的链接: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
在你的本地计算机上。$ 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
首先克隆原始仓库。
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
的作用是将一个特性分支从旧仓库推送到新仓库,并将其作为主分支。非常有用! - gorjanz如果您已经有现有的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
我发现使用set-url这个解决方案既简洁易懂:
cd
进入您本地计算机上的现有仓库(如果您尚未克隆,则需先进行此操作)git remote set-url origin https://github.com/user/example.git
git push -u origin master
你是否真的只想将本地仓库(包括本地分支等)推送到新的远程仓库,还是你真的想在新的远程仓库上镜像旧的远程仓库(包括所有分支、标签等)?如果是后者,这篇博客会给你提供非常好的指导:如何正确地镜像一个Git仓库。
我强烈建议你阅读这篇博客以获取一些非常重要的细节,但简要概述如下:
在一个新的目录中运行以下命令:
git clone --mirror git@example.com/upstream-repository.git
cd upstream-repository.git
git push --mirror git@example.com/new-location.git
尝试这个:如何移动完整的Git存储库
使用以下命令在temp-dir目录中创建本地存储库:
git clone temp-dir
进入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
现在使用以下命令将您的本地存储库链接到新创建的NEW存储库:
git remote add origin <url to NEW repo>
现在使用以下命令推送您的所有分支和标签: 现在使用以下命令推送您的所有分支和标签:
git push origin --all
git push --tags
您现在拥有来自您的ORI仓库的完整副本。
只需使用此命令更改GIT存储库URL,即可指向新的存储库:
git remote set-url origin [new repo URL]
示例: git remote set-url origin git@bitbucket.org:Batman/batmanRepoName.git
现在,推送和拉取将链接到新的存储库。
然后像这样正常推送:
git push -u origin master
这帮助我将本地项目推送到Git的另一个存储库。
git push https://github.com/yourusername/yourgithubproject.git master:master
首先,在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
这将创建您的本地repo与新远程之间的链接,检出并拉取源分支(以确保它是最新的),然后推送当前分支,最后取消本地repo与远程之间的链接。
此操作完成后,您的本地repo将完好无损,可以像以前一样使用。如果您需要推送多个分支,请根据需要重复checkout
-pull
-push
步骤,并相应更改分支名称。
origin
是指向官方远程位置的强烈约定。在这种情况下,github 应该是官方位置。 - troelskngit remote add
添加任意数量的远程地址。然后,您可以通过在git push
时显式声明远程地址来将内容推送到其中一个远程地址。例如,使用命令git push foobar master
将当前分支推送到名为foobar
的远程地址上的master
分支。 - troelskn