9

I want to unstage all file deletes. Is there an easy way?

I want to apply this to the file pattern of all deletes.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jacko
  • 12,665
  • 18
  • 75
  • 126

4 Answers4

30

The output of git status --porcelain is a great way to build one-liners and scripts for tasks like this:

git status --porcelain | awk '$1 == "D" {print $2}' | xargs git reset HEAD
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    @Jacko: Definitely handy things. You could've easily done this with sed too: `sed -n '/^D /s/^D //p`. – Cascabel Nov 22 '10 at 20:20
  • My previous comment is missing the closing single quote. – Cascabel Nov 22 '10 at 20:26
  • 1
    Old thread but worth noting that the answer breaks down on files surrounded by quotes in git status. – Mike S Jul 17 '14 at 01:57
  • 2
    Warning: this does not work (and fails silently) if you are not in the main folder of the repository (i.e. I tried it in a subfolder and it did not work - as git status --porcelain gives paths relative to the main folder) – BlueCoder Sep 04 '19 at 10:33
3

In case your path-/filenames returned from git status contain space characters, the call to awk can be modified to include the entire (quoted) path/filename including spaces:

git status --porcelain|awk '$1 == "D" {print substr($0, index($0,$2))}'|xargs git reset HEAD
rc0r
  • 18,631
  • 1
  • 16
  • 20
1

Just in case anyone else uses git with PowerShell, here is a powershell version of @jefromi's excellent answer:

git status --porcelain | where { $_.StartsWith(" D") } | foreach-object { git reset HEAD $_.replace(" D ", "") }
Nick Meldrum
  • 1,141
  • 1
  • 10
  • 27
  • Just a heads up that on my Win10 PowerShell with git 2.35, deleted files listed by "git status --porcelain" start with "D " not " D" so your StartsWith filters everything. Using StartsWith("D ") and replace("D ", "") fixes it. – BrandonL Aug 24 '22 at 21:03
-1

See the section 'Unstaging a staged file' in this book.

Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127
rmk
  • 4,395
  • 3
  • 27
  • 32