带有可选参数的 git 别名
我想确保我的最新提交在推送之前具有当前日期。由于我总是在合并+推送之前重新设置为 master,所以我创建了这个别名:
[alias]
sync = !git commit --amend --date=today && git rebase master
问题是,它不断启动我的文本编辑器,要求新的提交消息。有没有办法拥有一个可选参数,以便我可以选择使用:
git sync 'my commit message'
或者
git sync
后者将简单地使用现有的提交消息,无论它是什么?
I want to ensure that my latest commit has the current date before I push. Since I always rebase to master before a merge + push, I made this alias:
[alias]
sync = !git commit --amend --date=today && git rebase master
Problem is, it keeps launching my text editor asking for a new commit message. Is there a way to have an optional parameter, so that I can choose to use either:
git sync 'my commit message'
or
git sync
where the latter will simply use the existing commit message, whatever it happens to be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将额外的参数应用于除别名“命令行”末尾之外的任何内容,您需要将 shell 命令放入脚本中。您可以使用外部脚本来完成此操作(例如 jdelStrother 的回答),或者您可以使用“内联”shell 脚本来完成此操作。
您可以使用
-m
来提供git commit
您的新消息或使用-C HEAD
/--reuse-message=HEAD
选项让它使用现有的消息和作者(它也会重用作者时间戳,但您要使用--date=...
重置它)。使用任何这些选项都会阻止 Git 打开提交消息的编辑器。这是一个“内联”shell 脚本:
这个小脚本的核心是一对条件参数扩展:
当您使用额外参数(即替换日志消息)调用它时,这些扩展为两个 shell 字:
-m“<您的新日志消息>”
。当您不提供额外参数时,它们将扩展为仅一个单词:“--reuse-message=HEAD”
。尾随破折号也很重要;它可以是任何 shell 单词,重点是必须有一些东西,因为 shell 将使用它来初始化其
$0
参数(通常有一个默认值,因此对于条件扩展本身来说是无用的) )。如果我误解了,并且您实际上想在不提供额外参数时看到编辑器,请使用单个扩展
${1+-m "$1"}
而不是一对扩展。To apply the extra parameters to anything except the end of your alias’ “command line”, you will need to put your shell commands in a script. You can do it with an external script (like jdelStrother’s answer), or you can do it with an “inline” shell script.
You can use
-m
to feedgit commit
your new message or use the-C HEAD
/--reuse-message=HEAD
option to have it use the existing message and author (it would also reuse the author timestamp, but you are resetting that with--date=…
). Using any of these options will prevent Git from opening an editor for your commit message.Here it is as an “inline” shell script:
The core of this small script is the pair of conditional parameter expansions:
When you call it with an extra parameter (i.e. your replacement log message), these expand to two shell words:
-m "<your new log message>"
. When you do not supply the extra parameter, they expand to just a single word:"--reuse-message=HEAD"
.The trailing dash is also important; it could be any shell word, the point is that something must be there because the shell will use it to initialize its
$0
parameter (which usually has a default value, so it is useless for the conditional expansion itself).If I misunderstood and you actually want to see the editor when you do not supply the extra parameter, then use the single expansion
${1+-m "$1"}
instead of the pair of expansions.当您的别名开始变得更加复杂时,最简单的方法可能就是为它们创建一个单独的脚本。如果您将文件“git-sync”添加到路径中,则当您执行“gitsync”时会自动调用该文件。
因此,如果您使用类似 - 的内容创建该文件,
那可能会起作用。不过它是我凭空打出来的,所以请注意讲师。
When your aliases start getting more complex, it's probably easiest just to create a separate script for them. If you add a file 'git-sync' to your path, it will be automatically called when you do 'git sync'.
So, if you created that file with something along the lines of -
- that would probably work. It's typed off the top of my head though, so caveat lector.