DEV Community

ADEMOLA OGUNMOKUN
ADEMOLA OGUNMOKUN

Posted on • Edited on

How to Gitignore (untrack) files already pushed to GitHub repository.

Most developers usually initialize the folder they are working from as their GitHub repository. However, some files or/and directories may be present in that folder that you would not want to push to GitHub (files such as database, environment variables etc.)

Adding the file(s) or directories to gitignore seems to solve the issue. However, sometimes, you might have unknowingly pushed that file(s) to our GitHub repository failing or forgetting to gitignore the file(s) we did not want to push to the remote repository.

Once the file has been pushed, adding it to gitignore afterwards would not untrack the file from that commit. The file would still be present in the repository.

If you have that kind of issue, follow these simple five steps to solve the problem.

Step 1:

Git commit

Make sure the changes you have made on the projects have been added and committed, including the .gitignore file.

Do this by

git add .
git commit -m "initial commit" 
Enter fullscreen mode Exit fullscreen mode

Step 2:

Remove all the files in the repository.

You do that by:

git rm -r --cached .
Enter fullscreen mode Exit fullscreen mode

rm is the remove command, adding -cached allow us to remove the files from the index. Our files are still present.

Step 3:

Add everything

The next step is to re-add/track our files. By doing this, the files in the . gitignore file will not be tracked.

This is done by:

git add .
Enter fullscreen mode Exit fullscreen mode

Step 4:

Commit the changes.

Thus, let's commit our changes by

git commit -m "gitignore fix"
Enter fullscreen mode Exit fullscreen mode

Step 5:

Push to GitHub

After we are through with everything, we still need to push it to our online GitHub repository by

git push
Enter fullscreen mode Exit fullscreen mode

Thanks for reading.

Top comments (2)

Collapse
 
_adrian_e_ profile image
Adrian E.

There are also ways to clean the history from files i.e. with credentials

docs.github.com/en/free-pro-team@l...

Collapse
 
ademola_isr profile image
ADEMOLA OGUNMOKUN

Cool. I will definitely check it out.