标签云

微信群

扫码加入我们

WeChat QR Code

我怎么清楚我在git的本地工作目录?[重复]

This question already has an answer here:

How can I clear my working directory in git?


User interactive approach: git clean -i -fd

2018年05月27日28分41秒

I don't understand why this question has been flagged duplicate. That other question clearly deals with removing only untracked files from working directory and not modified files

2018年05月27日28分41秒

Note that by default, 'git clean -d' is insufficient. You need to also add the -f (force) flag. Also, if you want to additionally delete the files that are ignored by .gitignore, then you need to add the -x option. Here's what it all looks like: git clean -xdf

2018年05月27日28分41秒

I think you mean "working directory" instead of "repository" in the sentence "I usually just delete all files in the repository".

2018年05月27日28分41秒

Dr. Person Good point, that is clearer, edited

2018年05月27日28分41秒

However be sure to NOTE that command will also blow away your local sqlite database -- not undo recent changes, but actually delete it. Sp the "-x" option might NOT ne a good idea depending on what you are trying to do.

2018年05月27日28分41秒

Please note: the suggestion to delete all the files except the .git/ folder and then restore by running git reset --hard will probably take a long time if your repo has been around for any time at all. Please don't do this. Use git clean.

2018年05月27日28分41秒

I suggest to not use the -x parameter as it will remove all gitignored content. Say you have a folder named 'gitignored' added to .gitignore, and you store there your files that have to be secure, you're also deleting them using -x parameter

2018年05月28日28分41秒

Oops. git clean -xdf also removed .gitignore, and I did not notice before the next commit. I won't ignore .gitignore anymore. :)

2018年05月27日28分41秒

Grastveit git clean -xdf will remove .gitignore if and only if it has not been added to, thus working as intended.

2018年05月27日28分41秒

let's highlight this: 𝐈 𝐬𝐮𝐠𝐠𝐞𝐬𝐭 𝐭𝐨 𝐧𝐨𝐭 𝐮𝐬𝐞 𝐭𝐡𝐞 -𝐱 𝐩𝐚𝐫𝐚𝐦𝐞𝐭𝐞𝐫 𝐚𝐬 𝐢𝐭 𝐰𝐢𝐥𝐥 𝐫𝐞𝐦𝐨𝐯𝐞 𝐚𝐥𝐥 𝐠𝐢𝐭𝐢𝐠𝐧𝐨𝐫𝐞𝐝 𝐜𝐨𝐧𝐭𝐞𝐧𝐭

2018年05月27日28分41秒

Marko sure, everyone is free to use -x or not. But some people may read this github answer, copy-paste the command, and without realizing it loose all their gitignored files they wanted to keep. Then come back here and only notice Highmastdon's comment, about the consequence of using -x.

2018年05月27日28分41秒

Is branch name really required? For me git reset --hard simply works for current working branch without branch name.

2018年05月27日28分41秒

Without the branch, it does not clear local commits.

2018年05月27日28分41秒

ohh. ok. My observation was only regarding the local modified and newly added untracked files. So if I got you correctly, if any file is staged or has already been committed in local branch (not pushed as yet) will not get cleaned if I do not use the branch name explicitly. Correct?

2018年05月27日28分41秒

Super. I verified your observation. You were correct buddy. git reset --hard doesn't clear local commits until you specify the branch name explicitly. +1.

2018年05月27日28分41秒

This can also be a nice way to save some disk-space in a pinch.

2018年05月27日28分41秒

A much faster way to create the empty checkout: true | git mktree | xargs git commit-tree | xargs git tag empty

2018年05月28日28分41秒

jthill: That seems to work -- thanks for the tip!

2018年05月27日28分41秒

Note: There seems to be some value in using an empty commit as the base for projects. This makes it easier to perform full-project rebase operations in case your initial commit needs to be fixed -- and may simplify things when pushing an existing git repository to svn.

2018年05月27日28分41秒

'git init && git commit --allow-empty "EMPTY COMMIT && git tag empty' would do the job of the first block too.

1970年01月01日00分09秒

git checkout -p?

2018年05月27日28分41秒