git add adding ignored files

后端 未结 10 2164
不知归路
不知归路 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:44

    You can do

    git check-ignore -v --no-index path/with/unexpected/result
    

    to see why git add did or didn't add that path.

    git check-ignore docs.

    In particular, the point is you want to check what's actually getting added, not a directory.

    further, do find . -name .git. Submodules are nested repos, .gitmodules and the submodule command are handy but they're just there to help with them.

    0 讨论(0)
  • 2021-02-01 01:46

    Here is what I do to ignore node_modules folder after I tracked it.

    1. Create the .gitignore file and add node_modules to it.

    2. Commit the .gitignore file. After this point, whatever you update in the node_modules folder won't appear in git status.

    3. But this does not delete what we already have on the repo under the node_modules folder. So now we need to remove whatever we have committed previously.

      For this, use git rm --cached node_modules -r

    4. Now git status will show that the files are deleted.

    5. Use git commit -m "node_modules removed" command with any message.

    Now everything should be removed from the repo and future changes will not be tracked.

    0 讨论(0)
  • 2021-02-01 01:48

    The origin has to be bare. Here is a little bash to demonstrate it. Feel free to edit it, if it does not represent your use case.

    #!/bin/bash
    
    rm -rf /tmp/git_test
    mkdir /tmp/git_test/
    git init --bare /tmp/git_test/primary_repo
    
    # populate repo
    cd /tmp/git_test/
    git clone ./primary_repo ./secondary_repo
    cd secondary_repo
    echo hi_bob > some_content
    mkdir node_modules
    echo hi_dave > node_modules/moduleA
    git add .
    git commit -m "populated primary"
    git push
    
    # do the removal in tertiary 
    cd /tmp/git_test/
    git clone ./primary_repo ./tertiary_repo
    cd tertiary_repo
    echo node_modules >> .gitignore
    git add .gitignore
    git rm -r --cached node_modules
    git commit -a -m "removed node_modules"
    git push origin master
    rm -r node_modules
    echo --------------------------------
    echo git status:
    git status
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题