Jose Robledo
04 March 2025
What if we have files that we do not want Git to track for us, like backup files created by our editor or intermediate files created during data analysis? Let’s create a few dummy files:
and see what Git says:
Putting these files under version control would be a waste of disk space. What’s worse, having them all listed could distract us from changes that actually matter, so let’s tell Git to ignore them.
We do this by creating a file in the root directory of our project
called .gitignore
:
Type the text below into the .gitignore
file:
*.png
receipts/
Save the file and exit your editor.
Verify that the file contains the files to ignore.
Can (usually must) track .gitignore. Helps others
If we want to override our ignore settings, we can use
git add -f
to force Git to add something. For example,
git add -f a.csv
.
We can also always see the status of ignored files if we want:
Nested ignoring also works.
Also possible to ignore all extensions for example, except a given
file by using !
in your .gitignore
*.png # ignore all png files
!final.png # except final.png
Note also that, if you’ve previously committed .png
files in this lesson, they will not be ignored with this new rule. Only
future additions of .png
files to the root directory will
be ignored.
Given a directory structure that looks like:
How would you ignore all of the contents in the receipts folder, but
not receipts/data
?
Hint: think a bit about how you created an exception with the
!
operator before.
Assuming you have an empty .gitignore file, and given a directory structure that looks like:
receipts/data/market_position/gps/a.dat
receipts/data/market_position/gps/b.dat
receipts/data/market_position/gps/c.dat
receipts/data/market_position/gps/info.txt
receipts/plots
What’s the shortest .gitignore
rule you could write to
ignore all .dat
files in
receipts/data/market_position/gps
? Do not ignore the
info.txt
.
You wrote a script that creates many intermediate log-files of the
form log_01
, log_02
, log_03
, etc.
You want to keep them but you do not want to track them through
git
.
Write one
.gitignore
entry that excludes files of the form
log_01
, log_02
, etc.
Test your “ignore pattern” by creating some
dummy files of the form log_01
, etc.
You find that the file log_01
is
very important after all, add it to the tracked files without changing
the .gitignore
again.
Content