I know it seems like a duplicate of GIT: How to keep ignored files when switching branches?, but I couldn't find my answer there.
So I created a new branch with the --orphan
flag, to delete the commit history. Then I did git rm --cached
on all the files, modified .gitignore
to ignore some of them, then added and committed. As expected, the ignored files are not in the new repo (nor in any of its history). However, they disappear when I switch branches (that is, when I check out an old branch, then check out the new one again). How can I avoid that?
EDIT: The purpose of the whole operation was to prepare a project to be published on GitHub, so I wanted to hide some config files that contain private information like passwords, keys, etc. That's why I deleted the commit history by creating a new orphan branch. I understand that probably committing the files before adding them to .gitignore
would solve the disappearing issue, but that would make the files ultimately visible in the public history. I tried to do git add -f
, then git rm --cached
, but it doesn't do the trick.
When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file. dat , it will not just delete it. The file new_file.
If you already git add ed some files, their changes will still be tracked. To remove those files from your repository (but not from your file system) use git rm --cached on them. git rm --cached file_name. ext wroks fine for me to update gitignore for one file.
Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!
However, they disappear when I switch branches (that is, when I check out an old branch, then check out the new one again). How can I avoid that?
You cannot. This is by-design. In your publish
branch, you do not have the files tracked, and you have a .gitignore
for them. Great! But when you switch to some other branch that does have those files tracked, they will be checked out to your working directory. This is because a .gitignore
only applies to untracked files.
From the documentation:
A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
Now if you try to switch back to your publish
branch, the files will be removed on-disk because they do not exist in that branch. Your .gitignore
for these files is ignored because they are tracked and it would not apply to them.
(Note that they are - by definition - tracked even if they're in the branch that you're switching away from. This is mostly just an implementation detail and I concede that this may not be intuitive.)
What you want to do is remove every occurrence of these files in all the branches. Use a tool like BFG Repo Cleaner to remove these files from your history completely, in all branches. Then you can safely .gitignore
them, then once they're gone completely you can safely push this to GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With