从根本上来说,分支名称就像小便签标签一样。
您可以完全删除标签:
git branch -d foo
This finds the label with the word "foo" on it (this label is stuck on one particular commit) and peels that label off and throws it (the label, again) away.1
或者,您可以擦除标签上的名称并在上面写一个新名称:
git branch -m old new
该标签仍然停留在同一个提交上,但现在它有不同的名称。
或者,您可以将标签从一个提交移动到另一个提交:
git branch -f moved 1f0c9447
这里的最后一个参数是原始提交 SHA-1,但您可以使用任何可以定位某个提交的内容,例如另一个分支名称。
There are“删除并重新创建”、“重命名”和“移动”之间存在一些重要区别,因为这些标签还保留历史记录,其中记录了它们在上次移动之前命名的提交。如果完全删除标签,历史记录也会消失。如果您重命名标签,它会保留其历史记录,如果您移动标签,它会获取一个新的历史记录条目,因为您将其从一个提交移动到另一个提交。
当您进行新提交时,无论您所在的分支是什么,该分支标签都会移动自动地,指向新的提交。这一标签举动被载入史册。
使用最有意义的选项(您想要保留或增强历史记录,还是只是希望将其丢弃?)。
(请注意,当您创建新分支时:
git branch new
您可以提供原始提交 SHA-1,或任何找到提交的内容。所以git branch new existing
将建立新的分支new
指向相同的提交existing
.)
分支历史记录保存在每个分支的"reflog" https://www.kernel.org/pub/software/scm/git/docs/git-reflog.html。有一个参考日志HEAD
还有,以及git reflog
向你展示那个,但是git reflog show foo
显示历史记录以供参考(通常是分支)foo
.
This history is normally kept for 90 days.2 Furthermore, it's purely local: it does not get copied on git clone
and git fetch
, nor pushed with git push
.
顺便说一句,标签几乎与分支完全相同,除了“标签”一词之外,还有两个很大的区别:它们永远不会supposed移动(并且不会自动移动),并且它们不记录历史(因为它们不应该need一)。 “远程分支”,又名远程跟踪分支,是这两者的交叉。You不要移动它们,甚至不要通过提交来移动它们,但是它们domove:当你的 git 联系远程并获取新的提交时,它还会获取分支标签移动,并更新你的远程跟踪分支及其引用日志。
1You can do this at any time with any label (with -D
, uppercase), but if you remove the last label that finds some commit(s), those commits go invisible—you won't see them in gitk --all
displays any more, for instance—and eventually (after about a month) they will get garbage-collected.
2As usual with git, it's actually a lot more complicated than that: there are two different reflog expiry values, one for "reachable" objects and one for "unreachable" objects. The 90 day default is for reachable objects; unreachable objects default to 30 days; and both values can be configured.
此外,特别stash
参考(由git stash https://www.kernel.org/pub/software/scm/git/docs/git-stash.html)的默认过期设置为“从不”,这样存储就不会过期。