sixtyfootersdude
sixtyfootersdude

Reputation: 27241

Possible to create aliases in GIT?

In bash I can create an alias like this:

alias ls="ls -h"

This changes the command ls -l from running like this:

drwxr-xr-x    1 joe bob        0 Mar 25 12:06 4.7
drwxr-xr-x    1 joe bob     4096 Mar 25 14:58 Adobe Flash Builder 4.7 Installer
-rw-r--r--    1 joe bob       92 Mar 20 12:41 Automation-Timer

To running like this:

drwxr-xr-x    1 joe bob        0 Mar 25 12:06 4.7
drwxr-xr-x    1 joe bob     4.0k Mar 25 14:58 Adobe Flash Builder 4.7 Installer
-rw-r--r--    1 joe bob       92 Mar 20 12:41 Automation-Timer

Note how the units are shown in the second example.


Is it possible to do this with git commands?

Whenever I interactively run a git log, I would like it to be aliased to git log --relative-date. Is this possible?

I know that I can create a new git command to do this however, I am not especially interested in doing that.

Upvotes: 2

Views: 180

Answers (3)

eddiemoya
eddiemoya

Reputation: 7488

If I understand you correctly, I think you want to have git log always use --relative-date without having to pass the flag - and you dont want to have to use a command other than git log itself.

The answer is yes, and this is done through the config files.

git config --global log.date 'relative'

From now on any time you call git log it will use relative dates.

To see a complete list of all the configurable options, take a look at the man page for git-config

Thanks to @Saaman for the link

Upvotes: 4

Shawn Balestracci
Shawn Balestracci

Reputation: 7540

Shell aliases cannot include spaces (on the left hand side) and you can't use git alias, but you could create a shell function instead.

git ()
{
  if [[ $1 = log ]]
  then
    shift
    /bin/git log --relative-date "$@"
  else
    /bin/git "$@"
  fi
}

Upvotes: 2

sixtyfootersdude
sixtyfootersdude

Reputation: 27241

The best workaround I have found to do this is to do something like this in the .bashrc

alias gitl='git log --relative-date'

It is then still possible to run it like this:

gitl --graph

Which would be expanded to something like this:

git log --relative-date --graph

Upvotes: 1

Related Questions