190

I'm working on a branch, say "experimental", which I branch out from my master branch. Then, I generate a user model in the experimental branch, but don't add them to the index yet.

What do I have to do if I want to discard all the changes of the files recently added in my experimental branch? The untracked files are listed as below:

$ git status
 On branch new_chick
 Untracked files:
   (use "git add <file>..." to include in what will be committed)

       .project
       app/models/user.rb
       db/migrate/
       test/fixtures/users.yml
       test/unit/user_test.rb

I tried to run "git reset --hard" in the hope to undo all those changes, but all the files mentioned above still show up.

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Sarun Sermsuwan
  • 3,608
  • 5
  • 34
  • 46

8 Answers8

367

To remove untracked files / directories do:

git clean -fdx

-f - force

-d - directories too

-x - remove ignored files too ( don't use this if you don't want to remove ignored files)


Use with Caution!
These commands can permanently delete arbitrary files, that you havn't thought of at first. Please double check and read all the comments below this answer and the --help section, etc., so to know all details to fine-tune your commands and surely get the expected result.

Viktor Borítás
  • 135
  • 2
  • 11
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 103
    Add -n to preview first so you don't accidentally remove stuff – Druska Jun 04 '14 at 16:04
  • 12
    This is great! Except that it will also remove configuration files which you might have intentionally ignored in .gitignore but still need for your local development environment. You can use the same command with the extra -i for interactive mode as such: git clean -fdxi and it will prompt you for which files you want to delete. – moeabdol Oct 15 '15 at 11:50
  • 7
    This is great, but you might want to add a notice to users to USE `-x` WITH CAUTION because it will permanently delete files. Or use with `-n` as mentioned by @Druska – lacostenycoder May 18 '16 at 20:14
  • 3
    Didn't used `-x` still the .project and other config files got deleted. Had to pull the repo again from the last commit into a new directory. – timekeeper Dec 21 '16 at 00:09
  • 3
    I think you have to inform that this command is potentially dangerous because it can remove wanted files!!! – Davide Jun 08 '17 at 13:30
  • 1
    And how to undo that ? – Ulysse BN Mar 08 '22 at 15:52
  • I think `-n` must be included in the answer – Inan May 16 '22 at 09:37
  • 1
    Careful! I just deleted all my `.env` configs with this... – ruffrey Aug 03 '22 at 15:24
  • `git clean -ffdx` if you want to remove subfolders, e. g. `.terragrunt-cache` – Tarasovych Sep 29 '22 at 10:04
43

User interactive approach:

git clean -i -fd

Remove .classpath [y/N]? N
Remove .gitignore [y/N]? N
Remove .project [y/N]? N
Remove .settings/ [y/N]? N
Remove src/com/amazon/arsdumpgenerator/inspector/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove src/com/amazon/arsdumpgenerator/s3/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/manifest/ [y/N]? y
Remove tst/com/amazon/arsdumpgenerator/s3/ [y/N]? y

-i for interactive
-f for force
-d for directory
-x for ignored files(add if required)

Note: Add -n or --dry-run to just check what it will do.

bit_cracker007
  • 2,341
  • 1
  • 26
  • 26
23

For deleting untracked files:

git clean -f

For deleting untracked directories as well, use:

git clean -f -d

For preventing any cardiac arrest, use

git clean -n -f -d

etemple1
  • 1,748
  • 1
  • 11
  • 13
Sonu Mishra
  • 411
  • 3
  • 7
18

Those are untracked files. This means git isn't tracking them. It's only listing them because they're not in the git ignore file. Since they're not being tracked by git, git reset won't touch them.

If you want to blow away all untracked files, the simplest way is git clean -f (use git clean -n instead if you want to see what it would destroy without actually deleting anything). Otherwise, you can just delete the files you don't want by hand.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
9

You may also return to the previous state of the local repo in another way:

  1. Add the untracked files to the staging area with git add.
  2. return to the previous state of the local repo with git reset --hard.
Tomasz Nazarenko
  • 1,064
  • 13
  • 31
1

The command for your rescue is git clean.

Dexter
  • 49
  • 3
0

Well, I had the similar issue. I had taken latest but there were some changes in the local due to which the merge was not happening to a particular file. The file was untracked and I did not want them so What I did was -

$ git checkout filepath/filename

filepath - The location from where I did the git bash. then when I took the latest the changes were available

skb
  • 1,148
  • 2
  • 9
  • 17
0

While git clean works well, I still find it useful to use my own script to clean the git repo, it has some advantages.

This shows a list of files to be cleaned, then interactively prompts to clean or not. This is nearly always what I want since interactively prompting per file gets tedious.

It also allows manual filtering of the list which comes in handy when there are file types you don't want to clean (and have reason not to commit).


git_clean.sh


#!/bin/bash
readarray -t -d '' FILES < <(
    git ls-files -z --other --directory |
        grep --null-data --null -v '.bin$\|Cargo.lock$'
)
if [ "$FILES" = "" ]; then
    echo  "Nothing to clean!"
    exit 0
fi

echo "Dirty files:"
printf '  %s\n' "${FILES[@]}"

DO_REMOVE=0
while true; do
    echo ""
    read -p "Remove ${#FILES[@]} files? [y/n]: " choice
    case "$choice" in
        y|Y )
            DO_REMOVE=1
            break ;;
        n|N )
            echo "Exiting!"
            break ;;
        * ) echo "Invalid input, expected [Y/y/N/n]"
            continue ;;
    esac
done

if [ "$DO_REMOVE" -eq 1 ];then
    echo "Removing!"
    for f in "${FILES[@]}"; do
       rm -rfv "$f"
    done
fi
ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • git already has the functionality that your script is providing, you can use it with `git clean -i` whereas the `-i` stands for *interface*. – baltermia Feb 16 '23 at 10:47