2

This question was rejected at in the Software Engineering StackExchange, I was pointed here.

I use git entirely in the command-line and I look for ways to speed up my workflow.

Many times a day, I check my code with git status, followed by several git diff /path/to/specific_file. I have to type a lot in the second command, and there has to be a better way.

Are there solutions out there where you can immediately check e.g. the 2nd changed file of git status or similar?

5
  • You don't mention for what operating system are you looking programs for, nor price range or anything else. Also, what features are you searching exactly?
    – Alejandro
    Commented Nov 7, 2023 at 15:38
  • @alejandro I personally believe that my question is not a good fit for this exchange. No commercial software, just how to speed up the workflow on regular CLIs found in linux and unix-like systems. I cannot be the only one who checks their files that way before committing and this workflow is needlessly type-heavy and clumsy.
    – a1o
    Commented Nov 7, 2023 at 16:03
  • Given that you're not asking for a software recommendation, but on a usage guide, this is definitely off-topic here (probably you were directed here without really knowing if it's the proper thing to do). Voted to close as such. Maybe it's a better fit for Stack Overflow, but not entirely sure. You can also try with Unix & Linux Stack Exchange, they even have a relevant git tag.
    – Alejandro
    Commented Nov 7, 2023 at 16:28
  • 1
    I think the question is more than 50% there to being answered completely. Maybe it makes sense to leave it open instead of zealously pushing questions back and forth. After all, people googling in the future will only care about the question and answer, and not if this or a neighboring stackexchange is the perfectly right place for it.
    – a1o
    Commented Nov 7, 2023 at 16:56
  • People will absolutely care if it's posted at the right spot. This is an ok spot though, just your question should have been written more to adapt to here and that's it.
    – Destroy666
    Commented Nov 14, 2023 at 1:04

1 Answer 1

0

Use this one-liner, which you can alias to some shorter command if needed:

git status | grep -Po -m2 '^\s+modified:\s+\K.*' | tail -n1 | xargs git diff

Here, GNU grep uses the following options:
-P : Use Perl regexes.
-o : Print the matches only (1 match per line), not the entire lines.
-m2 : Print a maximum of 2 matches.

^\s+modified:\s+ : matches the beginning of the lines, followed by 1 or more whitespaces, then literal modified:, then 1 or more whitespaces.
\K : Cause the regex engine to "keep" everything it had matched prior to the \K and not include it in the match. Specifically, ignore the preceding part of the regex when printing the match.
.* : 0 or more occurrences of any character.

To alias this one-liner, for example, to gd2, type in your shell or add to your ~/.bashrc:

alias gd2="git status | grep -Po -m2 '^\s+modified:\s+\K.*' | tail -n1 | xargs git diff"

See also:

1
  • 1
    Thank you for your comment! This brings me close to what I want. I will adapt you code once I have the time & then post the solution for others.
    – a1o
    Commented Nov 7, 2023 at 16:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.