How to stop tracking and ignore changes to a file in Git?

前端 未结 20 1700
梦谈多话
梦谈多话 2020-11-21 07:17

I have cloned a project that includes some .csproj files. I don\'t need/like my local csproj files being tracked by Git (or being brought up when c

20条回答
  •  被撕碎了的回忆
    2020-11-21 07:46

    To ignore any changes to all the files (of a certain type) in a directory, I had to combine some of these approaches, otherwise the files were created if they didn't previously exist.

    In the below, "excludedir" is the name of the directory that I wish to not watch changes to.

    First, remove any existing new files from your change tracking cache (without removing from your file system).

    git status | grep "new file:" | cut  --complement -d " " -f1-4 | grep "^excludedir" | xargs git rm --cache
    

    You can do the same with modified:. renamed: is a bit more complicated, as you'll have to look at the post -> bit for the new filename, and do the pre -> bit as described for deleted: below.

    deleted: files prove a bit more complicated, as you can't seem to update-index for a file that doesn't exist on the local system

    echo .deletedfiles >> .gitignore
    git status | grep "deleted:" | cut  --complement -d " " -f1-4 | grep "^excludedir" > .deletedfiles
    cat .deletedfiles | xargs -d '\n' touch
    cat .deletedfiles | xargs -d '\n' git add -f
    cat .deletedfiles | xargs -d '\n' git update-index --assume-unchanged
    cat .deletedfiles | xargs -d '\n' rm
    

    The last command in the list above will remove the files again from your file system, so feel free to omit that.

    Then, block change tracking from that directory

    git ls-files excludedir/ | xargs git update-index --skip-worktree
    git update index --skip-worktree excludedir/
    

提交回复
热议问题