Open In App

What is Git-Ignore and How to Use it?

There are various types of files we might want the git to ignore before committing, for example, the files that are to do with our user settings or any utility setting, private files like passwords and API keys. These files are not of any use to anyone else and we do not want to clutter our git. We can do this with the help of “

Gitignore

.gitignore file is used in a git repository to ignore the files and directories which are unnecessary to project this will be ignored by the git once the changes as been committed to the Remote repository. The type of files which will gets ignored are the mainly temporary files and the files which should not be versioned.



gitignore File (.gitignore)

When the git commits are pushed to to the remote repository to want to ignore some files then we will use .gitignore file is a text file that specifies which files and directories Git should ignore. Which will reduce the size of the repository.

Creating .gitignore file (.gitignore)

Step 1:Open your terminal/cmd and change your directory to the folder where your files are located. You can use ” ls -a” command to view its contents.



cd directory(or)folder
ls -a


Here, the project files are saved inside folder named story which is further inside web development. Here, we want git to ignore the secrets.txt file.

Step 2: Create .gitignore File inside the project folder.

Step 3: Write the name of the files you want to ignore in the .gitignore text file. Each file name should be written in a new line .

Step 4: Initialize git in your terminal. Add these files to your git repository and commit all the changes with an appropriate message.

git init
git add .
git commit -m "your message"


Step 5: Check the status of the git repository. The file(s) added to the .gitignore text file will be ignored by the git every time you make any changes and commit.

git status


Examples

# Compiled class file
*.class

# Log file
*.log

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files
*.jar

*.war

*.nar

*.ear

*.zip

*.tar.gz

*.rar


gitignore file Patterns and Format (.gitignore)

gitignore Rules (.gitignore)

Local and Personal Git Ignore Rules (.gitignore)

Local Git Ignore Rules

There will one .gitignore file in the local repository in this file and all the files and the directories which should be ignored are mentioned and it will be shared with the others unless you commit and push it to the remote repository. It applicable only to single and particular repository where the file to be ignored.

Personal Git Ignore Rules

You can set up an .gitignore file which can be applied to the all the git repositories. It will not shared with any other collaborators.

Steps To Create a global .gitignore file

Step 1: Create .gitignore file as following.

touch ~/.gitignore_global

Step 2: Add the required rules to the .gitignore file as shown below.

# ~/.gitignore_global
*.swp
.DS_Store

Step 3: Know we need to make this file as an golbal .gitignore file.

git config --global core.excludesfile ~/.gitignore_global

How to undo any commits?

Before using .gitignore to ignore certain files, if you have already committed files you didn’t want to here’s how you can undo it. Use the following command on Git Bash to undo a commit:

git rm --cached -r



Here, “rm” stands for remove while “r” stands for recursive.

Note: Head over to the GitHub and search for gitignore repositories, you will find a list of gitignore repositories contributed by various people.

FAQs On gitignore

1. gitignore is Not Working

Following are the some of the causes that your gitignore file causes issues.

  • .gitignore file was not tracked by the git. Use (git add .gitignore)
  • The pattern in the .gitignore file was not correct.
  • FIles and directories that which you want to ignore you may be traced already.

2. gitignore Generator

.gitignore generator is an toll which is used to create .gitignore file for the git repositories they are available for the both online and desktop.

3. gitignore file for Nodejs

.gitignore file where we mention the file and directories which you want to ignore following is the sample .gitignore file for the nodejs.

node_modules/

npm-debug.log

4. gitignore All File in the Folder

If you want ignore all the file in the folder you can use the following command as.If you want to ignore the files in the folder name called GFG_Folder then you can use as follows.

GFG_Folder/*


Article Tags :
Git