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. Assuming Unchanged
    5. Resetting Commits
    6. Logs & Graphs
    7. Checking out & Branching
    8. Pulling & Rebasing
    9. Git SVN Commands
    10. Alias Master Command
    11. 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. Assuming Unchanged
  5. Resetting Commits
  6. Logs & Graphs
  7. Checking out & Branching
  8. Pulling & Rebasing
  9. Git SVN Commands
  10. Alias Master Command
  11. 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
    uc   = !git reset --soft HEAD^ && git reset HEAD .

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

Assuming Unchanged

This is a problem I faced when I committed the file and changed it. This is because, even though you have added the file in the .gitignore, git keeps a track of that file always other than in the situation you explicitly tell git to ignore it for a reason. To do this, what I did was I told git to assume that the file is always unchanged. This will remove the file from the list of tracked files. Any changes to this file will be untracked. This is for git to temporarily ignore the changes to a particular file.

git update-index --no-assume-unchanged <file / file pattern>  

In the above command, the <file / file pattern> denotes a single file like the above config.js or a file pattern like say secret/*. If we are going to use this command frequently, it is better we create an alias for these two commands. One best way to do is to edit the global config file using git config --global --edit and add the aliases as shown below:

[alias]
    hide = update-index --assume-unchanged
    unhide = update-index --no-assume-unchanged

Examples

git hide secret.txt      # File stays committed in the repo, but will not be tracked.  
git unhide secret.txt    # File will be tracked normally.  

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
    cod  = checkout development
    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
    pro  = pull --rebase origin
    prod = pull --rebase origin development
    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
    uc   = !git reset --soft HEAD^ && git reset HEAD .

    d    = diff
    dc   = diff --cached

    hide = update-index --assume-unchanged
    unhide = update-index --no-assume-unchanged

    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
    cod  = checkout development
    b    = branch
    bd   = branch -D

    p    = pull
    po   = pull origin
    pom  = pull origin master
    pr   = pull --rebase
    pro  = pull --rebase origin
    prod = pull --rebase origin development
    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