title | teaching | exercises | questions | objectives | keypoints | ||||
---|---|---|---|---|---|---|---|---|---|
Ignoring Things |
5 |
0 |
|
|
|
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 in the inflammation
directory:
$ mkdir results
$ touch a.dat b.dat c.dat results/a.out results/b.out
{: .bash}
and see what Git says:
$ git status
{: .bash}
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
a.dat
b.dat
c.dat
results/
nothing added to commit but untracked files present (use "git add" to track)
{: .output}
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
:
$ nano .gitignore
$ cat .gitignore
{: .bash}
*.dat
results/
{: .output}
These patterns tell Git to ignore any file whose name ends in .dat
and everything in the results
directory.
(If any of these files were already being tracked,
Git would continue to track them.)
Once we have created this file,
the output of git status
is much cleaner:
$ git status
{: .bash}
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
{: .output}
The only thing Git notices now is the newly-created .gitignore
file.
You might think we wouldn't want to track it,
but everyone we're sharing our repository with will probably want to ignore
the same things that we're ignoring.
Let's add and commit .gitignore
:
$ git add .gitignore
$ git commit -m "Add the ignore file"
$ git status
{: .bash}
# On branch main
nothing to commit, working directory clean
{: .output}
As a bonus, using .gitignore
helps us avoid accidentally adding to the repository files that we don't want to track:
$ git add a.dat
{: .bash}
The following paths are ignored by one of your .gitignore files:
a.dat
Use -f if you really want to add them.
{: .output}
If we really want to override our ignore settings,
we can use git add -f
to force Git to add something. For example,
git add -f a.dat
.
We can also always see the status of ignored files if we want:
$ git status --ignored
{: .bash}
On branch main
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
a.dat
b.dat
c.dat
results/
nothing to commit, working directory clean
{: .output}
Given a directory structure that looks like:
results/data results/plots
{: .bash}
How would you ignore only
results/plots
and notresults/data
?As with most programming issues, there are a few ways that you could solve this. If you only want to ignore the contents of
results/plots
, you can change your.gitignore
to ignore only the/plots/
subfolder by adding the following line to your .gitignore:
results/plots/
If, instead, you want to ignore everything in
/results/
, but wanted to trackresults/data
, then you can addresults/
to your .gitignore and create an exception for theresults/data/
folder. The next challenge will cover this type of solution.Sometimes the
**
pattern comes in handy, too, which matches multiple directory levels. E.g.**/results/plots/*
would make git ignore theresults/plots
directory in any root directory. {: .solution} {: .challenge}
How would you ignore all
.data
files in your root directory except forfinal.data
? Hint: Find out what!
(the exclamation point operator) doesYou would add the following two lines to your .gitignore:
*.data # ignore all data files !final.data # except final.data
{: .output}
The exclamation point operator will include a previously excluded entry. {: .solution} {: .challenge}
Given a directory structure that looks like:
results/data/ward/patient/a.data results/data/ward/patient/b.data results/data/ward/patient/c.data results/data/ward/patient/info.txt results/plots
{: .bash}
What's the shortest
.gitignore
rule you could write to ignore all.data
files inresult/data/position/gps
? Do not ignore theinfo.txt
.Appending
results/data/ward/patients/*.data
will match every file inresults/data/position/gps
that ends with.data
. The fileresults/data/ward/patient/info.txt
will not be ignored. {: .solution} {: .challenge}
Given a
.gitignore
file with the following contents:*.data !*.data
{: .bash}
What will be the result?
The
!
modifier will negate an entry from a previously defined ignore pattern. Because the!*.data
entry negates all of the previous.data
files in the.gitignore
, none of them will be ignored, and all.data
files will be tracked.{: .solution} {: .challenge}
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 throughgit
.
Write one
.gitignore
entry that excludes files of the formlog_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.Discuss with your neighbor what other types of files could reside in your directory that you do not want to track and thus would exclude via
.gitignore
.
- append either
log_*
orlog*
as a new entry in your .gitignore- track
log_01
usinggit add -f log_01
{: .solution} {: .challenge}