programing

Git에서 현재 분기만 표시

muds 2023. 5. 16. 23:11
반응형

Git에서 현재 분기만 표시

다음과 같은 Git 명령이 있습니까?

git branch | awk '/\*/ { print $2; }'
$ git rev-parse --abbrev-ref HEAD
master

Git 1.6.3 이상에서 작동해야 합니다.

Git 2.22(2019년 2분기)를 사용하면 다음과 같은 방식을 사용할 수 있습니다.git branch --show-current.

다니엘스 우마놉시스()umanovskis의 커밋 0ecb1fc(2018년 10월 25일)를 참조하십시오.
(Junio C Hamano에 의해 합병 -- -- 3710f60, 2019년 3월 7일 커밋)

branch소개하다--show-current표시 옵션

호출할 때--show-current,git branch현재 지점 이름을 인쇄하고 종료합니다.
실제 이름만 인쇄되며, 이름은 인쇄되지 않습니다.refs/heads.
HEAD가 분리된 상태에서는 아무것도 출력되지 않습니다.

스크립팅 및 대화형/정보 제공용으로 제작되었습니다.
와는 달리git branch --list지점 이름만 가져오는 데는 필터링이 필요하지 않습니다.

2018년 10월 Git 메일링 리스트의 원본 토론과 실제 패치를 참조하십시오.


경고: 올리비에댓글에 언급된 바와 같이:

이것이 모든 상황에서 작동하는 것은 아닙니다!
예를 들어 하위 모듈에 있는 경우에는 작동하지 않습니다.
'git symbolic-ref --short HEAD항상 작동합니다.

Git 1.8.1에서는 git symbolic-ref 명령을 "--short" 옵션과 함께 사용할 수 있습니다.

$ git symbolic-ref HEAD
refs/heads/develop
$ git symbolic-ref --short HEAD
develop

의 출력에 관심이 있을 수 있습니다.

git symbolic-ref HEAD

특히, 필요한 사항과 레이아웃에 따라 원하는 작업을 수행할 수 있습니다.

basename $(git symbolic-ref HEAD)

또는

git symbolic-ref HEAD | cut -d/ -f3-

그리고 또다시 그것이 있습니다..git/HEAD당신도 관심을 가질 수 있는 파일.

제가 알기로는 Git의 현재 브랜치만 기본적으로 보여줄 수 있는 방법이 없어서 다음을 사용해 왔습니다.

git branch | grep '*'

이것은 신속해야 하며 Python API와 함께 사용할 수 있습니다.

git branch --contains HEAD
* master

이는 더 짧지는 않지만 분리된 분기도 다룹니다.

git branch | awk -v FS=' ' '/\*/{print $NF}' | sed 's|[()]||g'

사용 중

/etc/bash_completion.d/git

Git와 함께 제공되며 분기 이름 및 인수 완료와 함께 프롬프트를 제공합니다.

별칭을 좋아하는 사용자:.zshrc에 다음을 추가하면 git 명령 흐름이 더 쉬워집니다.

alias gpsu="git push --set-push origin $(git symbolic-ref --short HEAD)"

누군가 이것을 git show-branch찾을 수도 있습니다. --current) 도움이 됩니다.현재 분기가 * 표시로 표시됩니다.

host-78-65-229-191:idp-mobileid user-1$ git show-branch --current
! [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
 * [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master
--
+  [CICD-1283-pipeline-in-shared-libraries] feat(CICD-1283): Use latest version of custom release plugin.
+  [CICD-1283-pipeline-in-shared-libraries^] feat(CICD-1283): Used the renamed AWS pipeline.
+  [CICD-1283-pipeline-in-shared-libraries~2] feat(CICD-1283): Point to feature branches of shared libraries.
-- [master] Merge pull request #12 in CORES/idp-mobileid from feature/fix-schema-name to master

완성도를 위해서,echo $(__git_ps1)적어도 Linux에서는 괄호로 둘러싸인 현재 분기의 이름을 제공해야 합니다.

특히 현재 분기를 표시하도록 Bash 명령 프롬프트를 설정하는 데 Git 명령이 아니기 때문에 일부 시나리오에서 유용할 수 있습니다.

예:

/mnt/c/git/ConsoleApp1 (test-branch)> echo $(__git_ps1)
(test-branch)
/mnt/c/git/ConsoleApp1 (test-branch)> git checkout master
Switched to branch 'master'
/mnt/c/git/ConsoleApp1 (master)> echo $(__git_ps1)
(master)
/mnt/c/git/ConsoleApp1 (master)> cd ..
/mnt/c/git> echo $(__git_ps1)

/mnt/c/git>

언급URL : https://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git

반응형