简体   繁体   English

git/Bitbucket - 如何在不更改 HEAD 的情况下将新分支合并到 master 中?

[英]git/Bitbucket - How to merge new branch into master without changing HEAD?

I'm trying to merge a newly created branch (from master branch) into master.我正在尝试将一个新创建的分支(从 master 分支)合并到 master 中。 However, when merging git HEAD points to master and also the newly merged branch and the master branch is identifiable by this newly created branch rather than just master by itself.但是,当合并 git HEAD 指向 master 时,新合并的分支和 master 分支可由这个新创建的分支识别,而不仅仅是 master 本身。

As an example:举个例子:

  • From master create new branch Feature_A从 master 创建新分支 Feature_A
  • Add and remove some files from Feature_A, also commit and push these changes to Feature_A从 Feature_A 添加和删除一些文件,同时提交并将这些更改推送到 Feature_A
  • Want to merge Feature_A with master想要将 Feature_A 与 master 合并

I took the steps of doing:我采取了以下步骤:

  • git checkout master git 结账师傅
  • git merge Feature_A git合并Feature_A

However, in doing these steps HEAD points to -> (HEAD -> master, origin/master, origin/HEAD, origin/Feature_A, Feature_A).然而,在执行这些步骤时,HEAD 指向 -> (HEAD -> master, origin/master, origin/HEAD, origin/Feature_A, Feature_A)。

Because of this it seems like this repo is now identifiable by Feature_A rather than master.因此,似乎这个回购协议现在可以由 Feature_A 而不是 master 识别。 So my question is how can I avoid this and rather merge the changes from Feature_A into master completely such that it doesn't point the HEAD to the merged branch?所以我的问题是如何避免这种情况,而是将 Feature_A 中的更改完全合并到 master 中,这样它就不会将 HEAD 指向合并的分支?

(I'm also using Bitbucket, but CLI/VScode if this helps, and there is no "MERGE" badge after doing this). (我也在使用 Bitbucket,但如果这有帮助,则使用 CLI/VScode,并且执行此操作后没有“MERGE”徽章)。

I think you are in a situation like this before trying to merge:我认为您在尝试合并之前处于这样的情况:

* ffffff (HEAD, feature_a, origin/feature_a) Some more work on feature A
* eeeeee Some work on feature A
* dddddd (master, origin/master) Some previous work that was merged into master
* ...

I'm trying to describe here that you created a new branch from master and then committed some work on that branch.我在这里试图描述您从 master 创建了一个新分支,然后在该分支上提交了一些工作。 But the critical thing is that you didn't make any commits on master or merge other branches into master (which amounts to the same thing as makign commits on master as far as git is concerned).但关键是您没有在 master 上进行任何提交或将其他分支合并到 master 中(就 git 而言,这与在 master 上进行 makign 提交是一样的)。

In this case, when you do git checkout master and git merge feature_ , git will perform a so-called fast-forward merge.在这种情况下,当您执行git checkout mastergit merge feature_时,git 将执行所谓的快进合并。 (Read the output after the merge to confirm this. It says exactly what it is doing.) This means that master simply moves to the same commit as feature_a , but no merge commit is created. (合并后阅读 output 以确认这一点。它准确说明了它在做什么。)这意味着master只是移动到与feature_a相同的提交,但没有创建合并提交。 If you want a merge request, there is a flag to do so when you do git merge .如果你想要一个合并请求,当你执行git merge时有一个标志可以这样做。 Check out the documentation for details.查看文档了解详细信息。

A standard (non-fast-forward) git merge creates a merge commit, where the previous tips of both branches become the direct ancestors of the merge commit.标准(非快进)git 合并创建合并提交,其中两个分支的先前提示成为合并提交的直接祖先。 This is the fundamental definition of a merge.这是合并的基本定义。 That merge commit appears on the branch that was the target of the merge, and once you push that branch it also appears upstream.该合并提交出现在作为合并目标的分支上,一旦您推送该分支,它也会出现在上游。

Here's an example of a git commit graph for a merge I had laying around:这是我放置的合并的 git 提交图示例:

*   ff72a376 - (HEAD -> develop, origin/develop) Merge pull request #463 'issue469' into develop (2022-11-15 12:29:46 -0800) <bonachea>
|\  
| * 6cf6ccaa - (fork/issue469, issue469) ... (2022-11-15 12:29:23 -0800) <bonachea>
| * f40dc706 - ... (2022-11-15 12:29:23 -0800) <bonachea>
|/  
*   cecb516c - Merge pull request #462 into develop (2022-11-10 14:16:21 -0500) <bonachea>

This is right after a merge of my local branch issue469 (which tracks upstream fork/issue469 ) into develop (ie git checkout develop; git merge --no-ff issue469 ) and I've then pushed develop upstream to origin/develop ( git push origin develop ).这是在我的本地分支issue469 (跟踪上游fork/issue469 )合并到develop (即git checkout develop; git merge --no-ff issue469 )之后,然后我将develop推到origin/developgit push origin develop )。 Note the merge commit is a child of the tip of the issue469 branch, and the previous tip of develop ( cecb516c ).请注意,合并提交是issue469分支提示和develop ( cecb516c ) 之前提示的子项。 However the issue469 branch (local or upstream) does not contain the merge commit, unless I take further actions to make that happen.但是, issue469分支(本地或上游)不包含合并提交,除非我采取进一步的措施来实现这一点。 Ie: IE:

$ git describe --always --exclude '*' HEAD
ff72a376
$ git describe --always --exclude '*' develop
ff72a376
$ git describe --always --exclude '*' origin/develop
ff72a376
$ git describe --always --exclude '*' issue469
6cf6ccaa
$ git describe --always --exclude '*' fork/issue469
6cf6ccaa

HEAD is a local marker that always tracks the current state of my working directory, which right now corresponds to the tip of develop == origin/develop , but as soon as I make more commits or checkout another branch it moves appropriately. HEAD是一个本地标记,它始终跟踪我工作目录的当前 state,它现在对应于develop == origin/develop的提示,但是一旦我进行更多提交或检查另一个分支,它就会适当移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM