How to get the current branch name in Git?

后端 未结 30 3143
清酒与你
清酒与你 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:33

    For my own reference (but it might be useful to others) I made an overview of most (basic command line) techniques mentioned in this thread, each applied to several use cases: HEAD is (pointing at):

    • local branch (master)
    • remote tracking branch, in sync with local branch (origin/master at same commit as master)
    • remote tracking branch, not in sync with a local branch (origin/feature-foo)
    • tag (v1.2.3)
    • submodule (run inside the submodule directory)
    • general detached head (none of the above)

    Results:

    • git branch | sed -n '/\* /s///p'
      • local branch: master
      • remote tracking branch (in sync): (detached from origin/master)
      • remote tracking branch (not in sync): (detached from origin/feature-foo)
      • tag: (detached from v1.2.3)
      • submodule: (HEAD detached at 285f294)
      • general detached head: (detached from 285f294)
    • git status | head -1
      • local branch: # On branch master
      • remote tracking branch (in sync): # HEAD detached at origin/master
      • remote tracking branch (not in sync): # HEAD detached at origin/feature-foo
      • tag: # HEAD detached at v1.2.3
      • submodule: # HEAD detached at 285f294
      • general detached head: # HEAD detached at 285f294
    • git describe --all
      • local branch: heads/master
      • remote tracking branch (in sync): heads/master (note: not remotes/origin/master)
      • remote tracking branch (not in sync): remotes/origin/feature-foo
      • tag: v1.2.3
      • submodule: remotes/origin/HEAD
      • general detached head: v1.0.6-5-g2393761
    • cat .git/HEAD:
      • local branch: ref: refs/heads/master
      • submodule: cat: .git/HEAD: Not a directory
      • all other use cases: SHA of the corresponding commit
    • git rev-parse --abbrev-ref HEAD
      • local branch: master
      • all the other use cases: HEAD
    • git symbolic-ref --short HEAD
      • local branch: master
      • all the other use cases: fatal: ref HEAD is not a symbolic ref

    (FYI this was done with git version 1.8.3.1)

提交回复
热议问题