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 have:
gittesting/repo1:
file.txt
ignoreme:
ignore.txt
and
gittesting/repo2
file.txt
ignoreme:
ignore.txt
Repo2 is a copy of repo1, and ignoreme is already being tracked. The ignore.txt file becomes changed in repo2, but I want to stop tracking it and for git to completely ignore it. The problem is that if I create a .gitignore file and add ignoreme, it's too late because it's already being tracked, so I would have to do git rm --cached ignore, but then it's marked as deleted and if I pulled the commit to repo1, the directory would be deleted instead of being left alone..
To sum it up:
- The ignore.txt have different content between the two repos.
- I want the ignore.txt contents to remain as they are and be completely ignored by git
I've looked online, asked in the IRC, and looked at the very related questions, but can't find a way to do this. I know the example seems trivial, but it's exactly what I need to do on my site, where the directory is Forum/cache instead.
edit:
This is a bit of a hack and I'd prefer a better answer, but I ended up doing:
cd repo2
echo "ignoreme" > .gitignore
echo "ignoreme/*" > .gitignore
git rm --cache -r ignoreme
git commit -m "Should ignore now"
cd ../repo1
mv ignoreme ignoreme2
git pull ../repo2
mv ignoreme2 ignoreme
If I understand your question correctly, you want
repo2
to know about theignoreme/
directory, but you don't want Git to care about any modifications to the directory. Andgit 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):
You could try the following:
Copy the
ignoreme/
directory to a new location, and make it a git repositoryAdd it back as a submodule in
repo2
: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 inignoreme/
will not be tracked inrepo2
unless you commit them in the submodule. Let's sayignoreme/ignore.txt
was modified somehow:Even if you run
git add .
, the changes inignoreme/ignore.txt
will not be added to the index unless they are committed to the submodule like so:However if you want to forget the local modifications in the submodule:
Try this
Git will ignore any future changes to this file without changing the repository. To start tracking changes again, use
Note that this only takes effect for the current working copy, so you would need to do this each time you clone the repository.
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.)
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.
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.