637

Is there a way to force git to add a file despite the .gitignore file?

4
  • 10
    A better question may be - why would you want to do that? If you want a file tracked, unignore it (prefix its pattern in the .gitignore file with !, e.g. !dont/ignore/this/file). Commented Sep 7, 2015 at 12:57
  • 8
    @OhadSchneider This is useful if you force commit build file(s) to another branch. A few projects do this such as StrongLoop for safe deployments Commented Dec 21, 2015 at 2:15
  • 8
    @OhadSchneider For us, it's more because most developers install git with Visual Studio 2015, which adds a global gitignore to the user folder. This excludes files like *.dll and *.exe. That is great for our new projects which only use nuget packages that are retrieved during build, but for a small number of older projects, we still use dll's that are manually referenced in the solution. For those projects, we would like to manually include them, instead of telling every developer to comment the *.dll and *.exe files in the global gitignore. Commented Feb 17, 2016 at 9:11
  • 7
    @Nullius Can't you just unignore them in a higher level .gitignore (say, add a .gitignore in the same folder as the dll, or one above, or something)? Commented Feb 17, 2016 at 13:41

8 Answers 8

846

See man git-add:

   -f, --force
       Allow adding otherwise ignored files.

So run this

git add --force my/ignore/file.foo
Sign up to request clarification or add additional context in comments.

11 Comments

Uhm that doesn't work, on git status the file is still not shown
Well, I tested it to make sure it really works and it does. Can you describe your environemnt (OS, git version ...)? This is what I've basically done: echo "/foo" >> .gitignore; echo "bar" > foo; git add foo # should throw an error; git add -p foo # works (cannot paste newlines in comment, execute the commands 1 by 1
Same happened to me, I had previously ran git update-index --asume-unchanged on my files. I had to undo it with --no-assume-unchanged and it worked
This only works for me using wildcards, using git 1.9.5 on Windows, even after trying --no--assume-unchanged. "git add - f <filename>" does nothing, but "git add -f *" works. Using TortoiseGit also works.
@mhenry1384 The reason that it works for wildcard only in Windows may be due to how the path separator is used. For me slash "/" works but backslash "\" doesn't. The fact that cmd/PowerShell convert "/" to "\" for us makes it easy to run into this.
|
34

Despite Daniel Böhmer's working solution, Ohad Schneider offered a better solution in a comment:

If the file is usually ignored, and you force adding it - it can be accidentally ignored again in the future (like when the file is deleted, then a commit is made and the file is re-created.

You should just un-ignore it in the .gitignore file like this: Unignore subdirectories of ignored directories in Git

6 Comments

This is correct. I was using the --force option for small data files, and then rearranged a bit of folder structure and clobbered all the force-tracked files. HOWEVER: in .gitignore use !specific-file-name.txt without the folder hierarchy, this way tracking will follow the file around the repo.
It depends on the scenario, e.g. sometime you want to exclude "1.txt" in all folders, and sometime "folder1/*.txt"
That's not a "better" solution. It's a solution to a different problem.
@aij - I disagree, my answer offers another "way to force git to add a file despite the (current state of) .gitignore file". Although the extra text in the parentheses was added by me - I think it is also implied from the text of the question. Anyway, I think that this is also important information to whoever that considers using the "--force" option
This is not a better solution becasue it would unignore the file that should be ignored. I use this gitignore method to push deploy files with the values marked as "YOUR_ID_HERE" and such so I do not want people to push their personal info but if there is a change to the deploy i still need to update the file upstream
|
23

How to unignore some select contents (files or folders) within an ignored folder

If you have ignored the contents of a directory like this:

/ignored_dir/*    # ignore the **contents of** this dir!

then you can unignore a file or directory inside there like this:

!/ignored_dir/special_file_to_include   # Do NOT ignore this file--DO include it!
!/ignored_dir/special_dir_to_include/   # And do NOT ignore this dir--DO include it!

BUT, if you have ignored the "ignored_dir" directly like this instead:

/ignored_dir/     # ignore this directory **directly**

then the above ! rules which attempt to unignore some files or folders will NOT work! So, in that case, switch from using this style: /ignored_dir/ to this style: /ignored_dir/*, and then add your ! rules as shown above to unignore something within that dir!

References:

  1. Unignore subdirectories of ignored directories in Git
  2. [my eRCaGuy_dotfiles repo with an example .gitignore file containing these notes and demos]: eRCaGuy_dotfiles/.gitignore

2 Comments

This might've been better as an edit (albeit a rather extensive one) on the A-S answer above, as it is the same answer and that one has 24 votes so is always going to appear higher. (Your version of it is much more helpful though.)
@DarrenCook, I didn't want to do an extensive edit to someone else's answer. One of the key appeals of Stack Overflow is personal ownership of answers and content, and large edits disturb that. I think my answer is much more helpful too, and I'm hoping to keep ownership over it myself to make it just how I like, and to get some credit along the way. Eventually, if people like you upvote it, it will float towards the top. I've seen it dozens of times. I added it because it provides new and lacking content and insight which wasn't already present on this page.
6

In cases where the ignored file is enforced by a git hook using git update-index --assume-unchanged <file>, a forced git add (git add -f <file>) won't work. In such cases you will need to manually update the index, as shown below.

git update-index --no-assume-unchanged path-to/ignored-file.ext

git add ...
git commit ...
git push ...

git update-index --assume-unchanged path-to/ignored-file.ext

Comments

4

Another way of achieving it would be to temporary edit the .gitignore file, add the file and then revert back the .gitignore. A bit hacky I feel

1 Comment

I agree, this is not what you want to do.
2

A better question may be - why would you want to do that? If you want a file tracked, unignore it (prefix its pattern in the .gitignore file with !, e.g. !dont/ignore/this/file). – Ohad Schneider

Sometimes we might change the API_KEY in a config file and need to ensure we don't accidentally upload it via git add *.

config.json:

{
    "DEBUG": true,
    "TESTCASE_DIR": "testcases/",
    "OUTPUT_DIR": "output",  <--- a new attribute, we want to modify it 
                                  and update to the git repository
    "API_KEY": "********-****-****-****-************",
    "API_URL": "https://api-ffm-stg.twcc.ai/api/models/conversation",
    ...
}

Solution (for example: config.json)

If git add --force config.json is not having the expected effect, please try the following steps to resolve the issue:

  1. Ensure .gitignore correctly ignores config.json:

    Make sure your .gitignore file contains config.json:

    __pycache__/
    *.log
    config.json
    
  2. Remove config.json from version control (this step is crucial):

    If config.json was previously tracked, you need to remove it from Git's index and then re-add it:

    git rm --cached config.json
    
  3. Commit the changes (this step will remove the current version of config.json):

    git commit -m "Remove config.json from version control"
    
  4. Force-add config.json:

    Now you can modify/update the config.json and force-add it:

    git add --force config.json
    
  5. Commit again:

    Commit the force-added config.json:

    git commit -m "Force add config.json despite .gitignore"
    
  6. Push to the remote repository:

    Push the changes to GitHub:

    git push origin master
    

enter image description here

These steps should solve your issue. If you still cannot force-add config.json, ensure your Git client has no configuration issues and that there are no Git hooks or settings interfering with the process.

Critical Next:

  1. DO NOT track the file config.json again:
    $ git status
    # try to modify `config.json` again, e.g. edit the API_KEY and save it
    # check the status again to see if it is be tracked
    $ git status
    
    # the critical action
    $ git update-index --assume-unchanged config.json
    
    # check the status again to see if it is be tracked
    $ git status
    

Comments

0

If you are using eclipse/STS, then you can do this:

  1. Right click on the file you want to unignore.
  2. Click on 'Team' post that.
  3. Select 'Advanced'.
  4. And finally click on 'No Assume Unchanged'
  5. Now you can see that your file is unignored.

Comments

0

The first option is git add -f ...:

git add -f my/ignored/file.txt

# or

git add --force my/ignored/file.txt

See git add --help (https://git-scm.com/docs/git-add)

Is git add -f doing nothing?

If git add -f is still not working, the following solution works for me:

# Move the file to a place were you can add it
mv my/ignored/file.txt git-add-file.txt

# Add the file with `git add`
git add git-add-file.txt

# Restore the original path of the file using GIT
git mv git-add-file.txt my/ignored/file.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.