tuk*_*irs 6 git terminal command
如何从命令行/终端获取主分支名称?
\n\n我知道默认情况下会调用主分支master,但是可以将其重命名为他们想要的任何名称。
PS\xe2\x80\x94如果能得到本地和远程主分支的名称就好了。
\n\n编辑:我所说的main branch其他人可能会称之为default branch或stable branch。它\xe2\x80\x99是你(应该)将所有内容(稳定/工作)合并到其中的一个。
我对 没有深入的了解git,但是,在 中git,通常有{remote}/HEAD,例如origin/HEAD。Here\xe2\x80\x99s的手册页git remote摘录:
set-head\n\n Sets or deletes the default branch (i.e. the target of the\n symbolic-ref refs/remotes/<name>/HEAD) for the named remote.\n Having a default branch for a remote is not required, but allows\n the name of the remote to be specified in lieu of a specific\n branch. For example, if the default branch for origin is set to\n master, then origin may be specified wherever you would normally\n specify origin/master.\nRun Code Online (Sandbox Code Playgroud)\n由此我了解到{remote}/HEAD是{remote}. 人们可以使用它来获取分支的名称(有人知道更好的/管道命令吗?):
git branch -r | grep -Po \'HEAD -> \\K.*$\'\norigin/master\nRun Code Online (Sandbox Code Playgroud)\n当一个人想要获取本地主/默认分支时,通常没有HEAD分支,但是通常有\xe2\x80\x99s 是唯一一个跟踪 的分支{remote}/HEAD,我们可以使用该名称(同样,肯定有一个更好的命令):
git branch -vv | grep -Po "^[\\s\\*]*\\K[^\\s]*(?=.*$(git branch -rl \'*/HEAD\' | grep -o \'[^ ]\\+$\'))"\nmaster\nRun Code Online (Sandbox Code Playgroud)\n当然,set-head如果尚未设置,则需要HEAD在git remote -r. 因此,我们需要在使用上述命令之前运行一次以下命令(感谢@pixelbrackets 指出这一点)。
git remote set-head origin -a\nRun Code Online (Sandbox Code Playgroud)\n更新:
\n最近,我想获得更多信息(我从这里得到的一些命令\xe2\x80\x99 ):
\n# Get currently checked out local branch name\ngit rev-parse --abbrev-ref HEAD\n# Output: branch\n\n# Get remote branch name that is tracked by currently checked out local branch\ngit for-each-ref --format=\'%(upstream:short)\' "$(git symbolic-ref -q HEAD)"\n# Output: origin/branch\n\n# Get local main branch name\ngit branch -vv | grep -Po \\\n "^[\\s\\*]*\\K[^\\s]*(?=.*$(git branch -rl \'*/HEAD\' | grep -o \'[^ ]\\+$\'))"\n# Output: master\n\n# Get remote branch name that is tracked by local main branch (AKA the remote HEAD branch)\ngit branch -r | grep -Po \'HEAD -> \\K.*$\'\n# Output: origin/master\nRun Code Online (Sandbox Code Playgroud)\n
如果您假设主分支将被称为master或main,那么我将执行以下操作来快速检测本地存储库中的主分支名称:
git_main_branch () {
git branch | cut -c 3- | grep -E '^master$|^main$'
}
Run Code Online (Sandbox Code Playgroud)
然后我需要知道主分支名称的其他命令可以使用它。例如,我有gc-m代表“git checkout mainbranch”的:
alias gc='git checkout '
alias gc-m='gc $(git_main_branch)'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7026 次 |
| 最近记录: |