简体   繁体   English

在 Neovim 中检索 git_main_branch

[英]Retrieve git_main_branch in Neovim

So I have been getting very acquainted with Neovim/Spacevim lately, and it's freakin awesome!所以我最近对 Neovim/Spacevim 非常熟悉,这真是太棒了!

I am in the process of setting up some personal keybindings and commands and what have you, and am currently working on Git stuff.我正在设置一些个人键绑定和命令以及你有什么,目前正在研究 Git 的东西。

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:来自使用 VSCode 和 iTerm2,当想要从任何分支切换到mastermain或任何分支时,我有一个别名,如下所示:

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.我可以在终端中输入gcom ,它会自动切换到分支。 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.不幸的是,在 Neovim 中, git_main_branch不是一个东西,所以我试图找出一个等价物,在那里我可以在 Neovim 命令提示符中输入:Gcom之类的东西,然后它会切换到主分支。

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 ):我试图在我的 init.vim 文件中设置一个像这样的init.vim文件(我已经安装了coc和所有相应的 Git 插件,包括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?有谁知道我如何用 Neovim 完成这个任务? Thanks!谢谢!

Ok so I think I actually may have figured out how to solve this.好的,所以我想我实际上可能已经想出了如何解决这个问题。 I changed my GitCheckoutMain function from my initial post to be as follows:我将我的GitCheckoutMain function 从最初的帖子更改为如下:

function GitCheckoutMain()
  let mainGitBranch = system('git branch | grep -o -m1 "\b\(master\|main\)\b"')

  echo mainGitBranch
endfunction

and I bound that to my Gcom command from above, and when I run :Gcom from the Neovim terminal, it prints out master in the case of a repo where master is the main branch.我从上面将它绑定到我的Gcom命令,当我从 Neovim 终端运行:Gcom时,它会在master分支的 repo 的情况下打印出master Will also test this in a repo where the main branch is main , but I think this will do what I want...还将在主分支为main的 repo 中对此进行测试,但我认为这会做我想要的......

EDIT : This does in fact work, Here is my full code.编辑:这确实有效,这是我的完整代码。 for anyone interested.对于任何有兴趣的人。

function GitCheckoutMain()
  let mainGitBranch = system('git branch | grep -o -m1 "\b\(master\|main\)\b"')

  execute "normal! :Git checkout " . mainGitBranch
endfunction

...

command! Gcom :call GitCheckoutMain()

and whichever branch I am on, if I type in :Gcom to the Neovim terminal, it will change branches to master or main or whatever main branch is set up in that particular repository.无论我在哪个分支上,如果我在 Neovim 终端中输入:Gcom ,它都会将分支更改为mastermain或在该特定存储库中设置的任何分支。 Woohoo!呜呼!

I want to credit the answer posted by @dwelle in this thread - How to get the default for the master branch in Git?我想感谢@dwelle 在此线程中发布的答案 - 如何在 Git 中获取主分支的默认值? - for getting me to the end result! - 让我得到最终结果!

That's a nifty solution.这是一个很好的解决方案。 Just wanted to add my own 2 cents about this, If you use GitHub as your repository, and you have the GitHub CLI installed, you could get the main target branch by typing只是想为此添加我自己的 2 美分,如果您使用 GitHub 作为存储库,并且安装了 GitHub CLI,则可以通过键入获取主目标分支

gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name'

That will open up another mode which will list the name of the main branch.这将打开另一种模式,该模式将列出主分支的名称。 I know you already got to where you needed, but this might be another cleaner method you could implement.我知道你已经到了你需要的地方,但这可能是你可以实施的另一种更清洁的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM