Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell git to only include certain files instead of ignoring certain files?

Tags:

git

gitignore

People also ask

How do I make git not ignore a file?

Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "! important!. txt".

How do you exclude or ignore some files from git commit?

Use an exclude file The exclude file lets Git know which untracked files to ignore and uses the same file search pattern syntax as a . gitignore file. Entries in an exclude file only apply to untracked files, and won't prevent Git from reporting changes to committed files that it already tracks.

What is the file used to tell git to ignore certain files?

The . gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo. / will ignore directories with the name.


I haven't had need to try this myself, but from my reading of TFM it looks like a negated pattern would do what you want. You can override entries in .gitignore with later negated entries. Thus you could do something like:

*.c
!frob_*.c
!custom.c

To have it ignore all .c files except custom.c and anything starting with "frob_"


create .gitignore file in your repository and you want to track only c files and ignore all other files then add the following lines to it....

*
!*.c

'*' will ignore all files

and ! will negate files be to ignored....so here we are asking git not to ignore c files....


The best solution to achieve this

create .gitignore file in repository root, and if you want to include only .c file then you need to add below lines to .gitignore file

*.*
!*.c

this will include all .c file from directory and subdirectory recursively.

using

*
!*.c

will not work on all version of git.

Tested on

git version 2.12.2.windows.2


If you need to ignore files but not a specific file inside a directory, here is how I did it:

# Ignore everything under "directory"
directory/*
# But don't ignore "another_directory"
!directory/another_directory
# But ignore everything under "another_directory"
directory/another_directory/*
# But don't ignore "file_to_be_staged.txt"
!directory/another_directory/file_to_be_staged.txt