gpt4 book ai didi

Git 在制表符和空格之间转换,但只是有时

转载 作者:太空狗 更新时间:2023-10-29 13:33:13 25 4
gpt4 key购买 nike

首先,我是 git 的新手。就像,如果它在暂存区命中我,我几乎无法区分缓存和索引。或类似的东西。有了这个,我的问题是:

假设我想从事一个编码风格要求缩进空格的项目,但我喜欢制表符。似乎我可以使用清洁和涂抹功能,但有一个问题。编码风格没有一贯遵循,有些文件在同一行混合制表符和空格。因此,一种天真的方法会导致我进行一行更改,但意外地创建了一个大量的提交,使项目完全符合其自己的标准。这会很棒,只是 diff 的用处不大,所以我最终会遇到新的敌人。

所以问题是:有没有一种方法可以让这种神奇的方式发挥作用,如果我不触摸文件,它就不会出现在画面中? (我愿意对我触及的文件的空白承担全部责任,即使我只更改了一个字符。)

编辑:好的,我刚刚接受了昨天接受的答案。我很确定这对我来说很粗鲁。我的借口是我今天才抽出时间来测试它。由于显然已经有两个人误解了我,所以让我清楚地说明我实际上做了什么,所以如果我感到困惑和/或困惑,也许有人可以告诉我。

$ ls -a
. .. t.txt
$ hd t.txt # file contains 3 bytes: a tab, a capital A, and a newline
00000000 09 41 0a |.A.|
00000003
$ git init
Initialized empty Git repository in /home/marvy/test/.git/
$ git config --local git config --local user.name me
$ git config --local user.email me@example.com
$ git add t.txt
$ git commit
[master (root-commit) 959bf99] testing cleverness of git status
1 file changed, 1 insertion(+)
create mode 100644 t.txt
$ echo '*.txt filter=tabspace' > .git/info/attributes
$ cat .git/info/attributes
*.txt filter=tabspace
$ git config --local filter.tabspace.smudge unexpand
$ git config --local filter.tabspace.clean expand
$ rm t.txt
$ git checkout t.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: t.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git help --stackoverflow

正如我们在这里看到的,git status 报告 t.txt 已被修改,即使我刚刚检查过它。如果你运行 git diff,它会声称我想将制表符转换为空格。我做错了什么吗?

最佳答案

您可以使用预提交 Hook ,只循环遍历您编辑的文件并将制表符替换为空格。类似下面的内容:

FILES=`git status -s -uno | egrep '^M' | sed 's/^M//'`

for FILE in $FILES
do
(sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || sed -i '' -E 's/[[:space:]]*$//' "$FILE")
fi

关于Git 在制表符和空格之间转换,但只是有时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com