티스토리 뷰

반응형

[ 브랜치 목록 조회 ]

 

master에서 develop, feature1, feature2 브랜치를 만들고 

feature2 브랜치만 빼고 원격에 올려준 상태에서 진행하겠습니다. 

 

 

1. local 브랜치 목록 조회  (with no flag)

 

git branch

 

 

 

 

2. remote 브랜치 목록 조회 (with remote flag)

 

git branch -r

 

 

 

 

 

3. 모든 브랜치(로컬 + 원격) 목록 조회 (with all flag)

 

git branch -a

 

 

 

 

[ 머지된 브랜치 목록 조회 ] 

 

feature1을 develop에 머지한 상황입니다. 

 

develop에 머지된 브랜치 목록을 조회할 때

git branch --merged develop

 

 

 

 feature2에 머지된 브랜치 목록을 조회할 때

git branch --merge feature2

 

 

master에 머지된 브랜치 목록을 조회할 때

git branch --merge master

 

 

만약 브랜치를 명시안해주면 현재 선택된 브랜치(feature1)에 머지된 브랜치 목록이 조회됩니다.

git branch --merged

 

 

 

 

위에서 살펴본 것 처럼 

위의 명령어들은 모두 flag를 안붙였기 때문에  local 브랜치들을 기준으로 실행된 것입니다. 

remote 브랜치들만 살펴보려면 -r flag를

local & remote 브랜치들을 살펴보려면 -a flag 를 붙여서 사용하면 됩니다.

 

 

예를들어 develp에 머지된 local & remote 브랜치들을 조회하고 싶을 때

git branch -a --merge develop

 

 

 

 

 

 

[ 특정 브랜치 제외하고 머지된 브랜치 목록 조회  ] 

 

위의 예제들로 출력되는 결과를 보면 머지된 브랜치 뿐만아니라 

자기자신 브랜치랑 base브랜치가 같이 나오고 있는 것을 볼 수 있습니다..! 

 

git-grep 을 이용해서 같이 안나오길 원하는 브랜치를 exclude 할 수 있습니다. 

 

예를 들어

develop에 머지된 local & remote 브랜치들 중,

master 또는 develop이라는 string을 가지는 것을 exclude하고 출력시켜줄 수 있습니다. 

git branch -a --merge develop | grep -i -v -E "master|develop"

 

 

 

 

 

그리고 브랜치를 지정안해주고

현재 선택된 브랜치에 머지된 브랜치들을 출력할 때

자기자신 브랜치가 안나오게 제외해도 좋을 것 같아요!

 

예를들어 feature1이 현재 선택된 브랜치 일때, 

git branch -a --merge

 

git branch -a --merge | grep -i -v -E "master|develop"

 

현재 브랜치까지 제외해주면 

git branch -a --merge | grep -i -v -E "master|develop|$(git branch --show-current)"

 

아무 목록도 안나옵니다. 

 

 

 

[ 머지안된 브랜치 목록 조회 ] 

 

develop에 머지안된 브랜치 목록을 조회할 수 있습니다. 

git branch --no-merge develop

 

 

만약 브랜치를 명시안해주면 현재 선택된 브랜치(feature1)에 머지안된 브랜치 목록이 조회됩니다.

git branch --no-merge

 

 

위의 명령어들은 모두 flag를 안붙였기 때문에  local 브랜치들을 기준으로 실행된 것입니다. 

remote 브랜치들만 살펴보려면 -r flag를

local & remote 브랜치들을 살펴보려면 -a flag 를 붙여서 사용하면 됩니다.

 

 

 

[ 참고 ] 

 

master에서 각 브랜치들을 만들어주고 각 브랜치에서 아직 아무 커밋도 안했을 때는

 

머지된 브랜치들 목록 출력하면 

전부 다 나오더라구요...🥲

git branch --merged develop

 

 

git branch --no-merged develop

아무 것도 안나옴

 

 

git branch --merged feature1

 

 

 

 

 

[  feature 브랜치를 지우면..? ] 

 

develop에 머지된 feature1 브랜치(로컬)를 지우고

merged된 브랜치 목록을 출력해보면 feature1이 안나옵니다.

 

 

깃헙에서 PullRequest로 머지된 브랜치(리모트)를 지우고

merged된 리모트 브랜치 목록을 출력해보면 해당 브랜치가 안나옵니다.

 

 

깃헙의 Restore Branch로 리모트 브랜치를 다시 되살리고

merged된 리모트 브랜치 목록을 출력해보면 해당 브랜치가 나옵니다.

 

 

[ Reference ]

git-scm.com/docs/git-branch/2.4.12

 

Git - git-branch Documentation

If --list is given, or if there are no non-option arguments, existing branches are listed; the current branch will be highlighted with an asterisk. Option -r causes the remote-tracking branches to be listed, and option -a shows both local and remote branch

git-scm.com

 

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

 

How can I know if a branch has been already merged into master?

I have a git repository with multiple branches. How can I know which branches are already merged into the master branch?

stackoverflow.com

 

반응형
댓글