Search code examples
gitgit-alias

How do I alias commands in git?


I saw a screencast where someone had gotten

git st
git ci

to work. When I do it I get an error asking me if I meant something else.
Being a git newb, I need to know what you have to do to get this done?


Solution

  • As others have said the appropriate way to add git aliases is in your global .gitconfig file either by editing ~/.gitconfig or by using the git config --global alias.<alias> <git-command> command

    Below is a copy of the alias section of my ~/.gitconfig file:

    [alias]
        st = status
        ci = commit
        co = checkout
        br = branch
        unstage = reset HEAD --
        last = log -1 HEAD
    

    Also, if you're using bash, I would recommend setting up bash completion by copying git-completion.bash to your home directory and sourcing it from your ~/.bashrc. (I believe I learned about this from the Pro Git online book.) On Mac OS X, I accomplished this with the following commands:

    # Copy git-completion.bash to home directory
    cp usr/local/git/contrib/completion/git-completion.bash ~/
    
    # Add the following lines to ~/.bashrc
    if [ -x /usr/local/git/bin/git ]; then
        source ~/.git-completion.bash
    fi
    

    Note: The bash completion will work not only for the standard git commands but also for your git aliases.

    Finally, to really cut down on the keystrokes, I added the following to my ~/.bash_aliases file, which is sourced from ~/.bashrc:

    alias gst='git status'
    alias gl='git pull'
    alias gp='git push'
    alias gd='git diff | mate'
    alias gau='git add --update'
    alias gc='git commit -v'
    alias gca='git commit -v -a'
    alias gb='git branch'
    alias gba='git branch -a'
    alias gco='git checkout'
    alias gcob='git checkout -b'
    alias gcot='git checkout -t'
    alias gcotb='git checkout --track -b'
    alias glog='git log'
    alias glogp='git log --pretty=format:"%h %s" --graph'