user4576114
user4576114

Reputation:

Force git to update .gitignore

I have a .gitignore file, and it's ignoring some files. I have updated the .gitignore file (removed some filenames and added some filenames). This is not reflected in git status. How can I force git to update these changes, so that track files which are not tracked before and vice versa.

I have tried this question, still all of my files are not tracked (according to my updated .gitignore). (In simple, how can I force git to retract files once .gitignore is updated or deleted).

Upvotes: 159

Views: 197952

Answers (7)

Guillaume G
Guillaume G

Reputation: 309

Step 1: Commit any changes to your code

git commit -m "Committing changes"

Step 2: Remove all tracked files from the Git index without deleting them

git rm -r --cached .

Step 3: Re-add all the files to the Git index

git add .

Step 4: Commit the changes

git commit -m "Updating .gitignore to stop tracking unwanted files"

Upvotes: 0

WangJiang
WangJiang

Reputation: 29

When you modify the .gitignore file and want to re-add the files that should be ignored, you can run git init again. If you have already executed git add before this, you can run git reset and then execute git add again.

Upvotes: -1

Shravan40
Shravan40

Reputation: 9888

You will have to clear the existing git cache first.

Remove the cache of all the files

  • git rm -r --cached .

Remove the cache of specific file

  • git rm -r --cached <file_name.ext>

Once you clear the existing cache, add/stage file/files in the current directory and commit

  • git add . // To add all the files
  • git add <file_name.ext> // To add specific file
  • git commit -m "Suitable Message"

As pointed out by Scott Biggs in comment that "This works for both adding a file that was once ignored as well as ignoring a file that was once tracked"

Upvotes: 331

Najathi
Najathi

Reputation: 3015

It works

//First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

//This removes any changed files from the index(staging area), then just run:

git add .

//Commit

git commit -m "Atualizando .gitignore para..."

Upvotes: 23

Saahithyan Vigneswaran
Saahithyan Vigneswaran

Reputation: 7143

If you want to add all files, delete all filenames from .gitignore file, not the .gitignore file and commit it, then try

git config --global core.excludesfile ~/.gitignore_global

Some files are ignored by the git depending on the OS (like .dll in windows). For more information.

Now

git add .

git status

git commit -m "your message"

Or

You can try a simple hack, it may or may not work. Delete all filenames from .gitignore file and add this line !*.*, then add and commit.

UPDATE

Simple, I'll explain with an example. Say you have a build folder which is already added and tracked by git. Now you decide not to track this folder.

  1. Add this folder (build) to .gitignore
  2. Delete build folder
  3. Commit your changes

From now on git will not track build folder.

Upvotes: 16

Rajdeep Gautam
Rajdeep Gautam

Reputation: 672

You can resolve this issue by deleting the cache of those specific files where you are getting this issue. The issue you mentioned occurs when you have committed some specific files once and then you are adding them in .gitignore later.

git rm -r --cached <your_file.extension>
git commit -am "Suitable Message"

Same solution is proposed by @sharvan40 above, but you don't need to remove the cache for all the files. It creates a new commit for all your files.

Upvotes: 8

Vidur
Vidur

Reputation: 1552

Assuming here that your current working directory is empty.

You can check what files git is currently tracking by using git ls-files. If you have a lot of files, you can use git ls-files | grep hello.txt to find if git is tracking that specific file.

If it is tracking it, then use git rm hello.txt to untrack it (as Tim mentioned in his comment). Perhaps commit that untracked state first and then add it in to your .gitignore on your next commit. I have seen some funky behavior in the past when trying to ignore and remove in the same commit.

Upvotes: 0

Related Questions