5

What is the simplest solution to fix committed white space issues like trailing white spaces to an existing repo?

4

3 回答 3

4

刚刚在 git-docs(待测试)中发现:

git-rebase --whitespace=fix

也可能是一种选择。

看 :

于 2014-08-21T13:17:17.457 回答
3

如果您的存储库中有现有的提交带有尾随空格,那么您将需要更改这些提交(重写历史记录),如果该存储库已经被其他人推送和克隆,这可能会很痛苦。

但如果这是一个选项,那么您可以使用类似gitolite 中的脚本:

#!/bin/bash

# from doener (who else!)
# to be called as an index-filter

if git rev-parse --quiet --verify $GIT_COMMIT^ >/dev/null
then
        against=$(map $(git rev-parse $GIT_COMMIT^))
        git reset -q $against -- .
else
        # Initial commit: diff against an empty tree object
        against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
        git rm --cached -rfq --ignore-unmatch '*'
fi

git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix

像这样运行:

git filter-branch --index-filter '. ~/git-scripts/ws-fix.sh'
于 2013-08-06T09:31:16.180 回答
1

我更愿意将此作为评论添加到已接受的答案中,但我刚刚注册以分享我的发现并且还没有足够的声誉。

如果重写历史记录(从而更改所有提交哈希)一种选择,那么像上述答案中的过滤器脚本似乎是最好的方法。但是,当我尝试ws-fix.sh'从那里使用脚本时,该map功能不可用。从快速查看最近的来源看来git-filter-branch.sh,该功能(默认情况map)仅在. 只是为了确保我也尝试导出该功能但没有成功。使它最终对我有用的原因是在过滤器脚本本身中简单地重新定义了 map 函数:commit-filterindex-filtermapws-fix.sh

#!/bin/bash

map()
{
    if test -r "../map/$1"; then
        cat "../map/$1"
    else
        echo "$1"
    fi
}

if git rev-parse --quiet --verify $GIT_COMMIT^ > /dev/null
then
        against=$(map $(git rev-parse $GIT_COMMIT^))
        git reset --quiet $against -- .
else
        against=$(git hash-object -t tree /dev/null)
        git rm --quiet --force --ignore-unmatch --cached -r '*'
fi

git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix 2> /dev/null

使用这个版本,我可以应用index-filter到目前为止没有问题:

git filter-branch --index-filter '~/ws-fix.sh'

不过,我没有在更复杂的存储库上对此进行测试。因此,请务必在测试前进行备份

于 2020-05-25T13:50:31.687 回答