Git Shell: How can we remove specific file from untracked files

26,761

Solution 1

If the file isn't tracked by git, it's not git's job to remove it. Just use normal shell commands, like rm instead of git rm.

If you want to delete all untracked files, you could do a git clean.

Solution 2

Might just need a git clean -df -n is handy to double check what you're doing first.

NAME
       git-clean - Remove untracked files from the working tree

OPTIONS
       -d
           Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if
           you really want to remove such a directory.

       -f, --force
           If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete
           directories with .git sub directory or file unless a second -f is given.

       -n, --dry-run
           Don't actually remove anything, just show what would be done.

However, if you've got uncommited changes you'd need to use git checkout -- .

Solution 3

If you have any file in your working tree & you want to remove it you will use git rm

Straight from the doc

git-rm - Remove files from the working tree and from the index

Now why will you use git rm instead of rm for removing file from your working tree

The answer is - If you just use rm, you will need to follow it up with git add <fileRemoved>. git rm does this in one step.

You can also use git rm --cached which will remove the file from the index (staging it for deletion on the next commit), but keep your copy in the local file system.

This part is taken from https://stackoverflow.com/a/7434558

To remove untracked file you may use rm for tracked file which is included in your source tree you will use git rm

Share:
26,761
Raju
Author by

Raju

Would like to learn new things in development always.

Updated on April 11, 2020

Comments

  • Raju
    Raju about 4 years

    I wish to delete a specific file from untracked files in git using shell command. but as i have searched there is a solution only for like

    -f - force, 
    -d - directories too, 
    -x - remove ignored files too.
    

    let consider file as like gitignore.gitignore, Am failed whenever i tried the command git rm gitignore.gitignore for this concern. would appreciate any expected result.

  • VVB
    VVB about 5 years
    Exactly. Hitting git rm is just waste of time if it's not in git territory. As git help shows usage of git rm : Remove files from the working tree and from the index