首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >防止在git中提交具有合并冲突的文件

防止在git中提交具有合并冲突的文件
EN

Stack Overflow用户
提问于 2014-06-14 05:20:44
回答 4查看 4.7K关注 0票数 24

有没有办法防止合并冲突的文件在git中被提交?没有人会故意提交有冲突的文件。但是有没有办法防止文件在git中被提交呢?

git是否有任何设置或可配置值,可以通过查找<<<<<<<=======>>>>>>>符号来阻止文件?

EN

回答 4

Stack Overflow用户

发布于 2014-11-26 21:20:19

VonC的回答已经解释了你可能想要检查合并提交的不同类型的钩子。

如果您只想要一个简单的解决方案来避免提交冲突,git已经在默认的pre-commit钩子示例中包含了这个解决方案。只需通过将.git/hooks/pre-commit.sample重命名为.git/hooks/pre-commit来启用钩子。如果您随后尝试提交冲突:

$ git commit -am "Fix crash in frobnicate"
src/foo.c:239: leftover conflict marker

注意:

该脚本在内部使用git diff --check,它还会检查各种空格问题-因此您可能也会得到空格错误。您还可以在提交以查找问题之前运行git diff --check。有关详细信息和配置选项,请参阅git diff手册页。

这对于GitV2.0是有效的;不知道它是什么时候引入的。

票数 10
EN

Stack Overflow用户

发布于 2017-03-04 01:10:41

一种使用预提交钩子的简单方法,改编自here,但改进为更仔细和彻底:

#!/bin/sh

changed=$(git diff --cached --name-only)

if [[ -z "$changed" ]]; then
    exit 0
fi

echo $changed | xargs egrep '^[><=]{7}( |$)' -H -I --line-number

# If the egrep command has any hits - echo a warning and exit with non-zero status.
if [ $? == 0 ]; then
    echo "WARNING: You have merge markers in the above files. Fix them before committing."
    echo "         If these markers are intentional, you can force the commit with the --no-verify argument."
    exit 1
fi

别忘了让钩子成为可执行文件(chmod u+x pre-commit)!

后来我把这个放到了github上:https://github.com/patrickvacek/git-reject-binaries-and-large-files/blob/master/pre-commit

票数 3
EN

Stack Overflow用户

发布于 2017-02-21 14:17:23

我添加了一个单元测试来遍历解决方案目录中的所有文件,以查找冲突标记字符串

[TestClass]
public class SolutionValidationTests
{
    [TestMethod]
    public void CheckForMergeConflicts()
    {
        var solutionValidationScripts = new SolutionValidationScripts();
        var mergeConflictCheckOkay = solutionValidationScripts.CheckForGitMergeConflict();
        Assert.IsTrue(mergeConflictCheckOkay);
    }
}

SolutionValidationScripts单独定义如下:

public class SolutionValidationScripts
{
    public bool CheckForGitMergeConflict()
    {
        var failCount = 0;
        System.Diagnostics.Debug.WriteLine($"{DateTime.Now.ToString(@"dd-MMM-yyyy HH:mm:ss")}: Starting");

        var currentDirectory = System.IO.Directory.GetCurrentDirectory();
        var sourceFolder = "";

        // Change to suit the build location of your solution
        sourceFolder = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
        // break up the string so this file doesn't get flagged in the test
        string searchWord = "<<<<<<< " + "HEAD";

        List<string> allFiles = new List<string>();
        AddFileNamesToList(sourceFolder, allFiles);
        for (int i = 0; i < allFiles.Count; i++)
        {
            // 35 sec
            var fileName = allFiles[i];
            string contents = File.ReadAllText(fileName);
            if (contents.Contains(searchWord))
            {
                // For faster result.. no need to continue once a problem is found
                // throwing an exception here actually works better than an early return to help resolve the issue
                throw new Exception(fileName);
            }
        }
        return (failCount == 0);
    }

    private void AddFileNamesToList(string sourceDir, List<string> allFiles)
    {
        string[] fileEntries = Directory.GetFiles(sourceDir);
        foreach (string fileName in fileEntries)
        {
            allFiles.Add(fileName);
        }

        //Recursion    
        string[] subdirectoryEntries = Directory.GetDirectories(sourceDir);
        foreach (string item in subdirectoryEntries)
        {
            // Avoid "reparse points"
            if ((File.GetAttributes(item) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
            {
                AddFileNamesToList(item, allFiles);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24213948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档