Unstage all deleted files in Git
Asked Answered
D

4

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.

Drice answered 22/11, 2010 at 20:4 Comment(0)
K
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
Kirst answered 22/11, 2010 at 20:15 Comment(4)
@Jacko: Definitely handy things. You could've easily done this with sed too: sed -n '/^D /s/^D //p.Kirst
My previous comment is missing the closing single quote.Kirst
Old thread but worth noting that the answer breaks down on files surrounded by quotes in git status.Galloping
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)Tawnytawnya
B
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
Bivalve answered 7/10, 2014 at 12:32 Comment(0)
P
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 ", "") }
Peoples answered 6/5, 2015 at 7:23 Comment(1)
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.Wean
R
-1

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

Radarman answered 22/11, 2010 at 20:8 Comment(1)
Link goes to a funny place now it seems, but the book is helpful.Kat

© 2022 - 2024 — McMap. All rights reserved.