- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to ignore a previously committed file in Git repository?
The .gitignore file does not operate on files that are already committed. To ignore changes to a file that was accidentally staged or committed the following should be done −
Step 1 − Remove such files or directories from the staging area
Step 2 − Commit changes in the repository
Step 3 − Add path of such files or directories in the .gitignore file
Step 4 − Commit changes to the repository
Let us understand this with an example −
Create a folder by the name “bin” in the working directory. Add a file “temp.bin” with some content inside the folder and commit the changes.
$ mkdir bin //create a directory $ echo hello>bin/temp.bin //create a file temp.bin in the directory $ git add bin/temp.bin //record the change in the staging area $ git commit −m ‘add temp.bin’ //commit the change
The output of the commit command states that the file has been permanently added to the repository.
[master dd1a3a8] add tremp.bin 1 file changed, 1 insertion(+) create mode 100644 bin/temp.bin
Add one more line to the file “temp.bin”. Use the git status command to check if this change is tracked.
$ echo hello again > bin/temp.bin $ git status
The output is as shown below and shows that the change is tracked.
Changes not staged for commit: (use “git add <file>...” to update what will be committed) (use “git restore <file>..” to discard changes in working directory) modified : bin/temp.bin No changes added to commit
Now let us say at a later point in time we decide that changes to the “bin” folder should be ignored. Adding the “bin” folder to the .gitignore file will not have any effect ,since the file is already committed. In order to ignore changes to the committed file perform the following −
Step 1 − Remove the “bin” folder from the staging area − It is important to remember that a file that is committed is not automatically removed from the staging area. So, let us first delete the file from the staging area. This can be achieved using the git rm - - cached -r command. The syntax for this command is −
git rm −−cached −r <file−name> OR git rm −−cached −r <folder−name>
Let us delete the “bin” folder now.
$ git rm −−cached −r bin/
The output of the above command is shown in the screenshot −
rm ‘bin/temp.bin’
Use the git ls−files to verify if the folder is removed from the staging area.
$ git ls−files
The output suggests that the folder is removed from the staging area.
.gitignore
Step 2 − Commit changes in the repository
$ git commit m ‘deleted bin folder from tracking’
Verify the output to confirm that the change is committed.
dell@DESKTOP−N961NR5 MINGW64 /e/tut_repo (master) $ git commit −m bin 'deleted bin folder from tracking changes' [master 593aacd] deleted bin folder from tracking changes 1 file changed, 1 deletion (−) delete mode 100644 bin/temp. bin
Step 3 − Add the “bin” folder in the .gitignore file. We assume that the .gitignore file is already present.
$ echo bin/ >>.gitignore
Now add the changes to the staging area.
$ git add .gitignore
Step 4 − Commit changes to the repository
$ git commit −m ‘modified .gitignore to add bin/’
Step 5 − To verify if Git is tracking the “bin” folder, let us make changes to the “temp.bin” file and then execute the git status command.
$ hello again from TutorialsPoint >>bin/temp.bin $ git status
The output proves that Git has completely ignored changes performed in the “bin” folder.
On branch master Nothing to commit, working tree clean
- Related Articles
- How to remove a committed file from the Git repository?
- How to add an image to your README.md file in Git repository?
- What is a Git Repository?
- What is bare Git repository?
- How to create a zip file and ignore directory structure in Linux?
- What are the steps to rename a file in Git?
- How to create a GitHub repository
- How to clone a GitHub repository?
- How to add collaborators to a repository in GitHub?
- How to tag a commit in git?
- How to print previously typed snippets in JShell in Java 9?
- How to ignore files in Docker?
- How to abort a merge conflict in Git?
- How to compare two branches in Git?
- How to Install Git in Vs Code?
