没有自动提交的Git合并

本文讨论如何在Git中执行合并操作但不自动创建提交。通过使用`git merge --no-commit`,可以合并分支而不立即提交。这使得用户有机会在实际提交之前审查和修改合并结果。此外,还提到了`git merge --squash`作为另一种合并多个提交为一个提交的方法。在某些情况下,如果Git执行的是快速前进合并,可能需要手动管理提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Git merge without auto commit

Is it possible to do a git merge , but without a commit? 是否有可能进行git merge ,但没有提交?

"man git merge" says this: “man git merge”说:

With --no-commit perform the merge but pretend the merge failed and do not autocommit,
to give the user a chance to inspect and further tweak the merge result before
committing.

But when I try to use git merge with the --no-commit it still auto-commits. 但是当我尝试将git merge--no-commit它仍然会自动提交。 Here's what I did: 这是我做的:

$> ~/git/testrepo$ git checkout master
Switched to branch 'master'

$> ~/git/testrepo$ git branch
* master
  v1.0

$> ~/git/testrepo$ git merge --no-commit v1.0
Updating c0c9fd2..18fa02c
Fast-forward
 file1 |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

$> ~/git/testrepo$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

A subsequent git log reveals all the commits from the v1.0 branch merged into master. 随后的git log显示v1.0分支中的所有提交都合并到master中。


#1楼

参考:https://stackoom.com/question/aFt9/没有自动提交的Git合并


#2楼

If you only want to commit all the changes in one commit as if you typed yourself, --squash will do too 如果您只想在一次提交中提交所有更改,就像您自己键入一样,--squash也会这样做

$ git merge --squash v1.0
$ git commit

#3楼

I prefer this way so I don't need to remember any rare parameters. 我更喜欢这种方式,因此我不需要记住任何罕见的参数。

git merge branch_name

It will then say your branch is ahead by # commits, you can now pop these commits off and put them into the working changes with the following: 然后它会通过#commit声明你的分支领先,你现在可以弹出这些提交并将它们放入工作更改中,具体如下:

git reset @~#

For example if after the merge it is 1 commit ahead, use: 例如,如果在合并之后提前1次提交,请使用:

git reset @~1

#4楼

当只在分支中有一个提交时,我通常会这样做

git merge branch_name --ff

#5楼

You're misunderstanding the meaning of the merge here. 你在这里误解了合并的含义。

The --no-commit prevents the MERGE COMMIT to occur, and that only happens when you merge two divergent branch histories; --no-commit阻止MERGE COMMIT发生,并且仅在合并两个不同的分支历史时发生; in your example that's not the case since Git indicates that it was a "fast-forward" merge and then Git only applies the commits already present on the branch sequentially. 在你的例子中并非如此,因为Git表明它是一个“快进”合并,然后Git只按顺序应用已经存在于分支上的提交。


#6楼

Note the output while doing the merge - it is saying Fast Forward 在进行合并时请注意输出 - 它表示Fast Forward

In such situations, you want to do: 在这种情况下,你想做:

git merge v1.0 --no-commit --no-ff
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值