简体   繁体   中英

Git command to unstage all new files (unstage by status)

Often after a merge operation between branches I find in the stage area several new files that I don't want to have by default in the stage, but rather I would want to have by default unstaged to check them and stage them one by one after a review .

So the question is : is it possible to unstage all new files automatically ?

or more generally : is it possible to unstage a group of files by their status ?

For example I have this contents in the staging area :

A   fileA.txt
A   fileB.txt
U   fileC.txt

I want to remove the newly added files ( fileA.txt and fileB.txt ) in a single command, without performing git reset on each of them by hand :

git reset HEAD fileA.txt
git reset HEAD fileB.txt

What is the best way to solve the problem ?

Since I searched on the web with no luck, I've created a git alias using the following concatenated set of commands from the git bash shell :

git diff --name-status --cached | grep ^A | sed 's/^A[[:space:]]//g' | xargs -I {} git reset HEAD {}

The alias(unstagenew) definition is as follows :

[alias]
    unstagenew = "!sh -c 'git diff --name-status --cached | grep ^A | sed 's/^A[[:space:]]//g' | xargs -I {} git reset HEAD {}'"

To adapt to other status just replace the regex : ^A with : ^M , ^D , ^U ...

While to avoid the cumbersome output put > /dev/null at the end of the command .

I hope in the future it will be implemented as an option to the reset command, or as a new built in git command .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM