[英]New branches get all previous commits from copied branch
if I create a new branch all the commits from the copied branch are coppied to the new one. 如果我创建一个新分支,则来自复制分支的所有提交都将被复制到新分支。
How can I stop this? 我该如何阻止呢? I would like to have just new commits in the new branch..
我想在新分支中添加新提交。
Commits in git contain a reference on their parent commit. git中的提交包含对其父提交的引用。 This is how git builds the history of your repository.
这就是git生成存储库历史的方式。
A branch on the other hand just points to a specific commit. 另一方面,分支仅指向特定的提交。 It is literally a file which contains the SHA-1 key of the current commit.
它实际上是一个包含当前提交的SHA-1密钥的文件。 You can find them in the
.git/refs/heads
folder in your repository. 您可以在存储库的
.git/refs/heads
文件夹中找到它们。
When you create a new branch without specifying a commit the branch will point to the same commit HEAD
currently points at. 当您创建新分支而不指定提交时,该分支将指向
HEAD
当前指向的同一提交。 (Note that HEAD
doesn't directly point at a commit but at a branch, but that's a different story.) (请注意,
HEAD
并不直接指向提交,而是指向分支,但这是另一回事。)
Let's assume you are currently on your development
branch and want to create a new branch to work on a new feature. 假设您当前在
development
分支上,并且想要创建一个新分支来使用新功能。
git checkout -b new-branch
Now both development
and new-branch
will point at the same commit, lets say the key is DEF456
. 现在,
development
和new-branch
将指向同一提交,可以说密钥为DEF456
。 This commit contains a reference to it's parent commit - as every commit does - which is ABC123
which in turn again contains a reference on it's parent commit and so on. 此提交包含对其父提交的引用(就像每次提交一样),即
ABC123
,它又包含对其父提交的引用,依此类推。
You see, git doesn't copy the history of a branch when creating a new branch, it just points to the same commit as the current branch does. 您会看到,git在创建新分支时不会复制分支的历史记录,它只是指向与当前分支相同的提交。
If you want to learn more about how git stores it's information internally you can take a look at the Git Objects chapter of the gitpro book . 如果您想了解有关git如何在内部存储其信息的更多信息,可以查看gitpro书的Git Objects一章 。
And as you already found in this question . 正如您已经在此问题中发现的那样。 You can force a merge commit, rather than a fast-forward, by using
git merge --no-ff <branch-to-merge>
. 您可以使用
git merge --no-ff <branch-to-merge>
强制执行合并提交,而不是快进。
A merge commit is special in the sense that it doesn't contain one refence on a parent commit, as most commits in git do, but it contains multiple references to it's parent commits. 合并提交在某种意义上是特殊的,因为它不像git中的大多数提交那样包含对父提交的引用,但是它包含对其父提交的多个引用。 This defines a merge.
这定义了合并。
It sounds like you were really asking about forcing "real" merges rather than "fast forward" merges, and about "squash" merges (which are not actual merges at all). 听起来您确实在问要强制执行“真实”合并而不是“快进”合并,以及有关“壁球”合并(根本不是实际合并)。 For completeness, though, I want to mention that there's one more thing you can do when creating a new branch-name:
但是,为了完整起见,我想提到的是,在创建新的分支名称时,您还可以做其他事情:
git checkout --orphan -b newbr
In this case, git puts you on newbr
without actually creating newbr
yet. 在这种情况下,git会让您使用
newbr
而实际上尚未创建newbr
。 This means that the next commit will start a new, independent commit-graph within your repository. 这意味着下一次提交将在您的存储库中启动一个新的独立提交图。 In other words, you will have multiple "root commits" in a single repository (a root commit is a commit that has no parents, vs ordinary commits that have one parent and merge commits that have two or more parents).
换句话说,您将在一个存储库中拥有多个“根提交”(根提交是没有父项的提交,而具有一个父项的普通提交和具有两个或多个父级的合并提交)。
Multiple independent commit graphs within a repository are unusual, but supported: git's own source repository contains them, for instance. 一个存储库中有多个独立的提交图是不常见的,但是受支持:例如git自己的源存储库就包含了它们。 (The git program is maintained using git.)
(git程序使用git维护。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.