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

Ignoring Files after Initial CommitIgnoring Files after Initial Commit

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

In most cases, configuration files often contain private information, so they shouldn’t be tracked at all. In order to avoid accidentally adding such files to Git, it’s better to ignore them. We’ll do the same with our config.txt file which has already been committed.

Take a look at the following illustration of our workflow:

Workflow example

Let’s first add config.txt to the list of ignored files in .gitignore:

We’ll then run the git rm command with the --cached flag to remove it from our repository without deleting:

Now, let’s check the status of our working tree and staging area:

As you can see, the deletion of our config file is already staged, however, we still have to add the .gitignore file, and then we can commit these changes:

The commit is successful. Now, let’s verify that the config.txt file is indeed ignored. We’ll append a new line with a certain example password to it using the echo command:

Finally, let’s check the status of our working tree:

Our file was modified, but it’s not tracked by Git, so the working tree is clean.

Everything was clear?

Section 2. Chapter 6
course content

Course Content

Git Essentials

Ignoring Files after Initial CommitIgnoring Files after Initial Commit

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

In most cases, configuration files often contain private information, so they shouldn’t be tracked at all. In order to avoid accidentally adding such files to Git, it’s better to ignore them. We’ll do the same with our config.txt file which has already been committed.

Take a look at the following illustration of our workflow:

Workflow example

Let’s first add config.txt to the list of ignored files in .gitignore:

We’ll then run the git rm command with the --cached flag to remove it from our repository without deleting:

Now, let’s check the status of our working tree and staging area:

As you can see, the deletion of our config file is already staged, however, we still have to add the .gitignore file, and then we can commit these changes:

The commit is successful. Now, let’s verify that the config.txt file is indeed ignored. We’ll append a new line with a certain example password to it using the echo command:

Finally, let’s check the status of our working tree:

Our file was modified, but it’s not tracked by Git, so the working tree is clean.

Everything was clear?

Section 2. Chapter 6
some-alt