SOLVED: git remove file from tracking [Practical Examples]


GIT

Author: Steve Alila
Reviewer: Deepak Prasad

git remove file from tracking - cheat sheet

Finding a quick solution to typical issues in git remove file from tracking will boost your software management experience. It enables you to comfortably wipe off push and pull request problems.

 

Scenario-1

If you want to ignore a tracked file without deleting it, run this command on the command line:

git rm --cached <file>

Assume you commit a file that you were to ignore and later reference the file in the .gitignore file. You add the changes to the staging area and commit them. However, the files keep appearing under tracked files.

The most straightforward steps to clear such errors include committing pending code changes then running this command:

git rm -r --cached <file>

followed by

git add .

and

git commit

 

Scenario-2

Other times you work on a file with other software engineers. You want to git remove the file from tracking without deleting it without interfering with your teammates who track the file.

The best command to use in the circumstance is

git update-index --skip-worktree <file>

Or

git update-index --assume-unchanged

If you are sure you will not change the file frequently.

 

Scenario-3

Lastly, you may want to untrack a folder with many files. The recommended command to use is

git rm --cached folder\*

Let us navigate to the following sections of this tutorial to understand the four standard issue-tracking environments and associated challenges.

 

Setup Lab Environment

I am creating a remote repo called remove_from_tracking on Github.

SOLVED: git remove file from tracking [Practical Examples]

Grab the clone URL.

SOLVED: git remove file from tracking [Practical Examples]

Head over to the terminal or command line and clone the repo into a local directory called golinux.

SOLVED: git remove file from tracking [Practical Examples]

cd into the cloned repo, remove_from_tracking.

SOLVED: git remove file from tracking [Practical Examples]

Now that the lab setup is complete, you are ready to learn file untracking by following simple steps.

 

Example-1: git remove file from tracking without deleting it

Let us continue building the repo in readiness for file tracking.

Check tracked files using the command

git ls-files

Currently, only one file, README.md, gets tracked.

SOLVED: git remove file from tracking [Practical Examples]

Let us create two more files, file1.txt and file2.txt.

touch file1.txt file2.txt

Running

git status

shows the two files are untracked.

SOLVED: git remove file from tracking [Practical Examples]

Let us stage the untracked files

git add .

and commit them by entering the following commands on the command line or terminal.

git commit -m "track file1 and file2"

Running

git ls-files

shows we are tracking the three files, README.md, file1.txt, and file2.txt.

SOLVED: git remove file from tracking [Practical Examples]

Assume we decide to untrack file2.txt. We can git remove the file from tracking by running the command:

git rm --cached file2.txt

Rerun

git ls-files

to check tracked files.

git remove file from tracking

Running

git status

shows file2.txt is now untracked.

SOLVED: git remove file from tracking [Practical Examples]

Using the --cached flag removes the target files from the staging area, without affecting the files in the working directory.

To confirm that git rm --cached <file> command does not alter the working area, open the file using a code editor or list the files and directories using the command

ls

in the remove_from_tracking folder.

 

Bonus trick

You are probably wondering, "How can I do git remove a file from tracking and delete it from the working area as well?"

Here is the command to untrack and delete a file from the working directory:

git rm --cached <file>

Let us apply it.

Run

git add .

to stage file2.txt file again, then commit it by running

git commit -m "tracking file2 again"

Re-enter

git ls-files

to confirm that we are tracking file2.txt again.

Remove the file from tracking using this short command

git rm file2.txt

on your command line.

What did you notice?

Using the command

git rm <file>

removes files from tracking and deletes them from the working area.

 

Example-2: git remove file from tracking and solve ignore issues

Sometimes you stage, commit and push a file to a remote repo then realize it was necessary to ignore a file before pushing the changes. You decide to create a .gitignore file and reference your target file in it, but you still face particular challenges, as you are about to see below.

So far, we are tracking two files, README.md and file1.txt.

Create a test.py file and append a new line to it.

echo password = JakeMay1992 >> test.py

git add ., git commit -m " password tracked accidentally" and git push it to the remote repo you created at the beginning of this tutorial.

Check the test.py file's content on the remote repo. Anybody can see our password.

SOLVED: git remove file from tracking [Practical Examples]

Let us do a git remove file from tracking by creating a .gitignore file and referencing the test.py file in it.

## create .gitignore file
touch .gitignore 

## reference the target file in it
echo test.py >> .gitignore

Track the changes.

git add .
git commit -m "now ignoring python file"

Check tracked files.

git ls-files

We expected the test.py to be removed from of the tracking zone because we now ignore it, but it did not disappear from tracked files.

SOLVED: git remove file from tracking [Practical Examples]

Let us remove it from the tracking space

git rm -r --cached test.py

and check the changes.

## check tracked files
git ls-files

## show ignored files
cat .gitignore

test.py is out of the tracked files, but still referenced in .gitignore.

SOLVED: git remove file from tracking [Practical Examples]

Let us also update the remote repo.

## stage files 
git add .

## commit the files to repo
git commit -m "secure the password"

## push changes to remote repo
git push

 

The remote repo lacks test.py file.

SOLVED: git remove file from tracking [Practical Examples]

Knowing how to ignore tracked files helps secure files promptly. What if you want to git remove file from tracking without stopping your teammates from tracking its changes? Let us take a look at that scenario.

 

Example-3: Remove file from tracking from all git pull requests

We can untrack a file on every pull request while letting other engineers continue tracking it by doing the following.

Assume we want to untrack the README.md. We can run this command:

git update-index --skip-worktree README.md

Or

git update-index --assume-unchanged

if we don't alter the file or it takes up massive storage space.

SOLVED: git remove file from tracking [Practical Examples]

Now we won't track the file on subsequent pull requests.

 

Example-4: Remove entire directory from tracking in git

Assume you have a bunch of files in a folder to untrack. We don't to waste time doing a cd into the folder to untrack each of the directory's files. Here is a step-step way to untrack the files from the directory.

Return to the terminal or command line and create a folder, html_files.

mkdir html_files

cd into the html_files folder and add two files, index.html, and about.html.

touch index.html about.html

Navigate into the parent folder, remove_from_tracking.

cd ..

Stage changes.

git add .

and

git commit -m "track html files"

the changes.

Running

git ls-files

shows we are now tracking index.html and about.html.

doing git remove file from tracking on HTML files

We later realize we won't work with HTML. We, therefore, want to untrack index.html and about.html then deleted them.

We can untrack the folder by running this one-line command:

git rm --cached html_files\*

Recheck the tracked files.

git ls-files

SOLVED: git remove file from tracking [Practical Examples]

Remember to put a slash \ before the star * to escape from the git commit command. Our files are now untracked and rest on the hard drive.

After untracking the files we can delete them by running the command

git clean -fd

 

Conclusion

Understanding and applying git remove file from tracking will boost your personal experience and ease team interactions when handling files. Now that you know why, when, and how to remove files from tracking, go ahead and enjoy your file tracking workflows.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment