7

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
2
  • 1
    I think you can't do this with git commands. You'll need some scripting. Here is an example: stackoverflow.com/questions/22158173/… Commented Aug 30, 2019 at 9: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 Commented Aug 31, 2019 at 5:04

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Looks good, but it still sorts in alphabetical order i.e. deleted files come first
Thought u just wanted them grouped. ..no matter how u sort deleted is always going to co.e before modified or new files...
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
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)))

3 Comments

It works, but colors are gone and only files are listed no other information is visible.
It would be great if git has sorting feature in-built imo
To restore colors try git -c color.ui=always status

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.