15

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.

Raju
  • 1,183
  • 3
  • 11
  • 19

4 Answers4

30

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.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • 2
    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 – VVB Feb 27 '19 at 07:13
5

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 -- .

Adam
  • 51
  • 1
  • 1
5

You may use the interactive mode with the flag -i in the following way

git clean -id

(d is for directories as well) and then you can choose to iterate over the file and folders by pressing a, and choose what you want and do not want to delete.

bruceg
  • 2,433
  • 1
  • 22
  • 29
cerebrou
  • 5,353
  • 15
  • 48
  • 80
2

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

Community
  • 1
  • 1
ARIF MAHMUD RANA
  • 5,026
  • 3
  • 31
  • 58