2

I'd like to checkout to other branches by providing an index instead of the full branch name. I often have many branches open with long names, e.g.

issue-555-lorem-ipsum-dolor
issue-555-sit-amet-bar
issue-123-foo-bar
master

Currently, I'm just doing (with tab autocomplete):

git checkout issue-555-lorem-ipsum-dolor

Instead, I'd like to do something like (pseudo code):

git checkout [1]

Does git have anything similar right now? I had a look at the docs but didn't find anything similar. It would speed up my workflow a little bit.

Emil A.
  • 3,387
  • 4
  • 29
  • 46
  • 4
    I don't think git has an idea of the "index" of the branches, they have *names* and are shown in alphabetical order of those names. However you can move between branches by index based on *recent usage*, see e.g. https://stackoverflow.com/q/61128146/3001761 – jonrsharpe Jun 05 '20 at 09:26
  • 1
    It sounds like you want to essentially give your branches aliases, which aren't supported: https://stackoverflow.com/questions/14365946/git-branch-alias#:~:text=Git%20does%20not%20support%20aliases%20for%20branches.&text=Specifically%2C%20take%20a%20look%20at,not%20as%20histories%20of%20folders. – mohammedkhan Jun 05 '20 at 09:31

1 Answers1

0

You can create an alias for that. In example:

  # enumerate branches
  branchn = ! git branch | nl
  # checkout based on index
  checkoutn = "!f(){ git checkout $(git branch | sed \"$1q;d\"); }; f"

branchn enumerates branches, while checkoutn checkouts based on the index in enumerated list.

Example usage:

git checkoutn 2
maciek97x
  • 2,251
  • 2
  • 10
  • 21