How To Ignore Files in Git | .gitignore file Examples

Steps To Ignore Files in Git | .gitignore file Tutorial

Sometimes we don't want to push or check in some files in our system to git repository, using gitignore we can achieve this and ignore files and folders(directories). for that we need to create .gitignore file

what is .gitignore file

Gitignore file is a normal text file under your repository where we can mention the files and folders(directories) which we want to ignore.

how to ignore files in git

How to ignore files in git

Create a .gitignore file in your repository and  mention the file names in it, which you want to ignore.

$ cat .gitignore

.DS_Store
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored)
!/.mvn/wrapper/maven-wrapper.jar

in the above file  ignored .DS_Store file,target/, pom.xml.tag, pom.xml.releaseBackup, pom.xml.versionsBackup, pom.xml.next, release.properties, dependency-reduced-pom.xml, buildNumber.properties, .mvn/timing.properties,

How ignore folders or directories in git

open the .gitignore file and mention the path of directories names and at the end of the directory name you should add /

in the above image you can see target/ , Here git will ignore the all the files inside target directory.

Wildcard Match *

using * we can ignore files that matches with our condition. if you mention *.log it will ignore all files that ending with .log

Wildcard Match !

we can use a prefix of ! to not ignore file. if you dont want to ignore a file we can use this prefix.

comments

we can add comments in .gitignore file mentioned prefix #

what if you already added the file to staging

we can revert or unstage a file by using git rm --cached filename
this will move file from staging to working directory. after moving to working directory we can add the file .gitignore

ignore files that already exist on the repository.

if you want to remove the files that already exist in our repository we have to follow below steps

Method: 1

Copy the files which you want to ignore to a temporary folder
Remove them from you project folder or repository.
Commit the changes which remove those files from the repository
Re-added those files to my project folder

Method: 2

ignore with below command

git update-index --assume-unchanged <file>

If you wanna start tracking changes again

git update-index --no-assume-unchanged <file>

 

 

  • git ignore folder
  • multiple gitignore files
  • git ignore file command
  • git exclude file from commit
  • how to ignore the files in git
  • gitignore file not working
  • gitignore not ignoring file
  • git ignore untracked files

Leave a Reply

Your email address will not be published. Required fields are marked *