Git lesson 5: ignoring files and amending commits

I hope that you have been following these lessons and found them useful. One of the key things about learning Git is that you have to use it regularly in order to remember the names of the various commands you use most often. Also, the more you use Git, the more you will start to think about your documents and workflow from a version control perspective.
This lesson will show you how to ignore certain files or folders in your repository, and also how to fix small errors when making commits.
git rm –cached
There may come a time when you want to stop tracking a file in your repository, but not remove it from the folder. This can be done using the git rm --cached
command. For example, let’s stop tracking the doc1.txt
you created previously.
git rm --cached doc1.txt
The output on the terminal should indicate rm doc1.txt
. If you run the git status
command, you should get the following output:
08:07 ~/.../project_repo$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: doc1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc1.txt
Git is saying that you have deleted doc1.txt
, and that you now have an untracked file in your repository called doc1.txt
. This is correct, so let’s go ahead and commit this change:
git commit -m 'Cach doc1'
git rm. In the current example, we used
git rm --cached
to stop tracking a file, but leave this file in the folder. If you want to stop tracking the file and delete the file, usegit rm
.
.gitignore
In the above example, you may have noticed that onced you stopped tracking doc1.txt
, Git told you there was an untracked file in your repository. While a single untracked file is not so bad, you will often have many files that you don’t want to track.
For example, what if one of the programs you use continually generates log files. While it is a good idea to keep a backup of these files, there is no need to have them under version control given that each log file is unique and will never be modified.
Wouldn’t it be great if there was a way to tell Git to ignore certain files or folders? There is! Git looks for a file called .gitignore
in the main (or root) folder of your repository, and any file or folder listed in .gitignore
will be ignored by Git. Let’s create a .gitignore
file for our repository (i.e., .gitignore
has to be in the base project_repo
folder).
For now, let’s tell Git to ignore doc1.txt
, all log files, and the dat
folder. You can write this file using the text editor of your choice, but for simplicity I will write it using the echo
command (remember, lines starting with #
are comments and are ignored by Git):
echo "# Ignore doc1.txt" > .gitignore
echo "doc1.txt" >> .gitignore
echo "# Ignore all log files" >> .gitignore
echo "*.log" >> .gitignore
echo "# Ignore dat folder" >> .gitignore
echo "dat/" >> .gitignore
Note that you specified *.log
to ignore the log files. The asterisk (*
) is a way to tell Git to ignore all files with the .log
file extension. The *
is a wild card that can represent any text of any length. You did not use this wild card to ignore doc1.txt
because *.txt
would have told Git to ignore all text files, which is what you don’t want to do. Finally, we told Git to ignore the dat
folder by including the name of the folder followed by a forward slash.
You will modify the .gitignore file as your project progresses, and your colleagues who will work on their own copy of the repository will want to ignore the same files. Thus, it is a good idea to add your .gitignore
file to your repository; i.e., have Git track changes to this file. Let’s do that now (but remember, it is good practice to verify the status of your repository before and after various actions):
git status
git add .gitignore
git commit -m 'Add .gitignore to repo'
git status
git commit –amend
There will be times when, in your haste, you commit changes prematurely. One solution is to make another incremental commit to fix your mistake, but this leads to a messy git log. A better solution is to add the missing changes to the previous commit.
This can be done using the git commit --amend
command. But first, let’s create and add a new file to our repository (remember, you can write the content of this file in your favourite text editor).
# Start a new verion of doc1
echo "Line 1 of document 1." > doc1_new.txt
echo "Line 2 of document 1." >> doc1_new.txt
echo "Line 3 of document 1." >> doc1_new.txt
echo "Line 4 of document 1." >> doc1_new.txt
echo "Line 5 of document 1." >> doc1_new.txt
git add doc1_new.txt
git commit -m "Added new file to repo"
git slog
git slog. This command is an alias we created in a previous lesson. It provides a short version of our Git log.
Let’s pretend you are not happy with your commit message. You can change (or amend) the commit message using the git commit --amend
command:
git commit --amend -m "Add new doc1 to repo"
git slog
Great, you fixed the commit message. Unfortunately, you forgot to add an important detail to doc1_new.txt
, as well as a new index.txt
file.
Let’s make these changes and amend our previous commit (rather than have a new, incremental commit).
echo "Line 6 of document 1." >> doc1_new.txt
echo "Start of the index file" > index.txt
git add index.txt
git add doc1_new.txt
git commit --amend -m 'Add index and new doc1 to repo'
git slog
Summary
In this lesson you learned to stop tracking single files using git rm --cached
, completely ignore files and folder using a .gitignore
file, and amend commits if you make mistakes in your commit messages or forget to add a change to a commit. In the next lesson you are going to learn about branches, a very powerful aspect of Git!