How to do it...

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.

  1. Let's begin with an alias to show the current branch only:
$ git config alias.b "rev-parse --abbrev-ref HEAD"
$ git b
aliases
  1. To show a compact graph history view with colors, the following alias will save you many keystrokes:
$ 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

The following screenshot shows you a typical output, where commits are colored red, committers are colored blue, and so on:

  1. When resolving a conflicted merge, it can be useful to get a list of the conflicted/unmerged files:
$ git config alias.unmerged '!git ls-files --unmerged | cut -f2 | sort -u'

  1. We can see the previous alias 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.
  
  1. First, check the output of git status:
$ 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")
  1. We see the unmerged path mentioned in the output. Let's use the unmerged alias to get a simple list of unmerged files:
$ git unmerged
spaceship.txt
  1. You can abort the merge as follows:
$ git merge --abort
  1. During a work day, you will type git status many times. Adding a shorthand status can be helpful:
$ 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
  1. An even shorter status with branch and file information can be defined as follows:
$ git config alias.s 'status -sb'

  1. To try it out, first modify foo and create an untracked test file:
$ touch test
$ echo testing >> foo
  1. Next, try your new s alias:
$ git s
## aliases...origin/aliases
M foo
?? test 
  1. Often, you'll just want to show the latest commit with some stats:
$ git config alias.l1 "log -1 --shortstat"
$ git l1
  commit a43eaa9b461e811eeb0f18cce67e4465888da333
  Author: John Doe <[email protected]>
  Date:   Wed May 14 22:46:32 2014 +0200
    
    Better spaceship design
    
 1 file changed, 9 insertions(+), 9 deletions(-)
  1. But sometimes, you need a bit more context. The following alias is the same  as the previous but for the five latest commits (the output is not shown):
$ git config alias.l5 "log -5 --decorate --shortstat"
  1. A commit listing with statistics on the changed files in color can be displayed using the following alias:
$ 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

As the next screenshot shows, committers are colored blue, their age in green, and so on:

  1. If you work in many repositories, remember that the upstream/tracking branch can be difficult. The following alias is shorthand for showing this:
$ git config alias.upstream "rev-parse --symbolic-full-name --abbrev-ref=strict HEAD@{u}"
$ git upstream
origin/aliases 
  1. You can show the details of ID/SHA-1 (commit, tag, tree, blob) with the details alias. Not that you save many keystrokes, but details is easier to remember:
$ git config alias.details "cat-file -p"
$ git details HEAD
tree bdfdaacbb29934b239db814e599342159c4390dd
parent 8fc1819f157f2c3c25eb973c2a2a412ef3d5517a
author John Doe <[email protected]> 1400100392 +0200
committer John Doe <[email protected]> 1400100392 +0200
    
Better spaceship design
  1. A repository will grow, and the directory tree will become large. You can show the number of cd-ups and, ../, needed to go to the repository root using the following alias, which can be useful in shell scripts:
$ 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
  1. The path of the repository on the filesystem can easily be viewed with the following alias:
$ git config alias.path "rev-parse --show-toplevel"
$ git path
/path/to/cookbook-tips-tricks
  1. If we need to abandon whatever changes we have in the index, working tree, and also possibly the commits, and reset the working tree to a known state (commit ID) but we don't want to touch the untracked files, all we need is a ref to a state of the repository to be restored, for example, 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.25.74