So I have been getting very acquainted with Neovim/Spacevim lately, and it's freakin awesome!
I am in the process of setting up some personal keybindings and commands and what have you, and am currently working on Git stuff.
Coming from using VSCode and iTerm2, when wanting to switch from any branch to master or main or whatever the main branch is, I have an alias as follows:
alias gcom=git checkout $(git_main_branch)
Where I can just type gcom into the terminal, and it will automatically switch to whatever the main branch is. Unfortunately in Neovim, git_main_branch is not a thing, so I am trying to figure out an equivalent, where I can do something like type :Gcom into the Neovim command prompt, and it switches to the main branch.
I tried to set up a function like this in my init.vim file (I have coc and all corresponding Git plugins installed, including fugitive):
function! GitCheckoutMain()
let gitMainBranch = system('git_main_branch')
execute "normal! :Git checkout" . gitMainBranch
endfunction
and then setting up a command like
command! Gcom :call GitCheckoutMain()
But that does not do the trick. Does anyone know how I might accomplish this with Neovim? Thanks!
Ok so I think I actually may have figured out how to solve this. I changed my
GitCheckoutMainfunction from my initial post to be as follows:and I bound that to my
Gcomcommand from above, and when I run:Gcomfrom the Neovim terminal, it prints outmasterin the case of a repo wheremasteris the main branch. Will also test this in a repo where the main branch ismain, but I think this will do what I want...EDIT: This does in fact work! Here is my full code, for anyone interested.
and whichever branch I am on, if I type in
:Gcomto the Neovim terminal, it will change branches tomasterormainor whatever main branch is set up in that particular repository. Woohoo!I want to credit the answer posted by @dwelle in this thread - How to get the default for the master branch in Git? - for getting me to the end result!