Git ignore files being tracked WITHOUT DELETING THEM

后端 未结 5 1316
别跟我提以往
别跟我提以往 2020-12-16 13:09

I\'m having some trouble understanding basic git concepts :/

I\'m testing on my local Windows machine before trying some things on my git-controlled site.

I

相关标签:
5条回答
  • 2020-12-16 13:34

    Git does not store directories. If you want to keep an empty directory in Git, the convention is to put there an empty file named .keepme, and commit it.

    As for the question, you will not be able to hide a file in an upstream branch from its clone, to the best of my knowledge. This is not something that Git is designed to do. Consider other options, like splitting to two repositories (and, maybe, using subtree or submodules). Or keep a separate branch in upstream to be tracked by downstream, and filter the ignore.txt from that branch by a post-receive hook.

    Tell us more of why do you want to do this, maybe there is a better way.

    Anyway, I hope that you do not try to hide this file for "security" reasons — otherwise matters would be much more complicated (e.g. you have to clear it from the whole history etc.)

    0 讨论(0)
  • Try this

    git update-index --assume-unchanged ignoreme/ignore.txt
    

    Git will ignore any future changes to this file without changing the repository. To start tracking changes again, use

    git update-index --no-assume-unchanged ignoreme/ignore.txt
    

    Note that this only takes effect for the current working copy, so you would need to do this each time you clone the repository.

    0 讨论(0)
  • 2020-12-16 13:51

    If I understand your question correctly, you want repo2 to know about the ignoreme/ directory, but you don't want Git to care about any modifications to the directory. And git rm --cached won't help you because you're telling Git to stop tracking this content from now on.

    Git's solution for keeping track of content, but fixed at a certain point, is through submodules. This except from the Git Book explains (emphasis added):

    Although rack is a subdirectory in your working directory, Git sees it as a submodule and doesn’t track its contents when you’re not in that directory. Instead, Git records it as a particular commit from that repository.

    You could try the following:

    1. Copy the ignoreme/ directory to a new location, and make it a git repository

    2. Add it back as a submodule in repo2:

      git submodule add file:///path/to/ignoreme ignoreme
      git commit -m"Add ignoreme/ as a submodule"
      

    The ignoreme submodule is now fixed to a particular commit. repo2 is still tracking whatever content is inside the submodule, but any changes to files in ignoreme/ will not be tracked in repo2 unless you commit them in the submodule. Let's say ignoreme/ignore.txt was modified somehow:

    $ 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)
    #   (commit or discard the untracked or modified content in submodules)
    #
    #       modified:   ignoreme (modified content)
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    

    Even if you run git add ., the changes in ignoreme/ignore.txt will not be added to the index unless they are committed to the submodule like so:

    $ cd ignoreme
    $ git commit -am"Time to change ignore.txt"
    $ cd ..
    $ git add .
    $ git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #       modified:   ignoreme
    #
    

    However if you want to forget the local modifications in the submodule:

    $ cd ignoreme
    $ git reset --hard
    $ cd ..
    
    0 讨论(0)
  • 2020-12-16 13:56

    Git only applies ignore patterns to untracked files. You can't use ignore patterns to ignore changes to files that are already tracked by git. Still, see https://gist.github.com/1423106 for ways people have worked around the problem.

    Note if you are worried about .gitignore itself, you can try using .git/info/excludes for a local-repo-only .gitignore file. Note this will not solve the problem of changing tracked files, only give you supplemental .gitignores which will not be tracked.

    0 讨论(0)
  • 2020-12-16 14:00

    Checking out a commit that has that file will replace it, and transitioning from a commit with it to a commit without it will remove it. Git couldn't sanely do anything else. .gitignore doesn't override the repo, it's a convenience to keep fluff off your status reports.

    You could make the file a symbolic link to someplace outside git's territory, that makes recovery from damage done by checking out the bad history easy. If that's not an option there's filter-branch.

    0 讨论(0)
提交回复
热议问题