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 is an auto-generated file inside the project folder that ignores/prevents files to get committed to the local and remote repositories.
How to use Git-Ignore?
.gitignore can be used in Git with the help of following steps:
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 repository. The file(s) added to the .gitignore text file will be ignored by the git every time you make any changes and commit.
Some common patterns and format for Git-Ignore :
- Blank Line: A blank line doesn’t refer to any file name, so we can use it to separate two file names for the ease of reading .
- #: A line beginning with the # symbol refers to a comment .However if # is used as a pattern then use backslash (“\”) before the # symbol so that it is not misunderstood as a comment.
- /: It is used as a directory separator i.e to include directories, for example webdev/ .
- *.extension_name: For example *.txt and *.log can be used to match ALL the files that have .txt and .log as their extension respectively.
- **/any_name: It is used to match any file or directory with the name any_name.
- any_name/**: It is used to match anything that is inside the directory of the name any_name. for example webdev/** matches all the files inside webdev directory.
# Compiled class file *.class # Log file *.log # Mobile Tools for Java (J2ME) .mtj.tmp/ # Package Files *.jar *.war *.nar *.ear *.zip *.tar.gz *.rar
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.
- How to install, configure and use GIT on ubuntu?
- Use of Submodules in GitHub
- An Ultimate Guide to Git and Github
- Git-Checkout And Merge
- Jenkins and GIT Integration using SSH Key
- GitHub App to Build Form using Typeform and Probot
- Difference Between Git and GitHub
- How to Build Portfolio Website And Host It on GitHub Pages?
- Merge Conflicts and How to handle them
- Difference Between GIT and SVN
- Difference between MERCURIAL and GIT
- Common Git Problems and Their Fixes
- Difference Between Bitbucket and GitHub
- Difference Between GitLab and GitHub
- Difference between CVS and GitHub
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.