我有分支master
追踪远程分支origin/master
。 git
我想将它们重命名为本地和远程master-old
。 那可能吗? 对于跟踪origin/master
(而且老是经过git pull
更新其本地master
git pull
)的其余用户,重命名远程分支后会发生什么? 他们的git pull
仍然能够工做仍是会抛出错误,致使找不到origin/master
了? bash
而后,进一步,我想建立一个新的master
分支(本地和远程)。 一样,在我完成此操做以后,若是其余用户执行git pull
会发生什么呢? 服务器
我想全部这些都会带来不少麻烦。 有没有一种干净的方法来获得我想要的东西? 仍是我应该只保留master
并建立一个新的master-new
分支,而后继续工做? ide
我假设您仍在询问与上一个问题相同的状况。 也就是说,“ master-new”将在其历史记录中不包含“ master-old”。*若是您将master-new称为“ master”,则将有效地重写了历史记录。 没关系,你如何进入,其中主不主的前面位置的后裔,只是它是在该州的状态。 this
其余尝试在主服务器不存在的状况下进行拉取的用户只会使其拉取失败(在远程上没有此类引用),一旦再次存在于新位置,他们的拉取将不得不尝试将其主服务器与新的远程主服务器合并,就像您在存储库中合并了master-new和master-new同样。 鉴于您要在此处执行的操做,合并将产生冲突。 (若是解决了这些问题,而且将结果推回到了存储库中,那么您将处于更加糟糕的状态-那里的两个版本的历史记录。) spa
简单地回答您的问题:您应该接受的是,您的历史记录有时会出现错误。 不要紧 它发生在每一个人身上。 git.git存储库中有还原的提交。 重要的是,一旦咱们发布了历史,每一个人均可以信任它。 code
*若是这样作的话,这等效于将一些更改推送到master上,而后在之前的位置建立一个新分支。 没问题。 rem
与重命名最接近的是删除,而后在远程上从新建立。 例如: get
git branch -m master master-old git push remote :master # delete master git push remote master-old # create master-old on remote git checkout -b master some-ref # create a new local master git push remote master # create master on remote
可是,这有不少警告。 首先,没有现成的检出会了解重命名- Git 并不试图跟踪分支重命名。 若是新的master
服务器不存在,则git pull将出错。 是否建立了新的master
。 拉将尝试合并master
和master-old
。 所以,除非您与先前签出了存储库的全部人进行合做,不然一般这是一个坏主意。 it
注意:默认状况下,较新版本的git不容许您远程删除master分支。 您能够经过将receive.denyDeleteCurrent
配置值设置为warn
或ignore
远程存储库来覆盖此设置。 不然,若是您准备当即建立新的master,则跳过git push remote :master
步骤,并将--force
传递给git push remote master
步骤。 请注意,若是您没法更改遥控器的配置,则将没法彻底删除master分支!
此警告仅适用于当前分支(一般是master
分支); 能够如上所述删除和从新建立任何其余分支。
对于Git v1.7,我认为这已经发生了一些变化。 如今,将本地分支机构的跟踪参考更新为新的遥控器很是容易。
git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
重命名分支有不少方法,但我将着重解决更大的问题: “如何让客户快速前进,而没必要在本地弄乱分支” 。
这实际上很容易作到; 但不要滥用它。 整个想法取决于合并提交。 由于它们容许快速前进,而且将分支的历史连接到另外一个。
# rename the branch "master" to "master-old" # this works even if you are on branch "master" git branch -m master master-old
# create master from new starting point git branch master <new-master-start-point>
# now we've got to fix the new branch... git checkout master # ... by doing a merge commit that obsoletes # "master-old" hence the "ours" strategy. git merge -s ours master-old
git push origin master
之因此可行,是由于建立merge
提交容许将分支快速转发到新修订版。
renamed branch "master" to "master-old" and use commit ba2f9cc as new "master" -- this is done by doing a merge commit with "ours" strategy which obsoletes the branch. these are the steps I did: git branch -m master master-old git branch master ba2f9cc git checkout master git merge -s ours master-old
git update-ref newref oldref git update-ref -d oldref newref