Unstaging a list of files in git

When merging I find it helpful sometimes to un-stage all changes with conflicts. This allows you to commit changes without conflicts, and then resolve the conflicts as a separate step.

If you do a cherry pick:

git cherry-pick ...insert sha here...

You will see changes that have conflicts, but the parts of the files that don’t conflict are automatically staged. To undo this, you can get a list of the files with conflicts like so (note this doesn’t cover files deleted in one repository, but that is typically easier to resolve manually):

git diff --name-only --diff-filter=U 

You can then make a shell script that lets you unstage all of these:

git diff --name-only --diff-filter=U | sed "s/(.*)/git reset $1/" | unstage.sh
./unstage.sh

Leave a Reply

Your email address will not be published. Required fields are marked *