识别在分支上完成的提交 [英] Identifying commits that were done on a branch
问题描述
我有一个功能分支-称它为featureX'
当我在开发它时-我创建了多个提交,将它们发送到服务器,对其进行测试-还有多个提交,等等.
稍后,我将该分支合并到master-我想在历史记录中看到,哪些提交在提交给master之前是该featureX
分支的一部分.
I have a feature branch - let's call it featureX'
When I'm developing it - I create several commits, send them to the server, test it - several more commits etc...
When later I merge this branch to master - I want to see in the history, which commits were part of this featureX
branch before they were committed to master.
目前,我的方式是记得每次提交时都将其添加到评论中-但是我很懒,而且并不总是记得这样做.
Currently my way is to remember to add that to the comment whenever I commit - but I'm lazy and not always remember to do so.
是否可以标记这些提交,或自动在其注释中添加带有分支名称的前缀?
Is there a way to tag these commits, or automatically add to their comments a prefix with the branch name?
我在Visual Studio中使用git,并且使用的是UI
I'm using git in Visual Studio, using it's UI
推荐答案
是的,有一种方法可以在其注释中自动添加带有分支名称的前缀.可以使用commit-msg
挂钩来实现.例如,如果您将此代码写入文件.git/hook/commit-msg
:
Yes there is a way to automatically add a prefix with the branch name to their comments. It can be acheived using the commit-msg
hook. For example, if you write this code into the file .git/hook/commit-msg
:
#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
echo '['"$current_branch"'] '$(cat "$1") > "$1"
您所有的提交消息都将自动以"[branch_name]
"为前缀.
All your commit messages will be automatically prefixed with "[branch_name]
".
我实际上设法在Windows中编写了相同的脚本
I actually managed to write the same script in for windows
在名为.git/hooks/commit-msg.bat
的文件中编写以下代码(几乎等同于上面的三行):
In a file called .git/hooks/commit-msg.bat
write this code (which is almost the equivalent of the three lines above):
@echo off
FOR /F "tokens=* USEBACKQ" %%F IN (`git rev-parse --abbrev-ref HEAD`) DO (
SET current_branch=%%F
)
FOR /F "tokens=* USEBACKQ" %%F IN (`type .git\COMMIT_EDITMSG`) DO (
SET commit_msg=%%F
)
SET p_commit_msg=[%current_branch%] %commit_msg%
ECHO %p_commit_msg% > .git\COMMIT_EDITMSG
在文件.git/hooks/commit-msg
中,按以下方式调用此脚本:
In the file .git/hooks/commit-msg
, call this script like that:
#!/bin/bash
cmd.exe /c "commit-msg.bat"
这篇关于识别在分支上完成的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!