how to remove untracked files in Git?

Git

Git Problem Overview


I'm working on a branch, say "experimental" branch which I branch out from my master branch.Then, I generate a user model in experimental branch, but does not add them to 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 hope to undo all those changes, but all the files above still show.

Anyone please shed some light on me?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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

Solution 5 - Git

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.

Solution 6 - Git

The command for your rescue is git clean.

Solution 7 - Git

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

Solution 8 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarun SermsuwanView Question on Stackoverflow
Solution 1 - GitmanojldsView Answer on Stackoverflow
Solution 2 - Gitbit_cracker007View Answer on Stackoverflow
Solution 3 - GitLily BallardView Answer on Stackoverflow
Solution 4 - GitSonu MishraView Answer on Stackoverflow
Solution 5 - GitTomasz NazarenkoView Answer on Stackoverflow
Solution 6 - GitDexterView Answer on Stackoverflow
Solution 7 - GitskbView Answer on Stackoverflow
Solution 8 - Gitideasman42View Answer on Stackoverflow