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

是否可以在一行中本地获取合并冲突中涉及的文件的所有版本?

git
  •  1
  • neverendingqs  · 技术社区  · 6 年前

    https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging 建议执行以下操作以使三个文件参与三向合并:

    $ git show :1:hello.rb > hello.common.rb
    $ git show :2:hello.rb > hello.ours.rb
    $ git show :3:hello.rb > hello.theirs.rb
    

    Git有什么内置的功能来代替它吗? https://stackoverflow.com/a/44755386/2687324 建议 git merge-tool 在启动工具之前帮你做这些,所以我想应该有办法。

    注意-应该可以使用别名:

    mergefiles = "!f() { git show :1:$1 > $1.common; git show :2:$1 > $2.common; git show :3:$1 > $3.common; }; f"
    

    但我不想为边缘情况(例如,如果HEAD和MERGE-HEAD添加了一个新文件,这将失败,因为公共祖先没有该文件):

    fatal: Path '<path>' is in the index, but not at stage 1.
    Did you mean ':2:<path>'?
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Schwern    6 年前

    git-mergetool 做了很多,你可以 read the source to learn what it does . 它只是一个shell脚本,您可以潜在地复制它的功能。但是利用所有这些工作并编写自己的合并工具要容易得多。

    From the git-mergetool docs ...

    git merge tool不是运行一个已知的合并工具程序,而是可以通过在配置变量mergetool..cmd中指定要调用的命令行进行自定义,以运行另一个程序。

    使用此工具(通过-t或--tool选项或merge.tool配置变量)调用git mergetool时,将调用已配置的命令行,并将$BASE设置为包含合并公共基的临时文件的名称(如果可用);$LOCAL set为包含当前分支上文件内容的临时文件的名称;$REMOTE set为包含要合并的文件内容的临时文件的名称,$merged set为合并工具应向其写入合并解析结果的文件的名称。

    编写一个小的shell脚本,把它放在您的路径中,并使其可执行。请务必以非零退出以指示合并失败。

    $ cat ~/bin/show-git-mergefiles 
    #!/bin/sh
    
    echo Base: $1
    echo Local: $2
    echo Remote: $3
    echo Merged: $4
    
    exit 1
    

    然后将其添加到 .gitconfig 把环境变量传给它。

    [mergetool "show-git-mergefiles"]
            cmd = show-git-mergefiles $BASE $LOCAL $REMOTE $MERGED
            trustExitCode = true
    

    鲍勃是你父母的兄弟。

    $ git mergetool -t show-git-mergefiles
    Merging:
    file2
    
    Normal merge conflict for 'file2':
      {local}: modified file
      {remote}: modified file
    Base: ./file2_BASE_84551
    Local: ./file2_LOCAL_84551
    Remote: ./file2_REMOTE_84551
    Merged: file2
    merge of file2 failed
    Continue merging other unresolved paths [y/n]?
    

    在此之前,考虑一下现有的合并工具是否符合您的需要。例如, vimdiff .

    aldsfkalkd           |  aldsfkalkd           |  aldsfkalkd
    ldfkjlj              |  ldfkjlj              |  ldfkjlj
    another line         |                       |  feature line          
    ---------------------|  ---------------------|  ----------------------
    ---------------------|  ---------------------|  ----------------------
    ---------------------|  ---------------------|  ----------------------
    ---------------------|  ---------------------|  ----------------------
    ~                    |  ~                    |  ~                     
    ~                    |  ~                    |  ~                     
    ~                    |  ~                    |  ~                     
    ./file2_LOCAL_82335     ./file2_BASE_82335      ./file2_REMOTE_82335  
    aldsfka
    ldfk
    <<<<<<< HEAD                                                          
    another line                                                          
    =======                                                               
    feature line                                                          
    >>>>>>> feature                                                       
    ~                                                                     
    ~                                                                     
    ~                                                                     
    ~                                                                     
    file2