I have now worked in 5 different jobs. Every single one used some kind of version control system.
Even though there are many of them, the most used by far is Git.
When working on a project, we often need to create or modify a file we don't want to be added to the repository. The most common scenario for a backend developer is probably some local file configuration to access a local database or some kind of parameters that need to be passed to the execution of the server itself.
For a frontend developer, you may be sick of hearing this but in case you forgot it, I'll remind you: Never upload the node_modules folder! The node_modules folder contains all dependencies of a project and is generated via npm install. This is equivalent to the dependencies folder of a Java project using Maven. Imagine uploading that to your Git repository 😅
Table of Contents
1. Introduction
Git offers shared and local configuration for ignoring files.
2. .gitignore file
The most used approach is probably the .gitignore file. It is a file located in every local git project. As you can imagine, given that the file starts with a dot, it is a hidden file.
Its exact location may vary depending on your IDE. In IntelliJ Idea it's inside the .idea folder. For Visual Studio, it's usually in the project's root.
Here's a typical .gitignore from a generated React/Node project:
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*The .gitignore file is shared with the team because it is uploaded to the Git repository. If we, as a team, want to ignore some files or folders for all of us, we just add those to the .gitignore file.
For a private file that shouldn't be uploaded, we need a local ignore rule. We can't do it through .gitignore.
3. Global exclude file
There is a file at .git/info/exclude whose purpose is to ignore private files. This configuration won't be pushed to the repository. It stays local.
The idea is the same as .gitignore, but this file is never shared. It is useful for local configuration files.
4. Example
Say I have a Main.java file and a local.conf file, but I only want to add Main.java to the remote repository.
Since local.conf is my personal and private local configuration, I'll add it to .git/info/exclude:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
local.confBut git status still shows it as added:
PS D:\Projects\git-testing> git status
On branch feature-foo
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: local.conf
new file: src/main/java/Main.javaKeep in mind: if the file was previously checked in, you'll need to run:
git rm --cached local.confNote: Sometimes you'll need the
-f(force) flag.
After that, git status shows our file has disappeared from tracking:
PS D:\Projects\git-testing> git status
On branch feature-foo
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: src/main/java/Main.java5. Conclusion
For local configuration, add the file to excludes. For files the whole team should ignore, discuss adding a rule to .gitignore.
Test Your Understanding
Question 1 of 5 · Score: 0/0 correct
Your first answer counts toward your score.
