How can I display the current branch and folder path in terminal?

前端 未结 13 1132
无人共我
无人共我 2020-12-04 07:09

I\'ve been watching some of the Team Treehouse videos and they have a very nice looking terminal when working with Git.

For example they have (something similar):

13条回答
  •  感情败类
    2020-12-04 07:46

    In 2019, I think git branch --show-current is a better command than the accepted answer.

    $ git branch --show-current
    master
    

    (Added in git 2.22 release in June 2019)

    It runs much faster as it doesn't need to iterate through all branches. Similarly git branch should be avoided too in the command prompt as it slows down your prompt if you have many local branches.

    Put it in a function to use anywhere on command prompt:

      # This function returns '' in all below cases:
      #   - git not installed or command not found
      #   - not in a git repo
      #   - in a git repo but not on a branch (HEAD detached)
      get_git_current_branch() {
        git branch --show-current 2> /dev/null
      }
    

    More context:

    $ git version
    git version 2.23.0
    

提交回复
热议问题