Git 命令,按状态输出索引引用文件?

发布于 2024-11-14 15:08:20 字数 1351 浏览 1 评论 0原文

有时,如果有许多相似的文件名,Git 可能会相当乏味。例如:

# 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")

我更愿意输入 git diff 1 而不是 git diff FormRegion1.Designer.cs (即使使用制表符补全)。目前我正在做这样的事情:

git diff $( ~/gitstatn 1 )

where ~/gitstatn contains:

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

这也好不到哪儿去。

如何输入类似 git add 3git diff 5 的内容并表示 git add FormRegion1.resxgit diff OptionPage.cs分别是?

我在 Windows 上使用 Cygwin。

编辑 - 根据 adymitruk 的建议,我决定将 gpick 别名为脚本:

#!/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

这足以满足我的需求。

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, I'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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世晨晓 2024-11-21 15:08:20

请改用 git ls-files。你不需要剪。您将需要编写脚本。您可以使用 -p 参数进行添加,但我喜欢您的脚本。让脚本接受您想要的命令:

gpick diff 1
gpick add 3

您可能也想对未跟踪的文件执行某些操作。

希望这有帮助

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

滥情空心 2024-11-21 15:08:20

查看 git-number我专门为此用例编写了它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文