1

git reflog |grep 'checkout: moving'|cut -d\ -f 8 shows me what I am after but I always heard using plumbing for scripting is better than parsing the output of porcelain commands. What would be the plumbing equivalent here?

chx
  • 11,270
  • 7
  • 55
  • 129
  • Not a direct answer but might be useful : you can refer to the *Nth* last checked out branch with `@{-N}`, see [here](https://mirrors.edge.kernel.org/pub/software/scm/git/docs/gitrevisions.html). – Romain Valeri Feb 11 '20 at 16:50
  • Yes, this is discussed at https://stackoverflow.com/q/7206801/308851 – chx Feb 11 '20 at 16:51

1 Answers1

1

You could skip reflog's formatting entirely, just

cut -f2- .git/logs/HEAD | awk '$1=="checkout:" { print $NF }'

but I think nobody's bothered making officially-scriptable interfaces for the reflog, branch names are local, arbitrary and ephemeral already, the reflogs for what's been checked out even more so.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Unlike git reflog which can be run anywhere in the working tree, the command above needs to be run on the top. But it's easy to fix: `cut -f2- $(git rev-parse --git-dir)/logs/HEAD |awk '$1=="checkout:" { print $NF }'` works well and has been added as `ch` to my git toolkit which includes a `git reset --hard` safety net most importantly and makes push sane. https://gist.github.com/chx/3a694c2a077451e3d446f85546bb9278 – chx Feb 19 '20 at 02:33