Reputation: 348
I'm building some aliases in my ~/.profile
for common command strings. One case is when I begin a new feature branch, I will checkout the current base which has a naming convention of release/1.1.0
, then make my new feature/feature-name
branch from there.
My question is how to make my bash command automatically checkout the latest version. If I type manually and tab over the results, some of the projects have many available like release/1.0.0
, release/1.1.0
, release/1.1.2
, release/1.2.0
.
What can I add to my following function that can will default to the latest available version?
relb(){
git checkout release/???
git pull
}
Upvotes: 0
Views: 199
Reputation: 52506
You can use --sort='-v:refname'
as an option to git branch
to version sort by refname,1 descending (the -
); the --format
option makes sure there is no unwanted whitespace in the output, and head -n1
returns just one branch:
git checkout \
"$(git branch \
--list \
--sort='-v:refname' \
--format='%(refname:short)' \
'release/*' \
| head -n1)"
You might have to git fetch
first.
Alternatively, you can do it without any external tools at all:1
git checkout \
"$(git for-each-ref \
--sort='-v:refname' --format='%(refname:short)' --count=1 \
refs/heads/release/)"
1Hat tip to Hauleth for pointing out that git branch
has sorting built-in and the git for-each-ref
can do it all.
Upvotes: 4