More aliases

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.

Getting ready

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

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.

  • Show the current branch only:
    $ git config alias.b "rev-parse --abbrev-ref HEAD"
    $ git b
    aliases
    
  • Show a compact graph history view with colors:
    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
    
    How to do it...
  • When resolving a conflicted merge, get a list of the conflicted/unmerged files:
    $ 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
    
  • Shorthand status as follows:
    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
    
  • A shorter status with branch and file information:
    $ 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
    
  • Show the latest commit with some stats:
    $ 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(-)
    
  • This gives the same view as the previous but for the five latest commits (the output is not shown):
    $ git config alias.l5 "log -5 --decorate --shortstat"
    
  • A commit listing with statistics on the changed files in colors can be displayed using the following command:
    $ 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
    
    How to do it...
  • Show the upstream/tracking branch:
    $ git config alias.upstream "rev-parse --symbolic-full-name --abbrev-ref=strict HEAD@{u}"
    $ git upstream
    origin/aliases
    
  • Show details of ID/SHA-1 (commit, tag, tree, blob):
    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
    
  • Show the numbers of "cd-ups", ../, 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
    
  • The path of the repository on the filesystem:
    $ git config alias.path "rev-parse --show-toplevel"
    $ git path
    /path/to/cookbook-tips-tricks
    
  • Abandon whatever changes we have in the index, working tree, and possibly also the commits and reset the working tree to a known state (commit ID). Do not touch the untracked files. We need a ref as an argument for the 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