标签云

微信群

扫码加入我们

WeChat QR Code

How do you delete untracked local files from your current working tree?


This interactive git cheat sheet ndpsoftware.com/git-cheatsheet.html shows the git workspace (google gives you better results with "workspace" than "working copy").

2018年09月27日37分49秒

Note: if you only want to remove some untracked files, but not all of them, git clean has now an interactive mode! See my answer to this other question: git 1.8.4+

2018年09月26日37分49秒

Note that you're not removing files from git branch, as branch is a reference to a commit and therefore doesn't contain untracked files. Those are only present in the working directory and have nothing to do with branches. That's probably just terminology clarification.

2018年09月26日37分49秒

Before you post a new answer, consider there are already 25+ answers for this question. Make sure that your answer contributes what is not among existing answers

2018年09月27日37分49秒

If the files are not yet being tracked, couldn't you just remove them without git? rm files-to-be-deleted

2018年09月27日37分49秒

git clean -f works only in the directory where it's called (and subdirectories). If you want to clean the whole working copy, you should call it in its root directory.

2018年09月26日37分49秒

It is also removing all files inside .gitignore. I need to delete only files/folders which are new and not in .gitignore

2018年09月26日37分49秒

Kostanos If you don't want to remove files that are in .gitignore, then do not provide the -x flag.

2018年09月27日37分49秒

git clean -f :/ works as if you had run it in the root repo dir. See also later answers also accounting for submodules with git clean -ffxd :/

2018年09月26日37分49秒

Michelle git clean -xfd WILL also REMOVE ALL FILES CURRENTLY IGNORED IN YOUR .gitignore and they are not recoverable

2018年09月26日37分49秒

As previously stated, good to dry-run it with git clean -n -d

2018年09月27日37分49秒

Same thing is to do git clean -nd and git clean -fd.

2018年09月26日37分49秒

got: error: unknown switch `i'

2018年09月26日37分49秒

pal4life It was added in 1.8.4, you might be running an older version of git? github.com/git/git/blob/master/Documentation/RelNotes/1.8.4.txt

1970年01月01日00分04秒

SystematicFrank, Don't use alias, they don't really scale beyond 1 person....

2018年09月27日37分49秒

BTW, this is written in documentation : Git will refuse to delete directories with .git sub directory or file unless a second -f is given. But thanks anyway!

2018年09月26日37分49秒

You can replace git add --all by git add .. So you can do it in a shorter way in oneline git add . && git reset --hard HEAD ( be very carefull with this command).

2018年09月26日37分49秒

Why use this over git clean?

2018年09月26日37分49秒

Because git clean apparently also deletes everything that is ignored. It just deleted my node_modules folder. Doing this would first stage all files except for the ignored ones, and then delete them by doing a reset. Ignored files will not be touched.

2018年09月26日37分49秒

Andreas it doesn't delete ignored files for me (git 2.14.1). You should run git clean -n anyway before doing the real deletion (or use git clean -i).

2018年09月27日37分49秒

git clean deletes ignored files only if you use either the -x or -X option, otherwise it just deletes untracked files.

2018年09月26日37分49秒

Can you explain the -u on the stash? I do not follow how that works differently from git stash save. I tried this and it worked. Looked on git docs and could not find it there either.

2018年09月26日37分49秒

-u is equivalent to --include-untracked. You can find a help with git help stash.

2018年09月26日37分49秒

hiroshi Thanks! after trying every darn solution from a dozen different people this is the one that finally worked...whew ! Even a git stash did nada. The save - u took care of untracked. reset hard /clean force/etc none of these did anything for me.

2018年09月27日37分49秒

Could you explain the remark about large projects?

2018年09月26日37分49秒

Martin One of the projects I'm working on is +8 years old with +80 developers actively coding. Git sometimes fails to clean it on the first pass.

2018年09月27日37分49秒

same comment as previous : linux.die.net/man/1/git-clean

2018年09月26日37分49秒

The command git clean -fdXx produces the error message "fatal: -x and -X cannot be used together" (using git-2.8). For your last sentence within your answer, please provide a link to git manual. Cheers

2018年09月26日37分49秒

Nice art dude!!!

2018年09月26日37分49秒

BTW you can just do git clean {flags} :/ so it will be as if you ran the command in the repo root

2018年09月26日37分49秒

Thanks. I left out the -x and just used git clean -ffd to avoid erasing files in the .gitignore.

2018年09月27日37分49秒

-ff ftw. Had no idea - thanks!

2018年09月26日37分49秒

At least this is a different approach. :) Another way, that would remember the deleted files in the reflog but not in any branches, would be: git add . git commit -m 'about to delete' git reset --hard HEAD~

2018年09月26日37分49秒

even more quick way is git add . && git reset --hard HEAD

2018年09月27日37分49秒

git add . && git reset --hard

2018年09月26日37分49秒

This might not be what you want if there are also changes you do wish to commit.

2018年09月27日37分49秒

can anyone explain how/why this works?

2018年09月26日37分49秒

This will also remove valid changes to tracked files. I wouldn't recommend it.

2018年09月27日37分49秒

Yeah, you'd want to commit changes to tracked files first.

2018年09月26日37分49秒

How is this specific to zsh? This should work in bash as well...

2018年09月26日37分49秒

Thanks for you feedback, my statement was confusing. I wanted to say that the highlighted aliases come automatically with oh-my-zsh. They obviously work great in bash. I edited my answer to reflect that.

2018年09月27日37分49秒