Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Ignoring Files after Initial Commit | More Advanced Interaction
Git Essentials

bookIgnoring Files after Initial Commit

Swipe to show menu

If you've already committed files that you now want to ignore, follow these steps:

  1. Add the files to your .gitignore;
  2. Remove the files from the repository (without deleting them locally) using git rm --cached;
  3. Commit the changes.

Workflow Example

Configuration files often contain private information, so they should not be tracked. To prevent accidentally adding such files to Git, itโ€™s best to ignore them. Apply this to the config.txt file, which has already been committed.

Refer to the following illustration of the workflow:

Workflow example

Add config.txt to the list of ignored files in the .gitignore file:

Adding config file to .gitignore

Then run the git rm command with the --cached flag to remove the file from the repository without deleting it locally:

Removing config file

Now check the status of the working tree and staging area:

Checking status

As you can see, the deletion of the config file is already staged, but the .gitignore file still needs to be added. After that, commit both changes:

Committing changes

The commit is successful.
Now verify that the config.txt file is ignored by appending a new line with an example password to it using the echo command:

Modifying config file

Finally, check the status of the working tree:

Checking working tree

The file was modified, but it is not tracked by Git, so the working tree remains clean.

question-icon

Match the actions with the respective commands.

Remove files:
Rename Files:

Remove the files from the repository (without deleting them locally):

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 2. Chapterย 6

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Sectionย 2. Chapterย 6
some-alt