重命名本地和远程Git存储库的主分支

重命名本地和远程Git存储库的主分支,git,git-branch,git-pull,Git,Git Branch,Git Pull,我有一个分支master,它跟踪远程分支origin/master 我想在本地和远程将它们重命名为master old。这可能吗 对于跟踪origin/master(并且总是通过git pull更新其本地master分支)的其他用户,我重命名远程分支后会发生什么情况? 他们的git pull是否仍然有效,或者它是否会抛出一个无法再找到origin/master的错误 然后,进一步,我想创建一个新的master分支(本地和远程)。同样,在我这样做之后,如果其他用户执行git pull,现在会发生什

我有一个分支
master
,它跟踪远程分支
origin/master

我想在本地和远程将它们重命名为
master old
。这可能吗

对于跟踪
origin/master
(并且总是通过
git pull
更新其本地
master
分支)的其他用户,我重命名远程分支后会发生什么情况?
他们的
git pull
是否仍然有效,或者它是否会抛出一个无法再找到
origin/master
的错误

然后,进一步,我想创建一个新的
master
分支(本地和远程)。同样,在我这样做之后,如果其他用户执行git pull,现在会发生什么


我想这一切都会带来很多麻烦。有没有一个干净的方法来得到我想要的?或者我应该保持原有的
master
,创建一个新分支
master new
,然后继续在那里工作吗?

我假设您仍然询问与您的客户相同的情况。也就是说,“新大师”在其历史中不会包含“旧大师”。*如果您将“新大师”称为“大师”,您将有效地重写历史。不管你如何进入一种状态,在这种状态下,主人不是先前主人位置的后代,只是它处于那种状态

如果其他用户试图在主机不存在的情况下进行拉取,则其拉取操作将失败(remote上没有此类引用),并且一旦主机再次存在于新位置,其拉取操作将不得不尝试将其主机与新的远程主机合并,就像您在存储库中合并了旧主机和新主机一样。考虑到您在这里试图执行的操作,合并将有冲突。(如果它们被解决了,并且结果被推回到存储库中,您将处于更糟糕的状态——两个版本的历史都在那里。)

简单地回答你的问题:你应该接受在你的历史中有时会有错误。这没关系。每个人都会这样。git.git存储库中有还原的提交。重要的是,一旦我们公布了历史,这是每个人都可以信任的


*若确实如此,这将相当于将一些更改推送到master上,然后在原来的位置创建一个新分支。没问题。

最接近重命名的方法是删除,然后在远程服务器上重新创建。例如:

remote="origin"

if [ "$#" -eq 0 ] # if there are no arguments, just quit
then
    echo "Usage: $0 oldName newName or $0 newName" >&2
    exit 1
elif
    [ "$#" -eq 1 ] # if only one argument is given, rename current branch
then
    oldBranchName="$(git branch | grep \* | cut -d ' ' -f2)" #save current branch name
    newBranchName=$1
else
    oldBranchName=$1
    newBranchName=$2
fi

git branch -m $oldBranchName $newBranchName

git push $remote :$oldBranchName # Delete old branch on remote
git push --set-upstream $remote $newBranchName # Add new branch name on remote and track it
git分支-m主节点旧节点
git推送远程:主控#删除主控
git推送远程旧主机#在远程主机上创建旧主机
git checkout-b master some ref#创建一个新的本地master
git推送远程主机#在远程主机上创建主机
然而,这有很多警告。首先,现有签出不会知道重命名-Git不会尝试跟踪分支重命名。如果新的
master
还不存在,git pull将出错。如果已创建新的
主文件
。拉取操作将尝试合并
master
master old
。因此,这通常是一个坏主意,除非您得到了之前签出存储库的所有人的合作

注意:较新版本的Git默认情况下不允许远程删除主分支。您可以通过将远程存储库上的
receive.denyDeleteCurrent
配置值设置为
warn
ignore
来覆盖此设置。否则,如果您准备立即创建新主机,请跳过
git push remote:master
步骤,并将
--force
传递到
git push remote master
步骤。请注意,如果无法更改远程服务器的配置,则无法完全删除主分支

此警告仅适用于当前分支(通常为
master
分支);可以如上所述删除并重新创建任何其他分支。

关于:

git update-ref newref oldref
git update-ref -d oldref newref
git checkout old-branch-name
git push remote-name new-branch-name
git push remote-name :old-branch-name
git branch -m new-branch-name

假设您当前在
主机上

git push origin master:master-old        # 1
git branch master-old origin/master-old  # 2
git reset --hard $new_master_commit      # 3
git push -f origin                       # 4
  • 首先,根据本地存储库中的
    master
    commit,在
    origin
    存储库中创建
    master old
    分支
  • 为此新的
    原始/主旧分支创建新的本地分支(将自动正确设置为跟踪分支)
  • 现在将您的本地
    主机
    指向您希望它指向的提交
  • 最后,强制更改
    origin
    存储库中的
    master
    ,以反映新的本地
    master

  • (如果您以任何其他方式执行此操作,您至少还需要一个步骤来确保
    master old
    已正确设置为跟踪
    origin/master old
    。撰写本文时发布的其他解决方案都不包括这一点。)

    登录服务器,转到Git目录并在裸存储库中重命名分支

    这并不存在与重新加载同一分支相关的所有问题。实际上,“客户端”将自动识别修改后的名称并更改其远程引用

    之后(或之前),您还可以修改分支的本地名称。

    尝试时失败。它抛出一个错误:
    拒绝删除当前分支:refs/heads/master
    。我想我会发布适合我的内容:

    git checkout master             # If not in master already
    
    git branch placeholder          # Create placeholder branch
    git checkout placeholder        # Check out to placeholder
    git push remote placeholder     # Push placeholder to remote repository
    
    git branch -d master            # Remove master in local repository
    git push remote :master         # Remove master from remote repository.
    
    诀窍是在将占位符推送到远程存储库之前签出占位符。其余的是不言自明的;删除主分支并将其推送到远程存储库现在应该可以工作了。摘自


    在使用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
    
    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
    
    doublerename master-new master master-old
    
    # doublerename NEW CURRENT OLD
    #   - arguments are branch names
    #   - see COMMIT_MESSAGE below
    #   - the result is pushed to origin, with upstream tracking info updated
    doublerename() {
      local NEW=$1
      local CUR=$2
      local OLD=$3
      local COMMIT_MESSAGE="Double rename: $NEW -> $CUR -> $OLD.
    
    This commit replaces the contents of '$CUR' with the contents of '$NEW'.
    The old contents of '$CUR' now lives in '$OLD'.
    The name '$NEW' will be deleted.
    
    This way the public history of '$CUR' is not rewritten and clients do not have
    to perform a Rebase Recovery.
    "
    
      git branch --move $CUR $OLD
      git branch --move $NEW $CUR
    
      git checkout $CUR
      git merge -s ours $OLD -m $COMMIT_MESSAGE
    
      git push --set-upstream --atomic origin $OLD $CUR :$NEW
    }
    
    git -m master master-old #rename current master
    git checkout -b master   #create a new branch master
    git push -f origin master #force push to master
    
    git branch -m my_old_branch_name my_new_branch_name
    
    git push origin -u my_new_branch_name
    
    git push origin -D <old_name>
    
    remote="origin"
    
    if [ "$#" -eq 0 ] # if there are no arguments, just quit
    then
        echo "Usage: $0 oldName newName or $0 newName" >&2
        exit 1
    elif
        [ "$#" -eq 1 ] # if only one argument is given, rename current branch
    then
        oldBranchName="$(git branch | grep \* | cut -d ' ' -f2)" #save current branch name
        newBranchName=$1
    else
        oldBranchName=$1
        newBranchName=$2
    fi
    
    git branch -m $oldBranchName $newBranchName
    
    git push $remote :$oldBranchName # Delete old branch on remote
    git push --set-upstream $remote $newBranchName # Add new branch name on remote and track it
    
    git branch -m <old-branch-name> <new-branch-name>
    git fetch origin
    git branch -u origin/<new-branch-name> <new-branch-name>