sai_ekkaldevi
sai_ekkaldevi

Reputation: 113

How to sort git status output

Currently, git status lists the files in alphabetically order, How can I order/sort the files in a way where modified files are shown first, then deleted, then new files respectively. It gets easier to review all the files before committing

Current output

deleted: app/Books.php
new file: app/Permissions.php
new file:  app/Roles.php
modified: app/User.php
modified: composer.json
modified: composer.lock
new file: database/seeds/RoleSeeder.php
modified: routes/web.php

Expected

modified: app/User.php
modified: composer.json
modified: composer.lock
modified: routes/web.php
deleted: app/Books.php                   // then deleted
new file: app/Permissions.php            // then new files
new file:  app/Roles.php
new file: database/seeds/RoleSeeder.php

Upvotes: 7

Views: 1425

Answers (2)

phd
phd

Reputation: 94492

Rewritten from https://stackoverflow.com/a/22193283/7976758 for Python 3:

#! /usr/bin/env python3

import sys, re

# custom sorting order defined here:
order = { 'A ' : 1, ' M' : 3, '??' : 2, '##' : 0 }

ansi_re = re.compile(r'\x1b[^m]*m')

print(''.join(sorted(
    sys.stdin.readlines(),
    key=lambda line: order.get(ansi_re.sub('', line)[0:2],0), reverse=1)))

Upvotes: 1

leoOrion
leoOrion

Reputation: 1957

You can do this -

git status | grep 'modified\|deleted\|new' | sort

Basically pipe it to grep to find it if it exists in the status and then pipe to sort.

Upvotes: 1

Related Questions