切换到Git中的其他存储库

时间:2013-06-12 08:52:44

标签: git github

Git的一个我仍然感到困惑的情况是:

$ git clone https://github.com/dude1/project

哎呀,那不是真正合适的版本。我会切换:

$ git remote add dude2 https://github.com/dude2/project
$ git fetch dude2
$ git checkout dude2/master

Note: checking out 'dude2/master'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at f3o845a... 

槽糕。我希望master引用dude2 / master。

$ git checkout -b master dude2/master

fatal: A branch named 'master' already exists.

如果没有删除整个目录并重新开始,我该如何干净地切换存储库?

2 个答案:

答案 0 :(得分:3)

在git中你不应该在远程分支上提交工作(你应该在本地分支上工作),这就是为什么当你{'detached head' state时你被放入checkout dude2/master {1}}。远程分支应包含来自远程的提交副本,而不是本地创建的提交。

正如jthill建议的那样,强制切换master意味着的最佳方法是

git checkout -B master dude2/master

输出应包括

Branch master set up to track remote branch master from dude2.

表示您的主人正在跟踪其他远程主控的更改。

答案 1 :(得分:0)

通过思考,明显的答案是:

首先git checkout dude2/master如上所述。然后:

$ git branch -d master
warning: deleting branch 'master' that has been merged to
     'refs/remotes/dude1/master', but not yet merged to HEAD.
Deleted branch master (was f30845a).

$ git checkout -b master dude2/master
Branch master set up to track remote branch master from dude2.
Switched to a new branch 'dude2'

我不知道删除母版时的警告是什么意思。