While working on a project, you may have some files or directories that you do not want them to be committed. There are several reasons that you might not want to add some files to your repository:
- A huge dataset file
- Private files, like passwords and tokens
- system or application specific files/configurations, like
.idea
forpycharm
and.ipynb_checkpoints
- ...
- Simulating a use case for gitignore
- How to add .gitignore at the time of project creation in GitHub
- How to create .gitignore for various use cases
To make sure that these unwanted files will not unintentionally be committed, you should append your files
and directories' paths to a file called .gitignore
. This file is recognized by git
, and whenever a file's
name appears in it, the git
, simply, ignores it and does not even show in the output of the status
command.
For the sake of this project, consider that we have a directory that contains a text file including some passwords. To make it more realistic, imagine that you are working in a team and these passwords are unique for each developer. Therefore, by committing this file you may overwrite others passwords our reveal yours, which both of them should be avoided!
So, first let's create a folder with a text file in linux:
cd git_01
mkdir credentials
cd credentials
cat >> credentials.txt
username: pooya-mohammadi
password: **************
Note: If you are Windows user, simply graphically create a directory including a text file containing the following text or what ever text you desire:)
username: pooya-mohammadi
password: **************
Let's see the output of the git status
command:
As you can see, the git
itself is suggesting you to commit the file. If you have several files to commit, the chances
are that you may commit credentials
along the others by mistake.
touch .gitignore # If you are on Windows, create a new and empty text file graphically and rename it to .gitignore
cat >> .gitignore
credentials/
cat >> .gitignore
git status
Note: create an empty text file in windows
Image Notes:
- make sure the spelling of
.gitignore
is correct! - If you are a Windows user, create a text file with no
.txt
extension, open the file as atxt
file, and typecredentials/
in it. - The
.gitignore
file is recognized by thegit
and the suggestion for committingcredentials
directory is removed.
git add .gitignore
git commit -m "Add credentials directory to gitignore"
git push origin main
git status
Done! With this, everyone can benefit from this file!
GitHub offers various templates for .gitignore
. These templates are mainly based on various programing languages:
After creating your project .gitignore
will be created automatically.
Image Notes:
- I choose gitignore to be created for
python
programing language - There are several files specific to
python
that should not be included in projects' repositories because they system specific, like pycache, etc.
Consider you have a project which should be implemented in python, and your chosen IDEs are jupyter notebook and Pycharm. In this case, ignoring specific files for python won't suffice. In this regard, there is a very useful website https://www.toptal.com/developers/gitignore that offers predefined templates for various IDEs and programing languages.
- Simply add the programing language and IDEs that you want
- click on
Create
to create thegitignore
file. - Copy/save the output files in a .gitignore file.
- Add the file to your project and make sure that you will not commit any specific files to your repository!
PS: Many thanks to FeryET for introducing this website to me.