35

My .gitignore file gets ignored and the files which should be ignored are still visible.

user@host ~/workdir % git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: .htaccess # modified: application/controllers/statistics.php # no changes added to commit (use "git add" and/or "git commit -a") user@host ~/workdir % cat .gitignore .htaccess application/config/config.php application/config/database.php user@host ~/workdir % 

The files are in version control, but with sensible changes I dont want to push. git rm ... is not an option because the files should be in version controll (with other settings in the files). I just dont want to push my changes on this files.

1

8 Answers 8

72

i had same problem: i wanted different .htaccess file per repository then i couldn't because htaccess file had already been added to repository. It's working now, i can change any of the .htaccess file from any version without getting git pushing them. Marc is right when he says "You need to remove it first, before it can be ignored. This can be done using git rm".

i proceeded in this order:

  • backup your .htaccess file somewhere
  • remove .htaccess file using git rm .htaccess
  • commit this change (.htaccess deletion)
  • push it (git push)

this will obviously remove your .htaccess file

  • then edit .gitignore and add /.htaccess
  • commit then push it
  • recreate your .htaccess file by restoring your previous backup

then you're done: whatever changes you make into .htaccess won't be pushed anymore. The hard part is following steps order...

hope this is clear

Sign up to request clarification or add additional context in comments.

2 Comments

The forward slash! It was driving me mad, i had just .htaccess on my gitignore file
agreed with @sledgeweight forward slash does not make any different if you have push the changes after removing .htaccess file all you need to add .gitignore file with .htaccess and you are good to go
51

after anybug's steps noted here my git still kept track of .htaccess and this helped:

git update-index --assume-unchanged .htaccess 

To revert back:

git update-index --no-assume-unchanged .htaccess 

1 Comment

You can revert that effect by git update-index --no-assume-unchanged
14

Since you want to:

  • keep .htaccess in the Git repo (no git rm)
  • have local sensitive data in it

use a filter driver which will:

  • during the checkout phase, complete the .htaccess with private data
  • during the commit phase clean the .htaccess from all sensitive data

content filter driver

you can then version an encrypted file that your smudge script will:

  • be the only one to decrypt
  • use in order to complete the classic (and pushed/pulled) .htaccess file.

You can version that encrypted sensible data in a special branch you won't push.
Even if you did push that file somehow, the damage would be limited, since only your workstation has the private key needed to decrypt said file.

3 Comments

+1 for the pretty picture and the filter driver. I was wondering when you'd pitch in this being a version control question and all. :)
+1 for automation. I was going to suggest git update-index --assume-unchanged, which may still work as a simple solution, but this kind of automation is fantastic.
+1 for the sheer fact that anybody knows how to wield all this arcane git stuff. This was not what I was looking for, but still!
10

Suppose the below is the content of .gitignore file. Here, .htaccess is in the project's root directory.

/.htaccess application/config/config.php application/config/database.php 

Let's run the below command to ignore the .htaccess, config.php and database.php files.

[email protected]:work-dir$ git update-index --assume-unchanged .htaccess [email protected]:work-dir$ git update-index --assume-unchanged application/config/config.php [email protected]:work-dir$ git update-index --assume-unchanged application/config/database.php 

If you want to start tracking it again:

[email protected]:work-dir$ git update-index --no-assume-unchanged path/to/file 

e.g.

[email protected]:work-dir$ git update-index --no-assume-unchanged .htaccess 

2 Comments

This worked for me. My code is inside project's /code/ dir. $ git update-index --assume-unchanged code/.htaccess. And SourceTree don't track it any more.
this is the only thing that worked for me. was driving me crazy, thank you
8

It seems that the .htaccess file has already been added to the version control. You need to remove it first, before it can be ignored. This can be done using git rm

3 Comments

I know, but I don't want to delete the file. I just want to ignore the changes
If there is a version of .htaccess in the version control, but you just don't want to commit any changes, .gitignore won't work. There is no option for this as far as I know. Just don't add that file to a commit.
you can use git rm --cached which will leave the file in the directory standing but remove it from the index
8

Ignoring files in git doesn't mean that you won't push them. It means that you won't version control them. If you don't want to push then, I suggest you create a local branch which will contain stuff for the local machine and then merge necessary changes to a branch which you'll push to remote.

I think you've already added your files and are now trying to ignore them.

3 Comments

A other way I use now is to stash the local changes and pop the stash back after merging/rebasing.
I had this file on my ignore list and somehow "add"-ed it as well. Good call @Naufal
Didn't really answer the question, but @anybug's answer did.
5

I do this all the time. I start with configuration files in my repo then decide that I don't want them in there.

You can remove something from your repo by using

git rm --cached <<filename>> 

Comments

2

This one bugged me for days. The .htaccess file is already under version control and has been modified.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.