DEV Community

Talha Aamir
Talha Aamir

Posted on

destroying git push --set-upstream origin with vim

I hate that everytime I need to push a new branch to my origin I need to go through the painful process of
git push --set-upstream origin ${branch-name}.

The problem is worse because I need to type and tab complete the branch name too, just too much hassle.

Now with vim I had the thought to one day just make a remap, which I did, and what that did was basically just type out the statement

:Git push --set-upstream origin and allowed me to just type the branch name.

I think today even that was too much repetition for me, so I had the idea to see how more could I reduce this. I mean I wanted to learn vimscript anyhow, so was a nice thing to do.

The Vim Magic

" For new branch
" gets branch name
" sets upstream origin automatically
function! GitPushUpsOrgBranch()
  let branch = system("git branch --show-current")
  let push_str = "git push --set-upstream origin ".branch
  let conf_msg = "Do you want to execute\n".push_str
  let conf_res = ConfirmBox(conf_msg)
  if conf_res == 1
    let push_str_out = system(push_str)
    echo push_str_out
  endif
  return 1
endfunction

" git push set upstream remap
nnoremap<silent> <Leader>gpsu :call GitPushUpsOrgBranch()<CR>

Enter fullscreen mode Exit fullscreen mode

What this beautiful set of commands does is that upon hitting gpsu it makes vim call the function GitPushUpsOrgsBranch().

GitPushUpsOrgsBranch

This does 3 things:

  1. Gets my current branch which git 6.6+ allows (I think, don't remember) via git branch --show-current
  2. Appends it to a string and forms the actual bland repetitive command I so hate git push --set-upstream origin ${branch_name}
  3. Before pushing it actually prompts me and then allows me to push. (see image below)

Git destroys set upstream

Conclusion

This was a comfortable way to get into vimscript, but also was a nice personalization I deeply enjoyed. Pretty cool stuff.

Discussion (0)