# How to ignore a previously committed file in Git repository?

GitOpen SourceLinuxSoftware & Coding

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
Published on 20-Feb-2021 08:38:09