본문 바로가기

Technical/Development

[Git Tip] Git 브랜치를 보여주는 Linux 프람프트(prompt) - Ubuntu 14.04, bash 기준


Git을 사용하는 방식은 명령형(커맨드라인; Command line; 또는 터미널 방식) 이거나 GUI Client 형(Mac, Windows)이거나 둘 중 하나일 것이다. Git의 내부 메커니즘을 알기 위해서이기도 하지만 커맨드라인 방식이 익숙해 지면 훨씬 수월해 지는 경우가 많은 듯 하다. 이 때 어쩔 수 없이 git status 를 쳐서 현재 어떤 브랜치에서 작업중인지를 수시로 확인해야 하는데, Linux 의 프람프트를 개조해서 사용하면 편리한 점이 많으므로 그 방법을 정리한다.


"Git 은 브랜치로 시작해서 브랜치로 울고 웃다가 브랜치로 끝난다" - Barracuda -


* 준비물: github 에서 공개된 아래의 스크립트를 계정의 Home에 내려 받아 둔다

* git-prompt.sh 는 Bash, Zsh 를 지원한다

bryan@bryan-XenPC:~$ curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh


* 홈 디렉토리의 .bashrc를 아래와 같이 수정하고 적용

* 기존의 프람프트에 영향, 변형이 없어야 하며, Git 으로 관리되는 작업 디렉토리에서만 프람프트가 자동으로 변경

bryan@bryan-XenPC:~$ vi .bashrc

...

# For Git prompt # <== Homedir에 .git-prompt.sh 가 있는지 확인하는 if문 추가

if [ -f ~/.git-prompt.sh ]; then 

    source ~/.git-prompt.sh 

fi


if [ "$color_prompt" = yes ]; then

    if [ -f ~/.git-prompt.sh ]; then # <== 원래의 PS1 설정을 위한 if 블록에 __git_ps1 변수 값을 끼워넣는 if 문 추가

        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$$(__git_ps1 "(Branch:%s)") '

    else

        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

    fi

else

    if [ -f ~/.git-prompt.sh ]; then # <== 위와 마찬가지

        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$$(__git_ps1 "(Branch:%s)") '

    else

        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

    fi

fi

unset color_prompt force_color_prompt

...


* 스크립트가 정상 작동하는지 디렉토리를 이동하면서 확인해 본다

~/git-test/gittest 디렉토리와 ~/PycharmProjects/PyTestProjects 는 git 으로 트래킹되는 working 디렉토리이다

bryan@bryan-XenPC:~$ source .bashrc

bryan@bryan-XenPC:~$ cd git-test/gittest/

bryan@bryan-XenPC:~/git-test/gittest$(Branch:develop) cd ../../kernel_src/

bryan@bryan-XenPC:~/kernel_src$ cd ../PycharmProjects/PyTestProjects/

bryan@bryan-XenPC:~/PycharmProjects/PyTestProjects$(Branch:master)


[관련 글]

2015/07/23 - [Technical/Development] - [Git Tip] AWS EC2 VM을 이용한 Git 서버설정과 git 기본 사용법

2015/07/24 - [Technical/Development] - [Git Tip] Git에 대한 궁금증들

2015/08/03 - [Git Tip] Git Branch와 상태를 보여주는 BASH-GIT-PROMPT



- Barracuda -