I have a Ruby on Rails project where every time I write config.log_level = :error
to config/environments/development.rb
. But I'm not allowed to commit and I only want to do that locally. Sometime I accidentally commit files when there are lots of files changed and I need to go through the pain of undoing the last commit and reset the development.rb file.
So I created a global gitignore file to ignore config/environments/development.rb:
$ echo 'config/environments/development.rb' >> ~/.gitignore
$ git config --global core.excludesfile ~/.gitignore
This works for only newly created config/environments/development.rb
but not for committed files. In my case, config/environments/development.rb
is committed. So the solution seems to be this:
git rm --cached 'config/environments/development.rb'
But seems like I have to commit the file now, otherwise, it says the file is deleted and is shown as this:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: config/environments/development.rb
And in my case, I can't commit such things because the organization will not permit this.
So I tried to hold the file with:
$ git update-index --no-assume-unchanged config/environments/development.rb
This isn't a global solution but works locally. I was still happy with this solution. But seems like when I do git stash
now, it stashes the held file successfully but the git stash pop
doesn't recover the file. And stashing is critical to me. I have to do a lot of rebase and frequently switch between branches while staging changes.
Then I tried adding the entry to .git/info/exclude
which also isn't a global solution. But it also doesn't work at all on already committed files.
Is there a way to globally ignore an existing and committed file?