在git中存在未分级的更改时强制不合并

时间:2010-09-23 00:24:51

标签: git

我想强制执行(即抛出错误并失败),每当我执行git merge时,我都没有任何非分段更改,这与为什么如果存在非分段更改时git rebase不起作用的原因相同。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

它不漂亮,但它有效。我有兴趣看到更好的解决方案。

如果要合并的文件是脏的,Git已拒绝合并:

    $ git init
    Initialized empty Git repository in /tmp/mmm/.git/
    $ echo this is file1 > file1
    $ git add file1 
    $ git ci -m'first commit' file1 
    [master (root-commit) efd89a6] first commit
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 file1
    $ git co -b branch1
    Switched to a new branch 'branch1'
    $ echo change file1 >> file1 
    $ git ci -m'change on a branch' file1 
    [branch1 031e317] change on a branch
     1 files changed, 1 insertions(+), 0 deletions(-)
    $ git co master
    Switched to branch 'master'
    $ echo change on master >> file1 
    $ git merge branch1
    Updating efd89a6..031e317
    error: Your local changes to 'file1' would be overwritten by merge.  Aborting.
    Please, commit your changes or stash them before you can merge.

继续举例:

    $ git ci -m'commit change on master' file1 
    [master 8a2d52c] commit change on master
     1 files changed, 1 insertions(+), 0 deletions(-)
    $ git merge branch1
    Auto-merging file1
    CONFLICT (content): Merge conflict in file1
    Automatic merge failed; fix conflicts and then commit the result.
    $ vi file1 
    $ git add file1 
    $ git ci
    [master ee16606] Merge branch 'branch1'
    $ git log --oneline
    ee16606 Merge branch 'branch1'
    8a2d52c commit change on master
    031e317 change on a branch
    efd89a6 first commit
    $ echo this is file2 > file2
    $ git add file2
    $ git co -b branch2
    A       file2
    Switched to a new branch 'branch2'
    $ echo change on branch2 >> file2 
    $ git ci -m'commit on branch2' file2 
    [branch2 06ff9c3] commit on branch2
     1 files changed, 2 insertions(+), 0 deletions(-)
     create mode 100644 file2
    $ git co master
    Switched to branch 'master'
    $ echo this change logically conflicts with the change to file2 on branch2 >> file1 

这是您要强制失败的下一个命令 - 您希望在树变脏时避免此合并。您可以在prepare-commit-msg挂钩中执行此操作,但前提是您执行--no-ff合并,并且不要让合并自动提交。这是钩子的概述:

#!/bin/sh
case "$2,$3" in
  merge,)
    echo "Check here if the working tree is dirty. If it is, fail."
    exit 1
    ;;
  *) ;;
esac

你必须在你要合并的每个分支上使用--no-ff和--no-commit,并希望防止这样的合并:

    git config branch.master.mergeoptions "--no-commit --no-ff"

这就是会议的样子:

    $ git merge branch2
    Automatic merge went well; stopped before committing as requested
    $ git ci
    IN THE PREP HOOK: .git/COMMIT_EDITMSG, merge, 
    Check here if the working tree is dirty. If it is, fail.
    $ git status
    # On branch master
    # Changes to be committed:
    #
    #       modified:   file2
    #
    # Changed but not updated:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #       modified:   file1
    #
    bstpierre@bstpierre 1119 /tmp/mmm master
    $ git ci
    IN THE PREP HOOK: .git/COMMIT_EDITMSG, merge, 
    Check here if the working tree is dirty. If it is, fail.
    bstpierre@bstpierre 1120 /tmp/mmm master
    $ echo $?
    1

撤消回合并前状态:

    $ git reset --merge
    $ git status
    # On branch master
    # Changed but not updated:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #       modified:   file1
    #
    no changes added to commit (use "git add" and/or "git commit -a")