Mercurial 等效于此 git:重命名分支并拉取远程分支

Mercurial equivalent of this git: rename branch and pull remote branch

我默认做了一些我不想默认的提交。这是我在 git 中要做的事情,什么是 mercurial 等价物?

git branch -m not_default
git checkout osrf/default
git checkout -b default

这是我尝试过的(但失败了)

hg branch -m not_default (default still exists after this)
hg pull -b default osrf (says no changes)
hg update default (does nothing)

这行不通,我留在了开始的地方。

hg help branch 的最顶部附近,您会看到以下文字:

Note:

    Branch names are permanent and global. Use hg bookmark to create a light-weight bookmark instead. See hg help glossary for more information about named branches and bookmarks.

因此,虽然您可以更改将与 new 提交相关联的 name——包括给新提交一个 branch-name 以前从未出现过,因此创建了一个 新分支 — 您永远不能更改现有提交的分支名称。 编辑,您通常不需要在 Mercurial 中执行此操作。 Mercurial 断言您从不 需要这样做并且只是不允许它。

这与 Git 非常不同,后者的 "branches"(分支名称)更像是 Mercurial 书签。 编辑:Git中的提交同时在许多个分支上,每个分支名称用于标识提示 thus-named 分支的 commit1,以及 reachability 确定哪些提交包含在哪些分支中。 (如果包含 Git 提交的分支数量下降到 ,但是,提交 可能 容易受到垃圾收集的影响,具体取决于关于它是否有一些非 branch-name 名称可以找到它。)Mercurial 中的提交总是在一个分支上。一枝独秀,一往无前。

(从技术上讲,可以通过复制 Mercurial 存储库并使用 Convert Extension to re-map branch names. The Evolve Extension is also supposed to be able to do it 来重命名分支,但我没有测试过。)


1提示 commit 是一个 Git 概念,没有 one-to-one 映射到 Mercurial。 Mercurial head 是没有 children 的提交,而在 Git 中,分支提示 often 没有children,但是 required 没有 children 的分支提示提交。在 Mercurial 和 Git 中,当当前提交为 X 时创建一个新提交会创建一个新的 child of X — 但在 Git 中,无论你在哪个分支 "on",如果你在任何分支上,该分支的名称都会 重写 以指向 new commit,它——因为它是新的并且没有 children——现在 相当于 Mercurial head。

您什么都不用做。 Mercurial 的分支模型与 Git 的完全不同。

Git 需要分支名称,不仅要命名分支,还要让它们保持活动状态(这样它们就不会被垃圾收集)。

在 Mercurial 中,您可以返回到原始提交,在那里执行 hg update 并在该点进行下一次提交。这会将您的新提交保留为 "anonymous branch"(参见 hg help glossary)。从那里开始,额外的提交将在一个新的匿名分支上进行。

您可以使用书签(hg help bookmark)以类似Git的方式为分支的尖端命名,但严格来说,这不是必需的。


原则上,您不必在 Mercurial 中命名分支或提交。分支和书签是为修订图 1 提供结构的便利功能。您唯一 需要 的是 hg update 将修订历史移动到不同的提交,然后您可以在修订图中的那个点添加新提交。

如果你在 Git 中这样做,你将收到 "detached HEAD" 警告,因为这可能会导致数据丢失,但对于 Mercurial 这不是问题,这就是关键区别所在是:在 Git 中,你的命令必须保留每个提交都可以从 "ref" 访问的不变性,这样它就不会被垃圾收集; Mercurial 没有垃圾收集,所以这个不变量是无关紧要的,你可以随意移动历史记录。

1:命名分支允许您永久地为一组提交命名。书签允许您为分支的尖端赋予符号名称,这也不是永久性的。