Git convert between tabs and spaces, but only sometimes

First off, I'm new to git. For example, I could barely indicate the cache from the index if it hit me in the intermediate area. Or something like that. From this point of view, my problem is this:

Suppose I want to work on a project whose coding style sets spaces for indentation, but I like the tabs. It seems that I can use clean and spotty features, but there is a trick. The coding style is not executed sequentially, and there are some files that mix tabs and spaces on the same line. Thus, a naive approach will lead me to make a one-line change, but accidentally create a mass commit that will bring the project into full compliance with its own standards. That would be great, except that the differences would be less useful, so I get new enemies.

So, the question is, is there a way to make this magic work in such a way that, if I do not touch the file, it remains out of the picture? (I am ready to take full responsibility for the spaces of files that I touch, even if I change only one character.)

EDIT: Well, I just accepted the answer that I accepted yesterday. I am pretty sure this is very rude. My excuse is that I only checked it today. Since, apparently, two people already misunderstood me, let me clearly understand what I actually did, so maybe someone can tell me if I was confused and / or confused.

$ 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

As we see here, git reports that t.txt has been changed, although I just checked it. If you run git diff, it will require me to convert tabs to spaces. Am I doing something wrong?

+4
3

. - :

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
+3

- script smudge-clean.

smudge-clean

Smudge/Clean - , , , , script.

smudge clean , .


( unix): uexpand/unuexpand,

~/.gitconfig

# filters to convert between tabs to spaces
[filter "tabspace"]
    smudge = unexpand --tabs=2 --first-only
    clean = expand --tabs=2 --initial

~/.gitattributes

*.txt  filter=tabspace

, / , .

github

+2

:

  • .
  • .

:

  1. , . , , . , . , " ". , : git add FilesYouWantToAdd.txt , . , , .

    git commit -m ' '// , .

    git push

.

:

, .

git reset HEAD .

This is actually quite simple. Hope this helps.

EDIT: git modified

+1
source

Source: https://habr.com/ru/post/1680161/


All Articles