Git是否从git状态列表添加第一个文件?

yftpprvb  于 2023-05-12  发布在  Git
关注(0)|答案(3)|浏览(93)

我有一个“懒惰”的问题。假设在git status列表输出中有一个新添加到项目中的文件列表。有没有一个快捷方式git add的第一个文件在该列表中没有键入其名称?

tzcvj98z

tzcvj98z1#

我不知道有什么捷径,但你可以自己编写脚本,非常容易。

git add $(git status --porcelain | sed s/^...// | head -n 1)

您可以尝试添加aliases using Git或仅为您的shell(如bash, zsh)。

fcg9iug3

fcg9iug32#

不需要输入它的名字,你可以使用Git的GUI Package 器,比如SourceTree或IntelliJ的VCS。只需单击一下,它就会为您运行git add命令。

q3qa4bjr

q3qa4bjr3#

我在自己的git中有一个别名:

git config --global alias.add1 '!f() { git add $(git status --porcelain | sed s/^...// | head -n 1); }; f'

我现在可以使用git add1调用它。如果你想要另一个别名,你可以改变alias.your_name到你的首选项。
因为有一个表达式substition $(),所以我们不能把is写成“标准”别名:

git config --global alias.add1 'add $(git status --porcelain | sed s/^...// | head -n 1); }; f'

因此我们使用!,它允许我们run external commands in an alias,并将表达式 Package 在函数中,并调用该函数。

相关问题