简体   繁体   English

Git:是否可以使用预推钩子编辑提交消息?

[英]Git: is it possible to edit the commit message using the pre-push hook?

Before users push their commits, I'd like to pre-pend the branch name to the commit message. 在用户推送其提交之前,我想在分支名称之前添加提交消息。 Is this possible? 这可能吗?

If it is, would I use the git commit --amend command? 如果是,我会使用git commit --amend命令吗?

Our branch names contain the ticket number and its important to see this info when its merged into master and in Jenkins. 我们的分支机构名称包含票证编号,并且在将其合并到master和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. 我可以使用其他挂钩,例如prepare-commit-message或使用消息模板,但这会将分支名称放在每次提交中,并且它不能确保在重新设置基准之后,分支名称就在那里。 Using the pre-push ensures the branch name is there before it goes into the remote. 使用预推确保分支名称进入远程之前就在那里。

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

update: Yes, it is possible but its a bad idea. 更新:是的,有可能,但这是一个坏主意。

The pre-push hook syncs the refs with the remote before running but before any objects are pushed. 运行之前但在推入任何对象之前 ,预推钩将ref与远程同步。 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. pre-push钩修改了提交消息时,将生成一个新的sha,从而使您立即与遥控器不同步。

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. git commit --amend所做的不是“编辑提交”,而是将提交推到一边,并更改当前分支名称(无论是什么名称)以指向新的提交。 The "before" picture is: “之前”的图片是:

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

and the "after" one is: 而“之后”是:

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

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. 但是预推挂钩在您的Git已经调用了另一个Git并提出通过其哈希ID向其提交commit * 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 * . 您已经新的和改进的提交X 替换了 commit *的事实现在是不相关的:您的Git专用于推送commit *并要求其Git将分支设置为指向commit *的副本。

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. 现在,推送被拒绝并失败了,您可以重新运行git push命令。 This time you'll propose to send them commit X . 这次,您将建议发送给他们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 commit --amend并且可以将提交X保留在适当的位置以进行推送,然后让推送运行。

Note that it's far better to do this outside Git. 请注意,最好 Git 之外执行此操作。 Instead of: 代替:

$ git push remotename HEAD:somebranch

or whatever it is you actually run, you would just run: 或无论您实际运行什么,都可以运行:

$ jirapush

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. 在此脚本中,您可以运行git push remotename <hash>:<branchname> 之前以任何顺序运行任何所需的Git命令,或者最终运行它。 Those Git commands can include, eg: 这些Git命令可以包括,例如:

generate_new_commit_message_text | git commit --amend -F -

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 push启动之前发生的,所以现在是时候做您想要做的事情了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM