Git Shortcuts

Being a power user I cannot stress more on the fact that I need to be super-productive when it comes to programming and development. Completely eradicating my mouse is one of the best things I did as it boosted my productivity more than twice. I thought it would be a nice gesture to share the details with the world of developers.

Contents

  1. Basics
  2. Shortcuts
    1. Basic Status & Adding
    2. Adding & Committing
    3. Diff Tool
    4. Resetting Commits
    5. Logs & Graphs
    6. Checking out & Branching
    7. Pulling & Rebasing
    8. Git SVN Commands
    9. Alias Master Command
    10. All in One

Basics

While creating a shortcut, we will be using the command alias. To create an alias in git, the complete syntax is:

git config [--global] alias.alias_name git_command  

For example, if you want to cut short git status to git s, you need to issue this command:

git config --global alias.s status  

Note that in the above command, there's a flag --global, which makes the shortcut available for all the git repositories. If that's not given, the shortcut will be applied only to the current repository and it will be stored in .gitconfig and not anywhere else.

Bonus: Wanna know where all git stores its configuration? have a look at Best way to import or export the Git Configuration. I have included a simple demonstration of where all git would store the configuration and consolidate them and uses it in your repository. It's kinda like CSS - cascaded rules.

Well, instead of using the above command git config, I would strongly recommend developers to use the other command:

git config --edit --global  

This will open up a text editor and you can safely put stuff inside. When you open with or without the --global keyword, you will be shown a configuration file. You have to place all the aliases under the [alias] section with a tab or four space front of each shortcut.

Shortcuts

I have grouped the shortcuts into sub-sections, where you can find all the related shortcuts grouped by its similarity in functions.

  1. Basic Status & Adding
  2. Adding & Committing
  3. Diff Tool
  4. Resetting Commits
  5. Logs & Graphs
  6. Checking out & Branching
  7. Pulling & Rebasing
  8. Git SVN Commands
  9. Alias Master Command
  10. All in One

Copy only those you need.

Basic Status & Adding

The following commands are the basic ones that are used often. They are used to check the status of the current working directory, adding all the files, displaying the status, etc.

[alias]
    s    = status
    a    = !git add . && git status
    au   = !git add -u . && git status
    aa   = !git add . && git add -u . && git status

Adding & Committing

These commands are used for adding the unstaged files, staging them, and then committing them too. The acm shortcut is a complete one, which will also allow you to add a commit message on the line and complete the commit. Please use the ca or --amend flag along with commit with caution. It's really dangerous when you use it during merging or when pulling and before committing at least once.

[alias]
    ac   = !git add . && git commit
    acm  = !git add . && git commit -m
    c    = commit
    ca   = commit --amend # Be careful with this one.
    cm   = commit -m

Diff Tool

These commands are used to see what has changed. The --cached flag is used when you have already staged the changes for commit and you haven't committed yet.

[alias]
    d    = diff
    dc   = diff --cached

Resetting Commits

Use with caution. These are the commands that remove your current changes that aren't committed. This doesn't mean the unsaved changes, but also those that are saved will be lost. Again, use with caution.

[alias]
    rh   = reset HEAD
    rha  = reset --hard
    rhh  = reset --hard HEAD

Logs & Graphs

If you wanna see the commit log, here they are. Some are used for displaying pretty graphs, which I had discussed in Git Standards followed in our way of Spotify Agile Methodology.

[alias]
    l    = log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
    lg   = log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    ll   = log --stat --abbrev-commit
    llg  = log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit

Checking out & Branching

These commands are used for checking out to a new branch, checking out to another branch, creating branch from an existing branch, deleting branch, etc.

[alias]
    co   = checkout
    com  = checkout master
    cob  = checkout -b
    b    = branch
    bd   = branch -D

Examples

git co development  
git com  
git cob new-feature-branch  
git b  
git bd old-feature-branch  

Pulling & Rebasing

The following commands are for pushing, pulling, pulling and rebasing.

[alias]
    p    = pull
    po   = pull origin
    pom  = pull origin master
    pr   = pull --rebase
    ps   = push
    pu   = push -u
    puo  = push -u origin

Git SVN Commands

These commands are used in conjunction with SVN.

[alias]
    spl = svn rebase
    sps = svn dcommit

Alias Master Command

This command is kinda the "master" / "boss" command that shows all the aliases that you have currently have. This is like the meta-alias! 😜

[alias]
    alias = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\\t => \\2/' | sort

Usage: git alias.

All in One

The below one contains all the above shortcuts in a single [alias] section. Use really with caution:

[alias]
    s    = status
    a    = !git add . && git status
    au   = !git add -u . && git status
    aa   = !git add . && git add -u . && git status

    ac   = !git add . && git commit
    acm  = !git add . && git commit -m
    c    = commit
    ca   = commit --amend # Be careful with this one.
    cm   = commit -m

    d    = diff
    dc   = diff --cached

    rh   = reset HEAD
    rha  = reset --hard
    rhh  = reset --hard HEAD

    l    = log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'
    lg   = log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
    ll   = log --stat --abbrev-commit
    llg  = log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commit

    co   = checkout
    com  = checkout master
    cob  = checkout -b
    b    = branch
    bd   = branch -D

    p    = pull
    po   = pull origin
    pom  = pull origin master
    pr   = pull --rebase
    ps   = push
    pu   = push -u
    puo  = push -u origin

    spl = svn rebase
    sps = svn dcommit

    alias = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\\t => \\2/' | sort


comments powered by Disqus