Git:是否可以使用预推钩子编辑提交消息? [英] Git: is it possible to edit the commit message using the pre-push hook?

查看:52
本文介绍了Git:是否可以使用预推钩子编辑提交消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户推送其提交之前,我想将分支名称预先添加到提交消息中.这可能吗?

Before users push their commits, I'd like to pre-pend the branch name to the commit message. Is this possible?

如果是,我会使用 git commit --amend 命令吗?

If it is, would I use the git commit --amend command?

我们的分支机构名称包含票证编号,并且在将其合并到master和Jenkins中时,查看此信息很重要.为了轻松查看,它必须是提交消息中的第一行.为了确保第一行是分支名称(包含票证编号),我想使用预推钩子.我可以使用其他钩子,例如 prepare-commit-message 或使用消息模板,但这会将分支名称放在每次提交中,并且不能确保在重新设置基准后该分支名称就在那里.使用预推可确保分支名称在进入远程之前就存在.

Our branch names contain the ticket number and its important to see this info when its merged into master and in Jenkins. To see it easily, it must be the first line in the commit message. To ensure the fist line is the branch name (containing the ticket number), I'd like to use the pre-push hook. I could use other hooks, such as prepare-commit-message or use a message template but that puts the branch name on every commit, and it doesn't ensure that the branch name is there after a rebase. Using the pre-push ensures the branch name is there before it goes into the remote.

=============

=============

在运行之前但在推送任何对象之前,pre-push钩将ref与远程ref进行同步.这意味着遥控器已经为您要推送的更改设置了提交密码.当 pre-push 钩子修改提交消息时,会生成一个新的sha,从而使您立即与遥控器不同步.

The pre-push hook syncs the refs with the remote before running but before any objects are pushed. This means the remote already has the commit sha set for the change you are pushing. When the pre-push hook ammends the commit message a new sha is generated, putting you immediately out of sync with the remote.

推荐答案

正如您所发现的,答案实际上是否".实际上,它是真正否",出于更深层次的技术原因:不可能编辑任何提交. git commit --amend 所做的不是编辑提交",而是将提交推到一边,并更改当前分支名称(无论是什么),以指向新的提交.之前"的图片是:

As you've found, the answer is effectively "no". In fact it's truly "no", for a deeper technical reason: it's impossible to edit any commit. What git commit --amend does is not "edit a commit", but rather, shove the commit aside and change the current branch name, whatever that is, to point to the new commit. The "before" picture is:

...--o--o--*   <--current_branch (HEAD)

之后"是:

          *   [abandoned - in reflog as HEAD@{1}, etc]
         /
...--o--o--X   <--current_branch (HEAD)

但是在您的Git已经调用了另一个Git并愿意通过其哈希ID向其提交提交 * 后,预推挂钩就会运行.您已经用新改进的提交 X 替换了 提交 * 的事实现在不相关了:您的Git专用于推动提交 * ,并要求其Git将分支设置为指向(其副本)commit * .

But the pre-push hook runs after your Git has already called up the other Git and offered to send it commit * by its hash ID. The fact that you've replaced commit * by new-and-improved commit X is now irrelevant: your Git is dedicated to pushing commit * and asking their Git to set their branch to point to (their copy of) commit *.

在此预按挂钩中,您可以做的是拒绝从自己的末端进行推送.现在,推送被拒绝并失败了,您可以重新运行 git push 命令.这次,您将建议向他们发送commit X .如果看起来正确,则无需运行 git commit --amend ,并且可以将提交 X 保留在适当的位置以进行推送,然后运行推送.

What you can do, in this pre-push hook, is reject the push from your own end. Now that the push is rejected and fails, you can re-run your git push command. This time you'll propose to send them commit X. If it looks right, you won't have any need to run git commit --amend and can leave commit X in place for the push, and let the push run.

请注意,最好在外部 Git中执行此操作.代替:

Note that it's far better to do this outside Git. Instead of:

$ git push remotename HEAD:somebranch

或者您实际运行的是什么,您都可以运行:

or whatever it is you actually run, you would just run:

$ jirapush

这是您自己的脚本.在此脚本中,您可以按任意顺序运行任意Git命令,先运行 ,再运行 git push remotename< hash>:< branchname> 或类似的命令您将在最后运行.这些Git命令可以包括例如:

which is your own script. In this script, you can run whatever Git commands you like, in whatever order, before running git push remotename <hash>:<branchname> or whatever it is that you will run at the very end. Those Git commands can include, e.g.:

generate_new_commit_message_text | git commit --amend -F -

如有必要.由于这一切都发生在之前 git push 触发,因此及时执行您想做的所有事情.

if necessary. Since it all happens before git push fires up, it's well in time to do whatever it is you want done.

这篇关于Git:是否可以使用预推钩子编辑提交消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆