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

前端 未结 20 1747
梦谈多话
梦谈多话 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:42

    Apply .gitignore to the present/future

    This method applies the standard .gitignore behavior, and does not require manually specifying the files that need to be ignored.

    Can't use --exclude-from=.gitignore anymore :/ - Here's the updated method:

    General advice: start with a clean repo - everything committed, nothing pending in working directory or index, and make a backup!

    #commit up-to-date .gitignore (if not already existing)
    #this command must be run on each branch
    git add .gitignore
    git commit -m "Create .gitignore"
    
    #apply standard git ignore behavior only to current index, not working directory (--cached)
    #if this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
    #this command must be run on each branch
    git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached
    
    #optionally add anything to the index that was previously ignored but now shouldn't be:
    git add *
    
    #commit again
    #optionally use the --amend flag to merge this commit with the previous one instead of creating 2 commits.
    
    git commit -m "re-applied modified .gitignore"
    
    #other devs who pull after this commit is pushed will see the  newly-.gitignored files DELETED
    

    If you also need to purge the newly-ignored files from the branch's commit history or if you don't want the newly-ignored files to be deleted from future pulls, see this answer.

提交回复
热议问题