Bootstrap FreeKB - Git (Version Control) - Remove tracked file using the git rm command
Git (Version Control) - Remove tracked file using the git rm command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Let's say you create a new file in the directory of the cloned repository.

touch foo.txt

 

The git status command will now show that foo.txt is an untracked file

~]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo.txt

 

At this point, if you try to remove the file using the git rm command, something like this should be returned. This occurs because the file is untracked, meaning the file has not yet been added to the currently selected branch of the cloned repository. The git clean command can be used to remove untracked files.

~]$ git rm foo.txt
fatal: pathspec 'foo.txt' did not match any files

 

Let's commit the file.

git commit --message 'First Commitment' foo.txt

 

Now, foo.txt can be removed from Git.

~]# git rm foo.txt
rm 'foo.txt'

 

The git status command should now show that the file is ready to be removed.

~]# git status foo.txt
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    foo.txt
#

 

Use the git commit command to commit the change to remove the file from Git.

~]# git commit -m "removing from Git" foo.txt
 1 files changed, 0 insertions(+), 459 deletions(-)
 delete mode 100755 foo.txt

 

The git status command should now return the following.

~]# git status foo.txt
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

 

The git push command to push the change to the origin Git repository.

git push origin master

 

The git status command should now return the following.

~]# git status foo.txt
# On branch master
nothing to commit (working directory clean)

 

And the git checkout command can be used to confirm that the file is no longer known to Git, meaning the file no longer exists in the origin Git repository.

~]$ git checkout foo.txt
error: pathspec 'foo.txt' did not match any file(s) known to git.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter e00eab in the box below so that we can be sure you are a human.