git-mastery
To round out this tour, let’s take a look at how you can hide files from Git.
There are several reasons why you might want to hide files from Git. Sometimes, the project file you are working on contains a lot of generated files that may not be necessary to include in any snapshots. Or you might be storing your secret keys in a file and you don’t want that file being tracked by Git to avoid sharing those secrets with others.
Whatever the reason, Git supports the use of .gitignore to
…ensure that certain files not tracked by Git remains untracked
Which essentially means hiding files from Git, which we will refer to as “ignoring files” from here on out.
.gitignore structure.gitignore is actually a plain text file, but each line specifies a pattern that corresponds to files or folders.
For example, the following .gitignore file hides the filea.txt file, and the nested/ folder.
filea.txt
nested/Note that if you ignore a folder, ALL of its contents are automatically ignored as well, unless you prefix it with !:
# ...
!nested/okay.txtIn the above, the nested/okay.txt will not be ignored by Git, but every other file in nested/ will continue to be nested. You may also have noticed but you can specify comments in .gitignore using #.
Let’s start with creating your very first .gitignore file!
1
Create the file
Create the file with name .gitignore. It must be named this.
touch .gitignore2
Finding a candidate to ignore
Run git status to find the untracked files (these are files that have never been seen before by Git). They will be your first files to ignore.
We specifically choose untracked files for simplicity.
git statusBased on the output, we will pick filea.txt for this demonstration to ignore.
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
filea.txt
script.py
nothing added to commit but untracked files present (use "git add" to track)3
Adding contents to .gitignore
Once you have picked the file(s) you want to ignore, edit .gitignore to include the filenames:
filea.txt4
Adding contents to .gitignore
Once you have picked the file(s) you want to ignore, edit .gitignore to include the filenames:
filea.txt5
Verifying the hiding
Run git status once again to verify that the .gitignore is working:
git statusIf all went well, you should not see the files you added in step (3) to appear in git status anymore.
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
script.py
nothing added to commit but untracked files present (use "git add" to trackInstead, you should see that .gitignore appears as an untracked file!
6
Creating a snapshot for .gitignore
Similar to what we’ve been doing every hands-on in this tour, create a snapshot with .gitignore!
There are a lot more complexities that go into .gitignore.
For instance, try adding a tracked file into .gitignore. You may notice that any future changes to the file will be ignored, but it would have existed in past snapshots. What if you want to ignore the file entirely?
Or how about ignoring files that might follow a given pattern?
We will discuss these nuances in a detour!
🚧 This detour is still a work in progress! Come back later! 🚧
bash download.sh nothing-to-hideTest your understanding of basic .gitignore syntax!
Congratulations on coming to the end of this tour! Hopefully, you are better equipped with answering the following questions:
Return to the tours page to check out more Git content!
| Problem set | Download |
|---|---|
| nothing-to-hide | bash download.sh nothing-to-hide |
For more problem sets, visit the problems directory