
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 update the .gitignore file to contain the files and directories that you do not want to track. To ignore a file in the same directory as the .gitignore file add the name of the file to the .gitignore file. For example, if foo.txt is in the same directory as .gitignore, then you can have foo.txt in .gitignore.
foo.txt
To ignore all files in the same directory as the .gitignore file containing a pattern such as "foo" add *<pattern>* to the .gitignore file. For example, if food.txt is in the same directory as .gitignore, then you can have *foo* in .gitignore.
*foo*
To ignore all files at and below a directory below whatever directory the .gitignore file is in add the name of the directory following by a forward slash to the .gitignore file. For example, if there is a directory named "logs" in the same directory as the .gitignore file then you would have "logs/" in the .gitignore file.
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