git: a quick command to go to root of the working tree

前端 未结 7 1588
悲&欢浪女
悲&欢浪女 2020-12-24 01:21

I wanted a simple git command to go up to the \"root\" of the repository.

I started with a script, but figured that I cannot change active directory of the shell, I

相关标签:
7条回答
  • 2020-12-24 01:54

    Peter's answer above works great if you're in a subdirectory of the git root. If you're already in the git root, it'll throw you back to $HOME. To prevent this, we can use some bash conditionals.

    if [ "`git rev-parse --show-cdup`" != "" ]; then cd `git rev-parse --show-cdup`; fi
    

    so the alias becomes:

    alias git-root='if [ "`git rev-parse --show-cdup`" != "" ]; then cd `git rev-parse --show-cdup`; fi'
    
    0 讨论(0)
提交回复
热议问题