Right now I'm using fugitive to manage almost everything git related, but I can't find a good workflow to quickly visualize and switch between available branches.

I can do :Git checkout <branch-name>, the problem is that it autocompletes both file names and branches, and I'd prefer a branch list.

up vote 11 down vote accepted

To culminate the answers here are the ways to accomplish this:

Vanilla Terminal Vim

Press CTRL-Z in normal mode will drop you back in to the shell and suspend Vim. After executing your git checkout [branchname] type the fg command to return to the suspended Vim which will also force Vim to refresh.

Vanilla GUI Vim

Some gvim implementations will have :shell support which will open a shell inside of Vim using a dumb terminal. However it may be easier to just type:

:!git checkout [branchname]

This will execute the command without having to open a new shell prompt. (This option is also usable in the terminal Vim).

vim-fugitive

vim-fugitive is a plugin that allows you to use Git from within Vim. The command would be:

:Git checkout [branchname]

vim-merginal

vim-merginal is a plugin that uses vim-fugitive and provides a user friendly-ish interface.

  1. Type :Merginal.
  2. Move cursor to the branch you want.
  3. Type cc to check out that branch.
  • 1
    It's fugitive, not fugative, and that's what OP is already using. – muru Dec 23 '15 at 20:07
  • 1
    Awesome answer. vim-merginal is what I was looking for. – Sergio D. Márquez Dec 23 '15 at 20:09
  • I like the ctrl-z because it auto loads the files. Co-workers like to have a tmux window for the command line and one for vim but when they do a checkout or merge when they return to vim they have to go to each open file and :e! each time. (:bufdo e) which is a pain to remember. Instead it is easier to ctrl-z then when fg happens Vim performs the :e auto-magically. – Sukima Nov 25 '16 at 17:25
  • I Another nice fugitive extension you could add to the list: github.com/sodapopcan/vim-twiggy – Sergio D. Márquez Mar 16 at 19:00
  • In case the git checkout is already done, :checktime comes in handy to reload all opened buffers. – LEI Apr 19 at 22:02

Use the vim-merginal plugin (fugitive extension). It offers interactive TUI for:

  • Viewing the list of branches
  • Checking out branches from that list
  • Creating new branches
  • Deleting branches
  • Merging branches
  • Rebasing branches
  • Solving merge conflicts
  • Interacting with remotes(pulling, pushing, fetching, tracking)
  • Diffing against other branches
  • Renaming branches
  • Viewing git history for branches

enter image description here

The fugitive Git checkout <branch> has a downside that it doesn't autocomplete the branch name. Using fzf.vim I've created this command:

function! s:changebranch(branch) 
    execute 'Git checkout' . a:branch
    call feedkeys("i")
endfunction

command! -bang Gbranch call fzf#run({
            \ 'source': 'git branch -a --no-color | grep -v "^\* " ', 
            \ 'sink': function('s:changebranch')
            \ })

hope you find it useful

You should embrace your terminal. If you use CTRL-Z, it will background Vim (or whichever process you are currently running), then you can run any commands you want, the fg to bring the process back to the foreground:

<CTRL-Z>
git checkout <tab>
fg
  • 2
    ... and when using GVim? – muru Dec 23 '15 at 5:30
  • 2
    :shell is the way to go or alt tabbing in to your other favorite terminal emulator – 65-e9-7e-17 Dec 23 '15 at 5:32
  • 1
    I always like !git checkout [branchname] – bronzehedwick Dec 23 '15 at 18:46

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.