git add adding ignored files

后端 未结 10 2166
不知归路
不知归路 2021-02-01 00:58

I\'m trying to remove a previously tracked directory from git, which works, but it\'s being added back with each subsequent git add ., git add -A, etc.

10条回答
  •  孤街浪徒
    2021-02-01 01:52

    gitignore - Specifies intentionally untracked files to ignore.

    $HOME/.config/git/ignore, $GIT_DIR/info/exclude, .gitignore

    Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

    To ignore entire directory you should use /**,

    A trailing /** matches everything inside. For example, abc/** matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

    Or

    You can ignore the entire directory by adding this line to your root .gitignore file:

    /Dir_Name

    Instead, you can add a /logs/.gitignore file containing this:

    [^.]*

    The directory will remain in your repo, but all files inside /directory will be ignored. Easy!

    The steps you need to follows are,

    1. Remove it from the project directory (without actually deleting it):

      git rm --cached folder/*

    2. If you don't already have a .gitignore, you can make one right inside of your project folder:
    3. project/.gitignore. Put folder/* (or any of the pattern you think better from above listing) in the .gitignore Commit:
    4. git commit -m "message".
    5. Push your change to github.

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
    

提交回复
热议问题