Set up aliases in your global ~/.gitconfig file.

Basic

Set up an alias.

[alias]
	lg = "log -p"

Call the lg alias.

$ git lg

That will run:

$ git log -p

Double quotes are best for aliases whether using git or shell commands. Setting up an alias as lg = 'log -p' or lg = '''log -p''' would mean it gets called incorrectly as git 'log -p'. And the error will be that log -p is “not a git command”.

Shell

Use a bang (!) at the start to invoke a shell comand.

Pipe commands

This lets you do ths followign:

[alias]
    # Not using git.
    greet = "! echo Hello"

    # Run a git command as pass to a shell command.
    tags = "! git tag -n | sort -V -r"

If you want to use double quotes inside, escape them or use a heredoc.

[alias]
    foo = "! echo \"Hello, $1!\" #"
$ git foo bar
Hello, bar!

Note that using single quotes will be literal.

[alias]
	foo = "! echo 'Hello, $1!' #"
$ git foo bar
Hello, $1!

Function

An alternative approach is to define and a shell command in one step. Note that in Bash, calling f will execute the function f().

[alias]
    foo = "! f() { echo \"Hello, $1!\" } ; f"

Arguments

You can pass arguments which are available as $@, $1, $2, etc.

[alias]
	greet = "! echo $@ #"
$ git greet I am world
I am world

First argument.

[alias]
	greet = "! echo $1 #"
$ git greet I
I

Comment

I recommend adding a comment at the end of a shell command as above, to avoid the params being used twice.

If it is not used:

[alias]
	greet = "! echo $@"
$ git greet I am world
I am world I am world

I think the reason this behavior exists is so you that git my-alias my-arg -a -b will be called as the expand the alias and still pass on on the positional and keyword params.

Multi-line

See TOML strings cheatsheet for more info.

[alias]
	undo = """! [ -z \"$(git status --porcelain)\" ] \
		&& git reset --hard HEAD~ \
		|| echo 'Unstaged changes! Stash and try again' \
	"""