202

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.

0

8 Answers 8

389

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.

10
  • 107
    Add -n to preview first so you don't accidentally remove stuff
    – Druska
    Commented Jun 4, 2014 at 16:04
  • 14
    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
    Commented Oct 15, 2015 at 11:50
  • 8
    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 Commented May 18, 2016 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
    Commented Dec 21, 2016 at 0:09
  • 3
    I think you have to inform that this command is potentially dangerous because it can remove wanted files!!!
    – Davide
    Commented Jun 8, 2017 at 13:30
47

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.

1
  • 2
    If you're using -i, then -f is no longer necessary Commented Feb 14, 2023 at 21:18
25

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

1
  • 3
    -n == --dry-run, Don't actually remove anything, just show what would be done. Commented Oct 23, 2020 at 15:54
19

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.

10

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

The command for your rescue is git clean.

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

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
1
  • 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
    Commented Feb 16, 2023 at 10:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.