如何使用git别名为您的git提交消息添加自定义前缀?

huangapple go评论62阅读模式
英文:

How to add custom prefix to your git commit messages using git aliases?

问题

Here's the translated content without the code:

假设我想让我的提交更加有特色。每个单独的提交都将包含在“-”符号中的特殊前缀类型。例如:

-feat- : 添加了新功能
-fix- : 修复了这个恼人的 bug
-refactor- : 重构了这段代码
-chore- : 编写了一些测试

我想使用 git 别名来自动化所有这些前缀,即为每种类型创建自定义命令:comfeatcomfixcomrefcomchore

到目前为止,我尝试过像这样做:

comfeat = "!f() { git add --all && git commit -am '-feat- : ${1}'; }; f"

当我尝试调用此命令时,我会得到这个结果:

PS D:\Projects\Test> git comfeat "Added new feature"
[master 9fcb0c9] -feat- : ${1}

P.S. 我对 bash/shell 没有先前的知识,所以任何帮助都将不胜感激。

P.S.S. 我看到有人使用 git 钩子来提取分支名称并将其前置到提交中,但我独自在项目上工作,所以我没有为每个正在进行的功能创建很多分支。

英文:

Let's say I want to make my commits more fancy.
Every single commit would have a special prefix type included in "-" symbol. For example :

-feat- : Added new feature
-fix- : Fixed this annoying bug
-refactor- : Refactored this piece of code
-chore- : Wrote some tests

I want to automate all those prefixes using git aliases i. e. create custom commands for each of the type : comfeat, comfix, comref and comchore.

So far I've tried doing something like this:

comfeat = "!f() { git add --all && git commit -am '-feat- : '; }; f"

And when I try to call this command I would get this :

PS D:\Projects\Test> git comfeat "Added new feature"
[master 9fcb0c9] -feat- : 

P.S. I have no prior knowledge in bash/shell so any help will be much appreciated.

P.S.S. I've seen people using git hooks for extracting branch names and prepending them to commits, but I am working alone on the project, so I don't have many branches for each feature I'm working on.

答案1

得分: 1

以下是已翻译的部分:

所以,在一些挖掘和 ChatGPT 的帮助下,我实际上找到了解决这个问题的方法。我的.gitconfig文件别名部分的最终版本如下:

[alias]
comfeat = !sh -c 'git add -A && git commit -m "-feat- : $1"' -
comfix = !sh -c 'git add -A && git commit -m "-fix- : $1"' -
comchore = !sh -c 'git add -A && git commit -m "-chore- : $1"' -
comref = !sh -c 'git add -A && git commit -m "-refactor- : $1"' -

现在我可以键入:

git comref "Refactored user class."
然后我的提交消息将如下所示:

[master 0ba3e4c] -refactor- : Refactored user class.

英文:

So, after some digging and with the help of ChatGPT thingie I actually found a solution to this problem. My finalized version of alias part of .gitconfig file looks like this:

[alias]
	comfeat = !sh -c 'git add -A && git commit -m \"-feat- : $1\"' -
	comfix = !sh -c 'git add -A && git commit -m \"-fix- : $1\"' -
	comchore = !sh -c 'git add -A && git commit -m \"-chore- : $1\"' -
	comref = !sh -c 'git add -A && git commit -m \"-refactor- : $1\"' -

Now I can type:

git comref "Refactored user class."

and my commit message would look like:

[master 0ba3e4c] -refactor- : Refactored user class.

huangapple
  • 本文由 发表于 2023年5月6日 22:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定