3

I used gcm(alias to git checkout $(git_main_branch)) to switch branches in my project, but switched to trunk branch, I expected to switch to master branch.

echo $(git_main_branch)
trunk

change git_main_branch to master rather than trunk. In my other project, git_main_branch is master as my expected

torek
  • 448,244
  • 59
  • 642
  • 775
Brady Li
  • 31
  • 3
  • 1
    `git_main_branch` is not a Git command. You must have invented it yourself, or copied it from someone else who invented it themselves. You'll need to find out where your command comes from and/or where its documentation is, in order to figure out how to control it. [Hint](https://kapeli.com/cheat_sheets/Oh-My-Zsh_Git.docset/Contents/Resources/Documents/index) – torek Apr 01 '22 at 18:38
  • I have found the [solution](https://github.com/ohmyzsh/ohmyzsh/pull/9412) in oh-my-zsh github issue.thx – Brady Li Apr 02 '22 at 02:26
  • @torek `git_main_branch` is a zsh function installed when you download oh-my-zsh – Matt C. May 15 '23 at 15:15

2 Answers2

5

You can override the function in your .zshrc like so:

function git_main_branch() {
  def=`git remote show origin | sed -n '/HEAD branch/s/.*: //p'`
  echo $def
}

refs:

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • I simply used `echo "master"` in this function – Liran H Dec 22 '22 at 21:28
  • 1
    @LiranH that works only if all of your branches "main" branch is called "master". The provided answer here will work more dynamically for branches not named "master". Also I should point out that oh-my-zsh defaults should work for branches named master anyways, so should not require this override to begin with – Matt C. May 15 '23 at 15:14
1

According to the oh-my-zsh documentation, the git_main_branch function returns main branch if it locally exists or fallbacks to master.

If there's a desire gcm, gswm aliases switch to main branch, just make sure it exists locally.

 
Command to verify whether main branch exists locally:

git show-branch main
Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53