user3556757
user3556757

Reputation: 3619

.gitignore everything except

I have a large directory full of files and subdirectories.

Two or three of the files are excel spreadsheets in the main directory that I need to transfer to and from home regularly.

My idea was to make a git repository that only includes those few files, then use git to pass the files back and forth via a private repository on github.

I am unable to move/segregate these files to their own directory elsewhere, they need to stay where they are (amongst the hundreds of other files). So if I create a repository on that directory, it's going to include a whole lot of files that I don't want included. It's like I need to create the repository with a whitelist that says, "exclude everything except file1.xls, file2.xls".

My git skills are weak. Can anyone advise the cleanest way to do this, or if there is a more idiomatic way to do this?

Upvotes: 2

Views: 178

Answers (1)

Jake Worth
Jake Worth

Reputation: 5852

As @matt pointed out, Git only "sees files you explicitly ask it to see". A simple solution here would be to not check in anything but your desired files.

$ git add file1.xls file2.xls
$ git commit -m "Here are the Excel files"

If you want the ability to checkin everything with $ git add ., or just avoid the chance of adding one of the other files by accident, one technique would be to ignore everything via *, and then include your specific files or matching file extensions.

Here's a sample .gitignore.

# Ignore everything
*
# Include specific files excluded by a previous pattern 
!file1.xls

Replace file.xls with *.xls if you can get away with checking in all the Excel files.

From the docs:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. 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".

https://git-scm.com/docs/gitignore

Upvotes: 1

Related Questions