your programing

브랜치가 이미 마스터에 병합되었는지 어떻게 알 수 있습니까?

lovepro 2020. 9. 27. 13:34
반응형

브랜치가 이미 마스터에 병합되었는지 어떻게 알 수 있습니까?


여러 분기가있는 git 저장소가 있습니다.

이미 마스터 브랜치에 병합 된 브랜치를 어떻게 알 수 있습니까?


git branch --merged master마스터에 병합 된 분기를 나열합니다.

git branch --mergedHEAD에 병합 된 분기를 나열합니다 (예 : 현재 분기의 팁).

git branch --no-merged 병합되지 않은 분기를 나열합니다.

기본적으로 이것은 로컬 브랜치에만 적용됩니다. -a플래그는 로컬 및 원격 지사, 그리고 모두가 표시됩니다 -r플래그 쇼에만 원격 지점.


git merge-base명령을 사용 하여 두 분기 간의 최신 공통 커밋을 찾을 수 있습니다 . 해당 커밋이 브랜치 헤드와 같으면 브랜치가 완전히 병합 된 것입니다.

이미 완전히 병합 되지 않은git branch -d 브랜치를 삭제하는 것을 거부하기 때문에 이미 이런 종류의 작업 수행합니다 .


그래픽 인터페이스 솔루션도 있습니다. 그냥 입력

gitk --all

새 애플리케이션 창에 전체 저장소의 그래픽 표현이 표시됩니다. 여기서 분기가 이미 병합되었는지 여부를 쉽게 알 수 있습니다.


다음과 같은 bash 함수를 사용하고 있습니다. git-is-merged develop feature/new-feature

git-is-merged () {
  merge_destination_branch=$1
  merge_source_branch=$2

  merge_base=$(git merge-base $merge_destination_branch $merge_source_branch)
  merge_source_current_commit=$(git rev-parse $merge_source_branch)
  if [[ $merge_base = $merge_source_current_commit ]]
  then
    echo $merge_source_branch is merged into $merge_destination_branch
    return 0
  else
    echo $merge_source_branch is not merged into $merge_destination_branch
    return 1
  fi
}

사용 git merge-base <commit> <commit>.

이 명령은 두 커밋 사이에서 가장 공통적 인 조상을 찾습니다. 공통 조상이 "branch"의 마지막 커밋과 동일하면 "branch"가 이미 마스터에 병합되었다고 안전하게 가정 할 수 있습니다.

단계는 다음과 같습니다.

  1. 마스터 브랜치에서 마지막 커밋 해시 찾기
  2. "분기"에서 마지막 커밋 해시 찾기
  3. 명령을 실행하십시오 git merge-base <commit-hash-step1> <commit-hash-step2>.
  4. 3 단계의 출력이 2 단계의 출력과 같으면 "분기"가 이미 마스터에 병합 된 것입니다.

git merge-base https://git-scm.com/docs/git-merge-base 에 대한 자세한 정보 .


원격 지점 정리에 관한 주제

git branch -r | xargs -t -n 1 git branch -r --contains

여기에는 각 원격 분기와 최신 SHA가있는 원격 분기가 나열됩니다.

이것은 어떤 원격 브랜치가 병합되었지만 삭제되지 않았는지, 병합되지 않았고 따라서 감소 하는지를 식별하는 데 유용합니다.

'tig'(gitk와 비슷하지만 터미널 기반)를 사용하는 경우 다음을 수행 할 수 있습니다.

tig origin/feature/someones-decaying-feature

git checkout없이 브랜치의 커밋 히스토리보기


기능 브랜치의 일반적인 시나리오 인 메인 브랜치로 최신 상태로 업데이트 된 경우에도 브랜치가 병합되었는지 알아 내야 할 때 내 기술은 다음과 같습니다.

이러한 접근 방식 중 어느 것도 어리석은 증거는 아니지만 여러 번 유용하다는 것을 알았습니다.

1 모든 지점에 대한 로그 표시

gitk 또는 TortoiseGit과 같은 시각적 도구를 사용하거나 --all을 사용하여 간단히 git log를 사용하여 기록을 살펴보고 기본 브랜치에 대한 모든 병합을 확인합니다. 이 특정 기능 분기가 병합되었는지 여부를 확인할 수 있습니다.

2 기능 브랜치에서 병합 할 때 항상 원격 브랜치를 제거합니다.

기능 브랜치에서 병합 할 때 항상 로컬 브랜치와 원격 브랜치를 모두 제거하는 좋은 습관이있는 경우 다른 컴퓨터에서 원격 브랜치를 업데이트하고 정리하면 기능 브랜치가 사라집니다.

To help remember doing this, I'm already using git flow extensions (AVH edition) to create and merge my feature branches locally, so I added the following git flow hook to ask me if I also want to auto-remove the remote branch.

Example create/finish feature branch

554 Andreas:MyRepo(develop)$ git flow start tmp
Switched to a new branch 'feature/tmp'

Summary of actions:
- A new branch 'feature/tmp' was created, based on 'develop'
- You are now on branch 'feature/tmp'

Now, start committing on your feature. When done, use:

     git flow feature finish tmp

555 Andreas:MyRepo(feature/tmp)$ git flow finish
Switched to branch 'develop'
Your branch is up-to-date with 'if/develop'.
Already up-to-date.

[post-flow-feature-finish] Delete remote branch? (Y/n)
Deleting remote branch: origin/feature/tmp.

Deleted branch feature/tmp (was 02a3356).

Summary of actions:
- The feature branch 'feature/tmp' was merged into 'develop'
- Feature branch 'feature/tmp' has been locally deleted
- You are now on branch 'develop'

556 Andreas:ScDesktop (develop)$

.git/hooks/post-flow-feature-finish

NAME=$1
ORIGIN=$2
BRANCH=$3

# Delete remote branch
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

while true; do
  read -p "[post-flow-feature-finish] Delete remote branch? (Y/n) " yn
  if [ "$yn" = "" ]; then
    yn='Y'    
  fi
  case $yn in
      [Yy] ) 
        echo -e "\e[31mDeleting remote branch: $2/$3.\e[0m" || exit "$?"
        git push $2 :$3; 
        break;;
      [Nn] ) 
        echo -e "\e[32mKeeping remote branch.\e[0m" || exit "$?"
        break;;
      * ) echo "Please answer y or n for yes or no.";;
  esac
done

# Stop reading user input (close STDIN)
exec <&-
exit 0

3 Search by commit message

If you do not always remove the remote branch, you can still search for similar commits to determine if the branch has been merged or not. The pitfall here is if the remote branch has been rebased to the unrecognizable, such as squashing commits or changing commit messages.

  • Fetch and prune all remotes
  • Find message of last commit on feature branch
  • See if a commit with same message can be found on master branch

Example commands on master branch:

gru                   
gls origin/feature/foo
glf "my message"

In my bash .profile config

alias gru='git remote update -p'
alias glf=findCommitByMessage

findCommitByMessage() {
    git log -i --grep="$1"
}

Here is a little one-liner that will let you know if your current branch incorporates or is out of data from a remote origin/master branch:

$ git fetch && git branch -r --merged | grep -q origin/master && echo Incorporates origin/master || echo Out of date from origin/master

I came across this question when working on a feature branch and frequently wanting to make sure that I have the most recent work incorporated into my own separate working branch.

To generalize this test I have added the following alias to my ~/.gitconfig:

[alias]
   current = !git branch -r --merged | grep -q $1 && echo Incorporates $1 || echo Out of date from $1 && :

Then I can call:

$ git current origin/master

to check if I am current.


In order to verify which branches are merged into master you should use these commands:

  • git branch <flag[-r/-a/none]> --merged master list of all branches merged into master.
  • git branch <flag[-r/-a/none]> --merged master | wc -l count number of all branches merged into master.

Flags Are:

  • -a flag - (all) showing remote and local branches
  • -r flag - (remote) showing remote branches only
  • <emptyFlag> - showing local branches only

for example: git branch -r --merged master will show you all remote repositories merged into master.

참고URL : https://stackoverflow.com/questions/226976/how-can-i-know-if-a-branch-has-been-already-merged-into-master

반응형