标签云

微信群

扫码加入我们

WeChat QR Code

如何消除地方(跟踪)从目前的Git工作树的文件吗?

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年05月23日34分55秒

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年05月23日34分55秒

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年05月23日34分55秒

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年05月23日34分55秒

To clarify for the understanding of the uninitiated and those new to Git - run git status and if it shows a file as untracked, and you don't want that file in the repo, you can just go to your filesystem and delete or move it. This will not do anything bad to your local repo or to Git. You can also use git clean or some variation in the answers below, including the interactive version to delete just selective files, but interactive mode can be tedious. Whatever you do, make sure you understand what git clean will delete or use --dry-run to have it tell you without deleting anything.

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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

2018年05月23日34分55秒

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

2018年05月23日34分55秒

got: error: unknown switch `i'

2018年05月23日34分55秒

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年05月23日34分55秒

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年05月23日34分55秒

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年05月23日34分55秒

Why use this over git clean?

2018年05月23日34分55秒

can anyone explain how/why this works?

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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年05月23日34分55秒

Could you explain the remark about large projects?

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

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年05月23日34分55秒

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

2018年05月23日34分55秒

git add . && git reset --hard

2018年05月23日34分55秒

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

2018年05月23日34分55秒

can anyone explain how/why this works?

2018年05月23日34分55秒

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

2018年05月23日34分55秒

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

2018年05月23日34分55秒

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

2018年05月23日34分55秒

-ff ftw. Had no idea - thanks!

2018年05月23日34分55秒

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

2018年05月23日34分55秒