Working somewhere where we prefix our branches with the creator’s initials, I sometimes forget to do so.1 This leads to me having to rename the branch, typing out the whole name again after adding
cd/ to the start of it.
Computers are meant to solve repetitive problems for us, so let’s put it to work in this case too. My ~/bin contains
git current-branch, which returns the current branch name.
If we hardcode the initials, this becomes a simple command to recall from our history:2
git branch --move --force cd/$(git current-branch)
But computers are supposed to solve all repetitive work, including knowing who I am, right? Correct, my local user account knows my full name, so we can work out my initials from that. Lets lean on the
id(1) command to lookup the user’s details then strip it down to just the initials.34
id -F # => "Caius Durling" id -F | sed -Ee 's/(^| )(.)[^ ]+/\2/g' | tr 'A-Z' 'a-z' # => cd
Bingo, we can wrap that into a subshell passed to the branch move command and we’re done in a one-liner.
git branch --move --force "$(id -F | sed -Ee 's/(^| )(.)[^ ]+/\2/g' | tr 'A-Z' 'a-z')/$(git current-branch)"
I don’t follow that policy for my personal repos, or working on forks of other people’s code. And I’m human, so I forget. ↩︎
You can also replace
git branch -M newname↩︎
On macOS you can use
id -Fto return the full name of the user. Doing this on other platforms is left as an exercise for the reader. ↩︎
Yes, this is an incredibly naive way to initialize a name, but it’s good enough for the people I work with. Handling edge cases is left as … you got it, an exercise for the reader. ↩︎