J.Floyd
J.Floyd

Reputation: 41

git alias for recent checkout branches

I recently found this command to get a list of most recent 10 checked out branches.

git reflog | egrep -io 'moving from ([^[:space:]]+)' | awk '{ print $3 }' | awk ' !x[$0]++' | head -n10

I wanted to create an alias for this call "git recent", but when I try to run the config command it throws an error. "event not found"

git config --global alias.recent 'reflog | egrep -io 'moving from ([^[:space:]]+)' | awk '{ print $3 }' | awk ' !x[$0]++' | head -n10'

Is there anyway to get this complicated command as a alias. If anyone knows how to it up as a alias that excepts a number parameter would be greatly appreciated as well. The -n10 at the end of the command states how many branches to return.

Upvotes: 2

Views: 713

Answers (3)

ryenus
ryenus

Reputation: 17381

Though a bit different from what you asked for, if might make more sense to find out branches that have been changed recently.

In this case git branch actually has a --sort=<key> option, which can print the branches sorted by the given key, e.g.:

git branch --sort=-committerdate -v

This will sort the branches by committerdate, but reversed as specified by the prefix - as in -committerdate. And also print the latest commit, to setup an alias, such as br, run this command:

git config --global alias.br "branch --sort=-committerdate -v"

See git-branch manual.

Upvotes: 1

J.Floyd
J.Floyd

Reputation: 41

This is for anyone happens upon this question and this command interests them. I determined a way to this.

For one I gave up trying to do this using the git config command. I instead opened the git config file itself and added alias that way. Adding git aliases

Second I determined my script was correct I just had to add it as a function. my_alias = "!f() { 〈your complex command〉 }; f" How to add Advanced alias template in git

Here is an example of my alias.

[alias]
    recent = "!f() { git reflog | egrep -io 'moving from ([^[:space:]]+)' | awk '{ print $3 }' | awk ' !x[$0]++' | head -n${1-10}; }; f"

Upvotes: 2

AnoE
AnoE

Reputation: 8345

"event not found" comes from your bash which interprets the ! in the command line ... you mixed up your single quotes in the second command. It should be clear how to fix this; say if you have trouble figuring it out...

Double quotes also don't work:

> echo '!'
!
> echo "!"
bash: !: event not found

Hint: you can make the outer quotes ' and then use " inside, this will escape the complete command including the ! from bash.

I don't have time now to make you that statement, but I'm sure you'll figure it out in no time... ;)

Upvotes: 0

Related Questions