简体   繁体   English

git 合并 --continue with --no-commit

[英]git merge --continue with --no-commit

Is there a workaround for not committing with command "git merge --continue"是否有不使用命令“git merge --continue”提交的解决方法

I am trying to do auto-merge with command # git merge -q --no-commit --no-ff --no-edit origin/myBranch --progress -m "Auto merge" - This works if there is no conflict.我正在尝试使用命令进行自动合并# git merge -q --no-commit --no-ff --no-edit origin/myBranch --progress -m "Auto merge" - 如果没有冲突,这会起作用.

However when there is a conflict on a specific folder, which I want to exclude from merge ( git reset folderName ) and then issuing a command that # git merge --continue with --no-commit但是,当特定文件夹发生冲突时,我想将其从合并中排除( git reset folderName文件夹名称),然后发出命令# git merge --continue with --no-commit

git merge --continue takes no more arguments like --no-commit ( fatal: --continue expects no arguments ) git merge --continue需要 arguments 就像 --no-commit ( fatal: --continue expects no arguments

git merge --continue makes automatic commit and results in below. git merge --continue进行自动提交,结果如下。

git status

    On branch program/XYZ
    Your branch is ahead of 'origin/XYZ' by 10 commits.
      (use "git push" to publish your local commits)

You cannot get what you are asking for, because the only way to merge is to commit.你得不到你想要的,因为合并的唯一方法就是提交。 1 1个

To see why this is the case, remember what a commit is and does:要了解为什么会这样,请记住提交是什么以及它做什么:

  • Each commit is numbered.每个提交都有编号。 These aren't simple counting numbers—they don't go 1, 2, 3, etc.—but they are unique: each commit has a hash ID that no other commit may have.这些不是简单的计数——它们不是 go 1、2、3 等——但它们是唯一的:每个提交都有一个hash ID ,其他提交可能没有。

  • Each commit stores two things: a full snapshot of all the files that Git knows about, and some metadata such as who made the commit, when, and why (the log message).每个提交存储两件事:Git 知道的所有文件的完整快照,以及一些元数据,例如提交人、提交时间和原因(日志消息)。 In the metadata, Git stores the commit-number of this commit's parent or parents .在元数据中, Git 存储此提交的父项或 parents的提交号。

This last part—the parents of a commit—is how history works, in Git. A branch name like master simply holds the hash ID of the last commit in the branch:最后一部分(提交的级)是历史的工作方式,在 Git 中。像master这样的分支名称只包含分支中最后一次提交的 hash ID:

... <-F <-G <-H   <-- master

Git can read this hash ID from the name master , and use that to locate commit H in its big database of all-Git-objects. Git 可以从名称master读取这个 hash ID,并使用它在它的所有 Git 对象的大数据库中定位提交H Having read commit H from this database, Git finds the hash ID of previous commit G .从该数据库读取提交H后, Git 找到先前提交G的 hash ID。 That allows Git to read out commit G , which contains the hash ID of earlier commit F , which allows Git to read commit F , and so on.这允许 Git 读取提交G ,其中包含早期提交F的 hash ID,这允许 Git 读取提交F ,等等。 Presto: there is the history. Presto:有历史。 Meanwhile each commit holds a full snapshot of every file, so by comparing the contents of the files in G to the contents of the files in H , Git can tell you what changed between G and H .同时,每次提交都会保存每个文件的完整快照,因此通过比较G中文件的内容与H中文件的内容,Git 可以告诉您GH之间发生了什么变化

This handles simple linear chains, but what about merges?这处理简单的线性链,但是合并呢? Merges, in Git, are achieved through merge commits . Git 中的合并是通过合并提交实现的。 A merge commit is defined as any commit that has at least two parent commits.合并提交定义为具有至少两个父提交的任何提交。

Suppose we have the following:假设我们有以下内容:

          I--J   <-- branch1
         /
...--G--H
         \
          K--L   <-- branch2

You might wish to do git checkout branch1 and then git merge branch2 , to merge the two branches—but Git doesn't really care about branches .您可能希望先执行git checkout branch1 ,然后git merge branch2 ,以合并这两个分支——但 Git 并不真正关心分支 Git cares about commits . Git 关心提交 The merge process works by:合并过程的工作原理是:

  • locating the best shared commit, which in this case is H ;找到最佳共享提交,在本例中为H this is called the merge base;这称为合并基础;
  • comparing the snapshot in the merge base H to the snapshot in the current commit J , to see what "we" changed;将合并基础H中的快照与当前提交J中的快照进行比较,以查看“我们”更改了什么;
  • comparing the snapshot in the merge base H to the snapshot in the other commit L , to see what "they" changed;将合并基础H中的快照与另一个提交L中的快照进行比较,以查看“它们”发生了什么变化; and
  • combining the changes and applying the combined changes to the snapshot in H .合并更改并将合并的更改应用到H中的快照。

(You can think of this as applying their changes on top of our changes, or vice versa, but internally, Git just combines the two sets of changes, which means it needs to apply the combined changes to the base version, H .) (您可以认为这是在我们的更改之上应用他们的更改,反之亦然,但在内部,Git 只是组合了两组更改,这意味着它需要将组合更改应用到基本版本H 。)

Having done all of this combining successfully, Git normally goes on to make a new commit like this:成功完成所有这些组合后,Git 通常会像这样继续进行新的提交:

          I--J
         /    \
...--G--H      M   <-- branch1 (HEAD)
         \    /
          K--L   <-- branch2

Note that new commit M points back to both the previous current commit J and the other commit L .请注意,新提交M指向前一个当前提交J另一个提交L The branch name that gets updated is the same as always: the current branch name .更新的分支名称与往常一样:当前分支名称 The name branch1 now points to new merge commit M .名称branch1现在指向新的合并提交M

Using --no-commit , you instruct Git to stop after combining, or failing to combine, the two sets of changes.使用--no-commit ,您指示 Git 在合并或未能合并两组更改后停止。 That leaves you in this state:这让你留在这个 state:

          I--J   <-- branch1 (HEAD)
         /
...--G--H
         \
          K--L   <-- branch2

Git's attempt at applying the combined changes to H is found in one or both of two places: Git 尝试将组合更改应用于H可以在两个地方之一或两个地方找到:

  • If Git succeeded at combining the changes for some file F , file F is at stage zero in Git's index , and is modified in your work-tree to match the index copy.如果 Git 成功合并了某些文件F的更改,则文件F在 Git 的索引处于零阶段,并在您的工作树中进行了修改以匹配索引副本。

  • If Git failed at combining the changes for F , file F is at a nonzero stage in Git's index.如果 Git未能合并F的更改,则文件F在 Git 的索引中处于非零阶段。 In most cases, 2 there are now three copies of F in the index: the one at stage 1 is from commit H , the one at stage 2 is from commit J , and the one at stage 3 is from commit L .在大多数情况下,索引中现在有2F的三个副本:第 1 阶段的一个来自提交H ,第 2 阶段的一个来自提交J ,第 3 阶段的一个来自提交L Meanwhile, your work-tree contains Git's attempt at combining the changes, but with conflict markers added.同时,您的工作树包含 Git 合并更改的尝试,但添加了冲突标记。

A git commit command will, at this point:此时, git commit命令将:

  • fail if any index entries are not at stage zero, or如果任何索引条目不在零阶段,则失败,或者
  • make a new merge commit (commit M as usual) if all index entries are at stage zero.如果所有索引条目都处于零阶段,则进行新的合并提交(像往常一样提交M )。

As always, Git will make the new commit using whatever is in the index at this time.与往常一样,此时 Git 将使用索引中的任何内容进行新提交。 So git reset --mixed , which resets index files, could be something you could do.因此,重置索引文件的git reset --mixed可能是您可以执行的操作。 Usually though it's better to use git checkout (or in Git 2.23 or later, git restore ) to update both the index and your work-tree from some commit, if the idea is to keep one particular commit's version of those files.通常,最好使用git checkout (或在 Git 2.23 或更高版本中, git restore )从某个提交更新索引工作树如果想法是保留这些文件的一个特定提交版本。

You wrote this in a comment :在评论中写道:

Reset command - I am resetting back to its original state/commit on the current branch重置命令 - 我正在重置回其原始状态/在当前分支上提交

To do this, it's better to use:为此,最好使用:

git checkout HEAD -- path

or:要么:

git restore -iw --source HEAD path

which will update both the index and your work-tree copy of the named path from HEAD , and in the process, clear out the higher-stage (merge conflict) entries for the given path.这将从HEAD更新索引和命名path的工作树副本,并在此过程中清除给定路径的更高阶段(合并冲突)条目。

When I do git merge --continue it should not make the commit...当我执行git merge --continue它不应该提交...

The only thing a successful git merge --continue can do is make the commit.成功的git merge --continue能做就是提交。 Either the merge is now done—the index is ready to commit because all higher-stage entries have been cleared out, replaced with stage-zero entries ready to be committed—and git merge --continue will run git commit which will make the commit, or one of these two conditions holds:合并现在已完成——索引已准备好提交,因为所有更高阶段的条目都已被清除,替换为准备提交的零阶段条目——并且git merge --continue将运行git commit ,这将使提交,或者这两个条件之一成立:

  • There are still some higher-stage entries, and the commit cannot be made ( git commit will also fail).还有一些higher-stage entry,无法commit( git commit也会失败)。 Or:要么:
  • You aborted the merge entirely, so that there is no merge to continue ( git merge --continue will fail, but git commit will attempt to make a new commit).您完全中止了合并,因此没有合并可以继续( git merge --continue将失败,但git commit将尝试进行新的提交)。 3 3个

1 A fast-forward merge is not a merge at all, plus you're excluding it with --no-ff anyway. 1快进合并根本不是合并,而且您无论如何都要用--no-ff排除它。

2 This glosses over cases where there are modify/delete, rename/delete, and other such high-level conflicts, hence the phrase most cases . 2这掩盖了存在修改/删除、重命名/删除和其他此类高级冲突的情况,因此出现了短语most cases

3 Note that git commit , whether it is committing a merge or not, can fail for other reasons. 3请注意git commit ,无论是否提交合并,都可能因其他原因而失败。 For instance, you might have a pre-commit hook that rejects the commit, or your disk drive might be on fire.例如,您可能有一个拒绝提交的预提交挂钩,或者您的磁盘驱动器可能着火了。 A regular (non-merge) commit will typically fail if the current index matches the current commit: Git calls this an attempt to make an empty commit.如果当前索引与当前提交匹配,则常规(非合并)提交通常会失败:Git 将此称为尝试进行提交。 Note that you can force the commit with --allow-empty , and such a commit isn't actually empty: it still has a full snapshot of every file.请注意,您可以使用--allow-empty强制提交,这样的提交实际上并不是空的:它仍然有每个文件的完整快照。 It's just that the snapshot in the new commit exactly matches the snapshot in the previous commit, which makes Git wonder why you're bothering.只是新提交中的快照与之前提交中的快照完全匹配,这让 Git 想知道你为什么要打扰。

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

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