How to get the current branch name in Git?

后端 未结 30 3135
清酒与你
清酒与你 2020-11-22 07:47

I\'m from a Subversion background and, when I had a branch, I knew what I was working on with \"These working files point to this branch\".

But with Git I\'m not sur

30条回答
  •  时光取名叫无心
    2020-11-22 08:20

    I have a simple script called git-cbr (current branch) which prints out the current branch name.

    #!/bin/bash
    
    git branch | grep -e "^*"
    

    I put this script in a custom folder (~/.bin). The folder is in $PATH.

    So now when I'm in a git repo, I just simply type git cbr to print out the current branch name.

    $ git cbr
    * master
    

    This works because the git command takes its first argument and tries to run a script that goes by the name of git-arg1. For instance, git branch tries to run a script called git-branch, etc.

提交回复
热议问题