开发者

Git commands, reference files by status output index?

Sometimes Git can be rather tedious if there are many similar filenames. For instance:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   FormRegion1.Designer.cs
#       modified:   FormRegion1.cs
#       modified:   FormRegion1.resx
#       modified:   OptionPage.Designer.cs
#       modified:   OptionPage.cs
#       modified:   OptionPage.resx
#
no changes added to commit (use "git add" and/or "git commit -a")

I'd much rather type git diff 1 instead of git diff FormRegion1.Designer.cs (even with tab-completion). Currently I'm doing something like this:

git diff $( ~/gitstatn 1 )

where ~/gitstatn contains:

git status -s | head -n$1 | tail -n1 | cut -c 4-

which is no better.

How can I type something like git add 3 or git diff 5 and mean git add FormRegion1.resx or git diff OptionPage.cs, respectively?

I'm using Cygwin on Windows.

.

Edit - As per adymitruk's suggestion, 开发者_如何学CI've settled on aliasing gpick to a script:

#!/usr/bin/bash

if [ -z $1 ]; then
    echo 'no git command specified'
elif [ -z $2 ]; then
    git $1
else
    git $1 $( git ls-files -m | head -n$2 | tail -n1 )
fi

which is sufficient for my needs.


Use git ls-files instead. You won't need cut. You will need to script. You could use the -p parameter for add, but I like your script. make the script accept the command you want:

gpick diff 1
gpick add 3

You may want to do something for untracked files too.

Hope this helps


Check out git-number. <plug>I wrote it specifically for this use case</plug>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜