Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run 'git diff some/long/file/name' without typing the full path?

In my workflow, I often run the following pair of commands:

$ git status
M README.txt
M some/long/file/name
$ git diff some/long/file/name

Is there any way, for fast typing/use_shortcat for long file name without copying it name (this action require using mouse and it's no so fast like typing)? Maybe something like git diff $2, where $2 is second changed file from the status list...?

like image 239
Kein Avatar asked Aug 17 '15 18:08

Kein


People also ask

How do I enable long path support in git?

windows-10-git.mdGo to Computer Configuration > Administrative Templates > System > Filesystem in gpedit. msc , open Enable Win32 long paths and set it to Enabled .

How do you tell the difference in files in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

What does M mean in git diff?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.


2 Answers

Another way you can do it without installing a separate tool is to strip the output of git status, and pipe it through sed, then back to git diff. Its a long command, so you can put it in your .bashrc and alias it. For example, putting this in my .bashrc:

 myfunction() {
 git status --porcelain | sed -n "${1} s/^...//p' | xargs git diff
 }
 alias gd=myfunction

I can then do

>> git status
M main.cpp
M tipsy.cpp
M other.cpp
>> gd 2

And the output is git diff of the second file.

EDIT: I combined the two seds into one, because having two seperate ones was silly.

like image 39
timdykes Avatar answered Sep 19 '22 07:09

timdykes


You can also use * placeholders as suggested in this answer. Usually you don't have to type the full name of the file like this

git diff -- **/name

Given that a short segment of the name, e.g. "na", is unique within the list of modified files, you can also do something like this:

git diff -- *na*

This way you don't have to count the entries to find out which number it is that you want to diff.

like image 105
lex82 Avatar answered Sep 20 '22 07:09

lex82