In Chapter 2, Configuration, we saw how we can create aliases and a few examples of them. In this example, we will see some more examples of the useful aliases.
Clone the cookbook-tips-tricks
repository and checkout the aliases
branch:
$ git clone https://github.com/dvaske/cookbook-tips-tricks.git $ cd cookbook-tips-tricks $ git checkout aliases
Here, we'll see some examples of aliases with a short description of each of them and an example of how to use them. The aliases are just made for the local repository; use --global
to make them available for all the repositories.
$ git config alias.b "rev-parse --abbrev-ref HEAD" $ git b aliases
git config alias.graph "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative" git graph origin/conflict aliases
$ git config alias.unmerged '!git ls-files --unmerged | cut -f2 | sort -u'
We can see the previous command in action by merging the origin/conflict
branch:
$ git merge origin/conflict Auto-merging spaceship.txt CONFLICT (content): Merge conflict in spaceship.txt Automatic merge failed; fix conflicts and then commit the result.
Check the output of git status
first:
$ git status On branch aliases Your branch is up-to-date with 'origin/aliases'. You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: spaceship.txt no changes added to commit (use "git add" and/or "git commit -a")
Let's see what the unmerged alias does:
$ git unmerged spaceship.txt
Abort the merge:
$ git merge --abort
git config alias.st "status" git st On branch aliases Your branch is up-to-date with 'origin/aliases'. nothing to commit, working directory clean
$ git config alias.s 'status -sb'
Modify foo
and create an untracked file test
:
$ touch test $ echo testing >> foo
Try the s
alias:
$ git s ## aliases...origin/aliases M foo ?? test
$ git config alias.l1 "log -1 --shortstat" $ git l1 commit a43eaa9b461e811eeb0f18cce67e4465888da333 Author: Aske Olsson <[email protected]> Date: Wed May 14 22:46:32 2014 +0200 Better spaceship design 1 file changed, 9 insertions(+), 9 deletions(-)
$ git config alias.l5 "log -5 --decorate --shortstat"
$ git config alias.ll "log --pretty=format:"%C(yellow)%h%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %Cred%d%Creset" --numstat" $ git ll -5
upstream/tracking
branch:$ git config alias.upstream "rev-parse --symbolic-full-name --abbrev-ref=strict HEAD@{u}" $ git upstream origin/aliases
git config alias.details "cat-file -p" git details HEAD tree bdfdaacbb29934b239db814e599342159c4390dd parent 8fc1819f157f2c3c25eb973c2a2a412ef3d5517a author Aske Olsson <[email protected]> 1400100392 +0200 committer Aske Olsson <[email protected]> 1400100392 +0200 Better spaceship design
../
, needed to go to the repository root using following command:$ git config alias.root "rev-parse --show-cdup" $ cd sub/directory/example $ pwd /path/to/cookbook-tips-tricks/sub/directory/example $ git root ../../../ $ cd $(git root) $ pwd /path/to/cookbook-tips-tricks
$ git config alias.path "rev-parse --show-toplevel" $ git path /path/to/cookbook-tips-tricks
HEAD
:$ git config alias.abandon "reset --hard" $ echo "new stuff" >> foo $ git add foo $ echo "other stuff" >> bar $ git s ## aliases...origin/aliases M bar M foo ?? test $ git abandon HEAD $ git s ## aliases...origin/aliases ?? test