1

I am using git in my project at Linux platform. I have plenty of files in a particular directory. I modified some 50 above files in that directory and didn't stage and commit it. I wish to delete all other unmodified files from that directory? Is there a way to do this, using git and Linux Commands?

Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75

5 Answers5

4

Not sure why you would want to do this.... but you can:

# Save changes to stash
git stash save

# Remove everything left
rm -rf ./*

# Checkout (restore) all of the changed files
git stash show --stat | grep -v changed | sed -e 's/|.*$//;' | xargs git checkout

# Restore the changes to those files
git stash pop
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
  • I was looking for the same thing and it was because I have a large refactor that is in progress and I want to send a teammate a zip of just the changes (monorepo, dozens of applications, databases, and gitops for infrastructure like kubernetes charts and cloud templates). And yes, I understand branches are cheap. – yzorg Apr 17 '23 at 17:10
  • 1
    Be careful if running something like this on Windows terminal or in shells like PowerShell b/c Windows doesn't skip .foo directories by default the same way linux builtins and shells do, make sure NOT to accidentally delete your .git folder! – yzorg Apr 17 '23 at 17:17
0

To do the opposite: How can I discard modified files?

git reset --hard [HEAD] 
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

You can also use more simple commands for this purpose:

git clean -Xfd // capital X
git clean -xfd // lower x

It will clean your working directory from the desired files.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
0

Using git clean is what you want. To remove (-x) those files and directories (-d), run:

$ git clean -fdx

If you use the -X option instead of -x, then the files you have told git to ignore will still be kept (e.g., build artifacts). Recent versions of git require either "-f" (force) or "-n" (dry-run) to be specified.

You should run a dry-run first, to show what will happen, but not actually do anything:

$ git clean -ndx

I use this so often, that I have an alias for this (added to your .gitconfig) to check for files that would be deleted when you run git clean. It's also useful to remind me if I've forgotten to "git add" a file that I want to keep.

[alias]
     # list files that would be removed via 'clean' (non-destructive)
     ifc = clean -ndx

Then, running git ifc (i.e,. "ifc" = "if clean") shows everything that isn't tracked and could be removed, or isn't tracked and should be added.

https://git-scm.com/docs/git-clean

michael
  • 9,161
  • 2
  • 52
  • 49
  • Does this answer remove unmodified files that have been previously committed, but unchanged in current status? I don't see how this answer, albeit very through and well-written, I don't see how it addresses the actual question asked. – yzorg Apr 17 '23 at 17:08
  • @yzorg though it's been 7 years, it does seem that I misread/misunderstood the original question. I probably thought they wanted to simply remove untracked files (a very common task), hence `git clean`. Rereading the question now, it's likely that was not the intent, but rather they're asking to do something that imho requires more details to clarify their (peculiar) intent, i.e., what did they mean by "unmodified" & "delete" (from git? from filesystem?). (Seems I'm not the only one to fall into this trap, so caveat lector to anyone reading these answers to address their particular issue.) – michael Apr 18 '23 at 05:57
0
git ls-files -m | git ls-files -zX /dev/stdin | xargs -0 rm
jthill
  • 55,082
  • 5
  • 77
  • 137