1

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?

15 Volts
  • 1,946
  • 15
  • 37
  • 3
    *"Is there a way to globally ignore an existing and committed file?"* - this is against Git's purpose. If you need it to ignore a file then make sure it is not tracked. Remove it from the repo if it is already tracked. – axiac Dec 16 '22 at 14:49
  • Now it becomes a factor of personal choice vs. what my organization wants. I don't need to keep track of development.rb but my organization wants that. In that case, I have no way other than ignoring a committed file. – 15 Volts Dec 16 '22 at 14:51
  • Your build environment should allow local overrides using files that are never added and are therefore safely listed in `.gitignore`. You can then create such override files and not have this problem in the first place. This just requires updating your ruby configs to conditionally include override files if they exist. – torek Dec 17 '22 at 14:41

1 Answers1

0

A very crude solution works so far. I ignored files globally with the following command:

$ echo 'config/environments/development.rb' >> ~/.gitignore
$ git config --global core.excludesfile ~/.gitignore

Then I removed the cache:

$ git rm --cached config/environments/development.rb

Now it shows this:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    config/environments/development.rb

Then I added and committed all the files I have in staging (development.rb along with few other ones).

$ git add .
$ git commit -m "Doesn't matter"

Then reset the last commit:

$ git reset HEAD~

Now the development.rb changes are there but git status won't show it. When stash drops the change, it will still bring the change back when git stash pop is run.

Now this is all local and one should bear the pain of doing that every-time they clone the same repo. So a shell alias might work.

Also, this is very crude way, but hoping for better answers.

15 Volts
  • 1,946
  • 15
  • 37