代码之家  ›  专栏  ›  技术社区  ›  ik1ne

在合并过程中,如何在保留合并头的同时重置所有文件?

git
  •  0
  • ik1ne  · 技术社区  · 7 年前

    例如,假设在合并到master的过程中发生了冲突,如下所示。

    A --- M <- master
        /
    B -
    

    有时我发现冲突或提交B太混乱(例如,所有文件的crlf更改),所以我决定将文件状态重置为A并只修改特定于特性的文件。(即从头开始写M。)

    但是,如果我这么做的话 git reset --hard . ,文件会被重置,但合并头也会在之后被删除,因此无法创建合并提交( fatal: There is no merge in progress (MERGE_HEAD missing). )

    如何在保留合并头时重置所有文件?

    1 回复  |  直到 7 年前
        1
  •  1
  •   VonC    7 年前

    使用git2.23+(2019年8月),检查是否可以使用 一 git restore ( man page )

    git restore --source=HEAD --staged --worktree -- .
    

    vonc@VONC D:\git\git
    > git restore --source=@ --staged --worktree  --ignore-unmerged -- .
    warning: path 'mru.h' is unmerged
    warning: path 't/t2028-worktree-move.sh' is unmerged
    
    vonc@VONC D:\git\git
    > git st
    On branch master
    Your branch is up to date with 'origin/master'.
    
    All conflicts fixed but you are still merging.
      (use "git commit" to conclude merge)
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   sha1collisiondetection (new commits)
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            mru.h
            t/t2028-worktree-move.sh
    

    仍在合并:

    > dir .git
     Volume in drive D is data
     Volume Serial Number is xxx
    
     Directory of D:\git\git\.git
    
    10/03/2019  20:04                 2 COMMIT_EDITMSG
    10/03/2019  20:06               593 config
    19/06/2016  09:15                73 description
    27/07/2019  08:55               483 FETCH_HEAD
    15/12/2017  07:28         5 324 666 gitk.cache
    27/07/2019  08:55                23 HEAD
    19/06/2016  09:15    <DIR>          hooks
    31/08/2019  13:54           358 360 index
    27/04/2019  18:25    <DIR>          info
    05/03/2018  23:34    <DIR>          lfs
    27/04/2019  18:25    <DIR>          logs
    31/08/2019  13:42                41 MERGE_HEAD   <===
    31/08/2019  13:42                 0 MERGE_MODE   <===
    31/08/2019  13:42             1 295 MERGE_MSG    <===
    
        2
  •  0
  •   ik1ne    7 年前

    这不能用一个命令完成,但这两个命令可以实现。

    1. git reset -- . 仅重置索引。

    2. git checkout -- . 放弃工作副本更改。

    在此合并之后,文件更改被放弃时,存在合并头。

        3
  •  -1
  •   torek    7 年前

    如果你没有git2.23,你可以用 git rm -r -- .; git checkout HEAD -- . git read-tree --reset -m -u HEAD .

    首字母 git rm -r 只有在提交文件时才需要 B A 和 B 和 希望他们合并,所以:

    git checkout HEAD -- .
    

    通常就够了。这与新的基本相同:

    git restore --source=HEAD --staged --worktree -- .
    

    --no-overlay 和 --overlay 与这些新文件有关)。