Bootstrap FreeKB - Git (Version Control) - List files and directories to ignore using the .gitignore file
Git (Version Control) - List files and directories to ignore using the .gitignore file

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Let's say you've cloned the example.git repository to the /usr/local/git directory on your PC and there is already a file named hello.txt in the example.git repository.

~]# ls -l /usr/local/git
 8437245  8 -rw-r--r--.  1 john.doe users  6353 Nov 29 05:12 hello.yml

 

And then you create the foo.txt and bar.txt files and the logs directory.

touch /usr/local/git/foo.txt
touch /usr/local/git/bar.txt
mkdir /usr/local/git/logs

 

The git status command will now show that the foo.txt and bar.txt files and the logs directory are untracked

~]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo.txt
#       bar.txt
#       logs/

 

The .gitignore file is used to instruct Git to not track certain files and directories. If the .gitignore file does not exist, you can create the .gitignore file.

touch .gitignore

 

And then updated the .gitignore file to contain the files and directories that you do not want to track. In this example, the hello.txt and foo.txt files and any file or directory beginning with bar and anything below the logs directory will be ignored.

hello.txt
foo.txt
bar*
logs/

 

Then use the git add command to add the .gitignore file to the currently selected branch of the cloned repository.

git add .gitignore

 

And commit the .gitignore file.

git commit -m "initial commit" .gitignore

 

And push the .gitignore file to HEAD.

git push origin HEAD

 

Now the git status command should no longer identify that the foo.txt and bar.txt files and the logs directory as untracked. 

~]# git status
# On branch master
nothing to commit, working directory clean

 

However, we aware that .gitignore will not ignore files that have already been committed in the repository. In this example, hello.txt will not be ignore. One option here would be to remove hello.txt from the repository.

git rm hello.txt
git commit -m "removed" hello.txt
git push origin HEAD

 

Or, you may want to go with the git rm command with the ---cache flag to remove the file from git but the actual file will not be deleted.

git rm --cached hello.txt

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fadaec in the box below so that we can be sure you are a human.