Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force git to update .gitignore

Tags:

git

gitignore

People also ask

How do I force add an ignored file in Git?

The git add command can be used to add ignored files with the -f (force) option.

How can I ignore files that have already been committed to the repo?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.

How do you force Add all files in git?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.


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"


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.


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..."

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.