【发布时间】:2011-07-08 02:23:37
【问题描述】:
假设我在git.fedorahosted.org 上有一个存储库,我想将其克隆到我在 github 的帐户中,以便在 fedorahosted 上更“官方”的存储库之外拥有自己的游乐场。 最初复制它的步骤是什么? 在 github 中有一个不错的“fork”按钮,但由于明显的原因我不能使用它。
我如何将 fedorahosted 存储库中的更改跟踪到 github 中?
【问题讨论】:
假设我在git.fedorahosted.org 上有一个存储库,我想将其克隆到我在 github 的帐户中,以便在 fedorahosted 上更“官方”的存储库之外拥有自己的游乐场。 最初复制它的步骤是什么? 在 github 中有一个不错的“fork”按钮,但由于明显的原因我不能使用它。
我如何将 fedorahosted 存储库中的更改跟踪到 github 中?
【问题讨论】:
git remote rename origin upstreamgit remote add origin URL_TO_GITHUB_REPOgit push origin master现在您可以像使用任何其他 github 存储库一样使用它。要从上游拉取补丁,只需运行git pull upstream master && git push origin master。
【讨论】:
origin 指向规范的远程位置是一个强有力的约定。在这种情况下,github 位置可能是规范的。
git remote add 添加任意数量的遥控器。然后,您可以通过在git push 中明确声明远程来推送到其中一个。例如。 git push foobar master 将当前分支推送到远程master 上的foobar。
关于这个问题的已删除答案有一个有用的链接: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
【讨论】:
error: failed to push some refs to 'https://github.com/username/testrep.git'
要将现有的 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 允许您将旧存储库中的功能分支推送为新存储库中的主分支。有用!
如果您有现有的 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
【讨论】:
您真的想简单地将本地存储库(及其本地分支等)推送到新远程,还是真的想在新远程上镜像旧远程(及其所有分支、标签等) ?如果后者是 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
【讨论】:
我找到了一个使用 set-url 的解决方案,它简洁且相当容易理解:
cd 到您本地机器上的现有存储库中(如果您尚未克隆它,请先执行此操作)git remote set-url origin https://github.com/user/example.gitgit push -u origin master【讨论】:
试试这个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 存储库的完整副本。
【讨论】:
只需使用以下命令更改 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
【讨论】:
这里是手动操作git remote set-url origin [new repo URL]:
git clone <old remote>
打开<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
并更改遥控器(url 选项)
[remote "origin"]
url = <new remote>
fetch = +refs/heads/*:refs/remotes/origin/*
将存储库推送到 GitHub:git push
你也可以同时使用/multiple remotes。
【讨论】:
我也遇到了同样的问题。
就我而言,由于我的本地机器中有原始存储库,因此我在没有任何隐藏文件(.git、.gitignore)的新文件夹中制作了一个副本。
最后我将 .gitignore 文件添加到新创建的文件夹中。
然后我从本地路径创建并添加了新的存储库(在我的例子中使用 GitHub Desktop)。
【讨论】:
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 remote add origin https://github.com/AyadiAkrem/teachandgo.git
git branch -M main
git push -u origin main
【讨论】:
这有助于我将本地项目推送到 git 上的不同 repo 中
git push https://github.com/yourusername/yourgithubproject.git master:master
【讨论】: