Programing

git의 병합 커밋 메시지를 어떻게 사용자 정의 할 수 있습니까?

lottogame 2020. 11. 21. 08:17
반응형

git의 병합 커밋 메시지를 어떻게 사용자 정의 할 수 있습니까?


병합을 할 때마다 병합 커밋이 생성되어야하며 모든 커밋의 요약 이상을 갖고 싶습니다.

내 질문은 git-fmt-merge-msg 형식을 지정하거나이 자동화 된 메시지를 결정 하는 방법입니다 (커밋 후 수정하고 git-log --pretty = format : '...'을 사용하여 수동으로 수행 할 수 있음).

예를 들어 다음과 같이 형식을 지정하고 싶습니다.

 Merge branch 'test'  
    * test:  
      [BZ: #123] fifth commit subject  
      [BZ: #123] fourth commit subject  
      [BZ: #123] third commit subject  
      [BZ: #123] second commit subject  
      [BZ: #123] first commit subject  

 __________________________________________
 Merge details:  
     [BZ: #123] fifth commit subject  
               at 2010-06-30 11:29:00 +0100  
       - fifth commit body  

     [BZ: #123] fourth commit subject  
               at 2010-06-30 11:22:17 +0100  
       - fourth commit body  

     [BZ: #123] third commit subject  
               at 2010-06-30 11:21:43 +0100  
       - third commit body  

     [BZ: #123] second commit subject  
               at 2010-06-30 11:21:30 +0100  
       - second commit body  

     [BZ: #123] first commit subject  
               at 2010-06-30 11:29:57 +0100  
       - first commit body

나는 이와 같은 것을하고 싶었다. 나는 git fmt-merge-msg일할 합리적인 방법을 찾지 못했습니다 . 내가 원하는대로 작동하지 않는다고 생각합니다 (메시지에 사용할 완전히 사용자 지정 텍스트를 전달). 그래서 대신 -no-commitcommit -F명령을 사용하는 다른 방법을 알아 냈습니다 . 물론 출력은 사용자 정의 할 수 있지만 출력을 원한다고 말한 것과 거의 정확히 일치합니다.

커밋 메시지 출력 샘플 :

Merge branch fix4 into master

::SUMMARY::
Branch fix4 commits:
Add fix4b-4
Add fix4b-3
Add fix4b-2
Add fix4b-1

Branch master commits:
fix4b-5 on master

* * * * * * * * * * * * * * * * * * * * * * * * *
::DETAILS::
commit < 98ffa579e14610b3566e1a3f86556a04dc95a82b
Author: -----
Date:   Fri Aug 17 17:23:26 2018 -0400

    fix4b-5 on master

commit > 7e386dddee16a7c2588954d25dd6793cdaa1b562
Author: -----
Date:   Fri Aug 17 15:18:17 2018 -0400

    Add fix4b-4

    use log output as commit message

    commit 2e630b1998312ec1093d73f9fe77b942407f45e8
    Author: -----
    Date:   Fri Aug 17 15:15:28 2018 -0400

        Add fix4b-3

commit > 2e630b1998312ec1093d73f9fe77b942407f45e8
Author: -----
Date:   Fri Aug 17 15:15:28 2018 -0400

    Add fix4b-3

commit > c9bb199be49c17ca739d019d749263314f05fc46
Author: -----
Date:   Fri Aug 17 15:15:27 2018 -0400

    Add fix4b-2

commit > 5b622a935c9d078c7d0ef9e195bccf1f98cce5e4
Author: -----
Date:   Fri Aug 17 15:15:27 2018 -0400

    Add fix4b-1

그리고 사용법은 다음과 같습니다.

$ git mergelogmsg branch-name

여기에 별칭을 복사하겠습니다.

[alias]
    mergelogmsg = "!f() { var=$(git symbolic-ref --short HEAD) && printf 'Merge branch %s into %s\n\n::SUMMARY::\nBranch %s commits:\n' $1 $var $1 > temp_merge_msg && git log --format=format:'%s' $var..$1 >> temp_merge_msg && printf '\n\nBranch %s commits:\n' $var >> temp_merge_msg && git log --format=format:'%s' $1..$var >> temp_merge_msg && printf '\n\n* * * * * * * * * * * * * * * * * * * * * * * * *\n::DETAILS::\n' >> temp_merge_msg && git log --left-right $var...$1 >> temp_merge_msg && git merge --no-ff --no-commit $1 && git commit -eF temp_merge_msg; rm -f temp_merge_msg;}; f" 

복사하여 붙여 넣어 사용자 정의하려면 위를 사용하십시오. 아래 버전에는 원하지 않는 줄 바꿈이 있지만 내가하는 일을 설명하는 데 사용할 것입니다.

[alias]
1   mergelogmsg = "!f() { var=$(git symbolic-ref --short HEAD) && 
2        printf 'Merge branch %s into %s\n\n::SUMMARY::\nBranch %s commits:\n' $1 $var $1 > temp_merge_msg && 
3        git log --format=format:'%s' $var..$1 >> temp_merge_msg && 
4        printf '\n\nBranch %s commits:\n' $var >> temp_merge_msg && 
5        git log --format=format:'%s' $1..$var >> temp_merge_msg && 
6        printf '\n\n* * * * * * * * * * * * * * * * * * * * * * * * *\n::DETAILS::\n' >> temp_merge_msg && 
7        git log --left-right $var...$1 >> temp_merge_msg && 
8        git merge --no-ff --no-commit $1 && 
9        git commit -eF temp_merge_msg; rm -f temp_merge_msg;}; f"

좋구나...

1 행 은 사용자 정의 함수를 bash 쉘 스크립트로 시작하므로 git은 그것이 git 명령이 아님을 알 수 있습니다. 나중에 사용할 수 있도록 현재 분기 (다른 분기를 마스터로 병합하는 경우 마스터)를 변수로 설정합니다.
2 행은 현재 분기와 원래 명령에 제공 한 분기 이름을 사용하여 첫 번째 행을 인쇄합니다 (일반 병합 명령에서와 마찬가지로). 이것은 임시 파일에 기록합니다.
3 행 은 현재 분기에없는 수신 분기의 커밋 로그를 가져와 해당 커밋의 주제 만 임시 파일에 기록합니다.
4 행은 다음 행을 temp에 인쇄합니다.
5 행들어오는 분기에없는 현재 분기의 커밋 로그를 가져와 해당 커밋의 주제 만 임시 파일에 기록합니다.
6 행 은 요약 부분과 세부 정보 부분 사이에 약간의 가로 구분선을 인쇄합니다.
7 행 은 현재 분기와 들어오는 분기의 모든 커밋 로그를 서로 분기하거나 마지막으로 조상을 공유 한 직후의 시간으로 되돌립니다. 왼쪽-오른쪽은 커밋의 출처를 나타내는 화살표를 제공합니다. <는 현재 분기를 의미하고>는 들어오는 분기를 의미합니다.
8 행 은 빨리 감기없이 (그러므로 커밋을 얻음) 커밋없이 들어오는 브랜치와 병합 명령을 실행합니다 (따라서 직접 작성해야합니다 ... 아,하지만 그렇게하지 않습니다!).
9 행-e-F매개 변수를 사용 하여 commit 명령을 실행하여 편집을 허용하고 지정된 파일의 텍스트로 메시지를 채우도록 커밋에 지시합니다. 커밋 메시지를 원하는대로 완료하면 병합을 커밋하고 임시 파일을 삭제합니다.

타다! ;긴 명령 끝에 있는 두 개 는 printf 함수가 콘솔에 기록하지 않고 파일에만 기록하도록 만듭니다.


이것이 원래 질문에 대한 답이 아니라는 것을 알고 있지만, 현재 "git change merge commit message"에 대한 첫 번째 Google 결과이기 때문에이 페이지에 도달 한 저와 같은 git noob의 이익을 위해 가능하다고 언급하겠습니다. 에:

git commit --amend -m"New commit message"

병합 커밋의 부모에 대한 링크를 잃지 않고 병합 커밋의 커밋 메시지를 변경합니다.


Looks like as of version Git 1.7.8 you can do git merge --edit ... to specify the commit message.

And as of 1.7.10, dropping into edit mode will be the default behavior

From this release on, the "git merge" command in an interactive session will start an editor when it automatically resolves the merge for the user to explain the resulting commit, just like the "git commit" command does when it wasn't given a commit message.

(though I'm not seeing it on in msysgit on windows).


I've found there are two ways for solving this problem

note: don't use both at the same time, as if the commit fails to merge it will add the log again to the bottom.

personal note: I'm using the first solution as it relies entirely on git's hooks and config properties, instead of an external script.
For a real solution one would have to extend a git command named 'fmt-merge-msg' that generates the oneline descriptions when passing the --log option (if you really need this solution you'll have to create your own patch (for git) and compile it from source).

1. using prepare-commit-message as VonC suggested
this solution has the problem that you need to interrupt the commit and then commit manually

setting an alias that will build the desired commit message:

[alias]  
lm = log --pretty=format:'%s%n   by %C(yellow)%an%Creset (%ad)%n %n%b' --date=local

creating the prepare-commit-msg hook by creating an executable prepare-commit-msg in $GIT_DIR/hooks/ (example script below)

#!/bin/sh
#...

case "$2,$3" in  
  merge,)  
  echo "Merge details:" >> $1  
  echo "" >> $1  
  git lm ORIG_HEAD..MERGE_HEAD >> "$1" ;;  
*) ;;  
esac  

one should define an alias commit msg such as

[alias]  
m = merge --no-ff --no-commit

2. using a custom command that will generate the merge automatically
(using the lm alias created in 1.)

#!/bin/sh

echo ""
echo "merge with commit details -- HEAD..$1"
git merge --no-ff --no-log -m "`git lm HEAD..$1`" --no-commit $1

and then execute a rather rigid command:

./cmd-name <branch to merge>

if you still wish to have the oneline description of the commits you'll need to add new commands or whatever to the -m argument (if you use --log then it will be generated on the bottom)


Once you merge your <branch A> to <branch B>, git will automatically commit a message saying "merge branch <branch A> into <branch B>.

If you want to customize git's merge commit message you can try:

$ git commit --amend -m "Your merge message"

This command will update your git's merge commit message to your commit message.

you can also try :

$ git merge <branch A> --no-commit

it will merge your <branch B> with <branch A>, with list of <Branch B>'s commit and commit messages

If it fails to do fast-forward, then you will get something like this:

Automatic merge went well; stopped before committing as requested

# check this with git status 
$ git status

It will show you, your commits are already added to stage but not yet commited so, you can commit them without running git add:

$ git commit -m "your merge commit message"

If you want to change <branch B>'s last commit message then again you can try:

$ git commit --amend -m "your new commit message"

But, generally, we don't update other commit messages, unless they are incorrect.

Suppose, you get conflict after git merge, then simply resolve your conflict and do:

$ git add .
$ git commit -m "your commit message"

There's also a --log option for git-merge now, which does half of what you want - it places the shortlog (commit summaries) in the merge message. A full solution will have to use a hook like in VonC's answer, though.


You could try defining a prepare-commit-msg hook (the sample one does generate some custom "default commit messages")


Coming late to the party, but nowadays we can just use
git merge --squash <branch>
to merge another branch into a single commit on my current branch and prepopulating our commit message with all the merged commit messages -- and the detailed messages, too, not just the one-liners.
I looked for ages for this command.


A very simple bash function that set's a default message and adds your argument. It opens your editor with the --edit switch if you want make changes.

edit ~/.bashrc or bash_aliases. (Don't forget to source ~/.bashrc) to apply changes in your bashrc

function mergedevelop()
{
    git merge --no-ff --edit -m "master <-- develop: $1" develop;
}

use:

mergedevelop "PR #143..." to have message:

master <-- develop: PR #143...

참고URL : https://stackoverflow.com/questions/3148863/how-can-i-customize-gits-merge-commit-message

반응형