2

Lets say I have 3 branches in my repository: main, develop and feature. Now, suppose I have switched my working branch in the following order: from main, to develop, to feature, back to develop, and to feature again.

From this position I would like to go back to my main branch without having to write main so that in theory I can forget the name of my branch. The switch command can come a long way with the @{-N} notation to refer to the N-th last branch/commit, like so:

  • git switch - will take me to branch develop (same as git switch @{-1})
  • git switch @{-1} will take me to branch develop
  • git switch @{-2} will take me to branch feature
  • git switch @{-3} will take me to branch develop
  • git switch @{-4} will take me to branch main

As you can see, this lists all your previous working branches. However, since I will be switching between develop and feature multiple times before wanting to go back to main, I would have to remember the precise amount of times I have switched between develop and feature since I left main. I would like to be able to refer to a previous unique branch, such that something like git switch @{-2} --unique would take me to main, but that option does not exist for the git switch command at least.

I have found a handy trick to list your most recently-used branches using Git, and that will list your branches uniquely. Maybe it is possible to take that idea to create a way to switch to the Nth previous unique branch?

knittl
  • 246,190
  • 53
  • 318
  • 364
Robin Bastiaan
  • 572
  • 2
  • 8
  • 21
  • 1
    Not sure what you want to accomplish.... `git checkout -` can help you switch between `develop` and `feature` without having to specify anything about them (just `git checkout -`..... something I learned recently, actually). So.... if you want to then get out of this never-ending cycle between the 2 branches: `git checkout main`. – eftshift0 Oct 05 '22 at 09:09
  • 1
    If you want to get back to main, why not simply `git switch main`? Remembering `main` sounds a lot easier than keeping track of how many unique branches were visited since main was last checked out. – knittl Oct 05 '22 at 09:14
  • @eftshift0 Of course I can use `git checkout main`, but I was looking for a way to not be required to type the name of that branch. – Robin Bastiaan Oct 05 '22 at 09:15
  • `git switch m` – knittl Oct 05 '22 at 09:17

1 Answers1

1

The simplest is still to use git switch main. But if you really want to …

Should be simple enough (although will start to fail if output of git reflog changes – it's not intended to be parsed)

Get unique branch names from reflog:

git reflog | awk '/checkout: moving from/ && !seen[$NF]++ {print $NF}'

Select n'th entry

git reflog | awk … | sed -n "${n}p"

or

git reflog | awk … | awk "NR==${n}"

Make it an alias (sn – "switch n")

git config --global alias.sn '!f() {
  branch="$(git reflog | awk '\''/checkout: moving from/ && !seen[$NF]++ {print $NF}'\'' | awk "NR==${1:-1}")";
  git switch "$branch";
}; f'

(n will default to 1 – the most recent branch)

Use it

git sn 5 # go to 5th last branch
git sn   # go to previous branch
knittl
  • 246,190
  • 53
  • 318
  • 364