我有一个分支主机,它跟踪远程分支源/主机。

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

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

接下来,我想创建一个新的主分支(本地和远程)。再次,在我这样做之后,如果其他用户执行git pull,现在会发生什么?

我想这一切都会带来很多麻烦。有没有一种干净的方法可以得到我想要的东西?还是我应该让master保持原样,创建一个新的分支master,然后继续工作?


当前回答

我相信关键是你意识到你正在执行一个双重重命名:从主到主,从主到新。

从所有其他答案中,我综合了这一点:

doublerename master-new master master-old

我们首先要定义doublerename Bash函数:

# 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 rebase,因为分支内容非常不同,但不同之处在于客户端仍然可以安全地使用git pull master快速前进。

其他回答

有很多方法可以重命名分支机构,但我将重点放在更大的问题上:“如何让客户快速前进,而不必在本地处理分支机构”。

首先是一张快速图片:

这实际上很容易做到;但不要滥用它。整个想法取决于合并提交;因为它们允许快进,并将分支的历史与另一分支的历史联系起来。

重命名分支:

# 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
git checkout -b new-branch-name
git push remote-name new-branch-name :old-branch-name

在删除旧分支名称之前,您可能必须手动切换到新分支名称

我相信关键是你意识到你正在执行一个双重重命名:从主到主,从主到新。

从所有其他答案中,我综合了这一点:

doublerename master-new master master-old

我们首先要定义doublerename Bash函数:

# 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 rebase,因为分支内容非常不同,但不同之处在于客户端仍然可以安全地使用git pull master快速前进。

假设您当前在主机上:

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 old已正确设置为跟踪原始/master old。在撰写本文时发布的其他解决方案中均未包含此项。)

关于:

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