6

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
Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
sai_ekkaldevi
  • 103
  • 1
  • 6
  • 1
    I think you can't do this with git commands. You'll need some scripting. Here is an example: https://stackoverflow.com/questions/22158173/how-to-sort-output-of-shorthand-git-status – mfnx Aug 30 '19 at 09:42
  • @phd I tried and it gives me syntax error as I have python 3 installed, it would be great if anyone could share it with python 3 syntax – sai_ekkaldevi Aug 31 '19 at 05:04

2 Answers2

1

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.

leoOrion
  • 1,833
  • 2
  • 26
  • 52
  • Looks good, but it still sorts in alphabetical order i.e. **d**eleted files come first – sai_ekkaldevi Aug 31 '19 at 04:45
  • Thought u just wanted them grouped. ..no matter how u sort deleted is always going to co.e before modified or new files... – leoOrion Aug 31 '19 at 05:23
  • You will have to add a script that parses each line and then print them.in the order u want... I'm out now.. typing from mobile.. will send once I get to a laptop – leoOrion Aug 31 '19 at 05:25
1

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)))
phd
  • 82,685
  • 13
  • 120
  • 165