How do I alias commands in git?

282,345

Solution 1

Basically you just need to add lines to ~/.gitconfig

[alias]
    st = status
    ci = commit -v

Or you can use the git config alias command:

$ git config --global alias.st status 

On unix, use single quotes if the alias has a space:

$ git config --global alias.ci 'commit -v'

On windows, use double quotes if the alias has a space or a command line argument:

c:\dev> git config --global alias.ci "commit -v"

The alias command even accepts functions as parameters. Take a look at aliases.

Solution 2

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'

Solution 3

I think the most useful gitconfig is like this,we always use the 20% function in git,you can try the "g ll",it is amazing,the details:

[user]
    name = my name
    email = [email protected]
[core]  
    editor = vi 
[alias]
    aa = add --all
    bv = branch -vv
    ba = branch -ra
    bd = branch -d
    ca = commit --amend
    cb = checkout -b
    cm = commit -a --amend -C HEAD
    ci = commit -a -v
    co = checkout
    di = diff
    ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
    ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
    ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
    mm = merge --no-ff
    st = status --short --branch
    tg = tag -a 
    pu = push --tags
    un = reset --hard HEAD  
    uh = reset --hard HEAD^
   [color]  
    diff = auto  
    status = auto  
    branch = auto 
   [branch]  
    autosetuprebase = always

Solution 4

You need the git config alias command. Execute the following in a Git repository:

git config alias.ci commit

For global alias:

git config --global alias.ci commit

Solution 5

This worked for me:

bco = "!f(){ git branch ${1} && git checkout ${1}; };f"

on:

$ git --version

git version 1.7.7.5 (Apple Git-26)
Share:
282,345
DevelopingChris
Author by

DevelopingChris

I'm a software developer mainly doing web based applications.

Updated on July 08, 2022

Comments

  • DevelopingChris
    DevelopingChris almost 2 years

    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?

  • Cascabel
    Cascabel about 14 years
    I highly recommend you use git config --global to place the aliases in ~/.gitconfig instead of .git/config for your current repository.
  • hasen
    hasen about 14 years
    I prefer settings st to status -s (short status)
  • Amir Raminfar
    Amir Raminfar over 12 years
    This is really awesome. I have been looking for this. Just a heads up, if you have a command with spaces you should use ' like git config --global alias.sr 'svn rebase'
  • Duncan Lock
    Duncan Lock about 12 years
    For linux, I did this to get the git-completion.bash stuff: blogs.oracle.com/linuxnstuff/entry/…
  • Russell
    Russell over 11 years
    you could also do: git config --global alias.bco 'checkout -b'. Then you could do: git bco new-branch. :)
  • floer_m
    floer_m almost 11 years
  • jobwat
    jobwat over 10 years
    If you use zsh, the excellent oh-my-zsh suite contains a plugin with all those "standard" git aliases - github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/… -- for bash, have a look at github.com/revans/bash-it
  • Matthew Turner
    Matthew Turner about 10 years
    I like git cob. reminds me of summer, as in corn on the cob. actually a great word we don't think about enough... cob that is
  • HellishHeat
    HellishHeat about 10 years
    These Aliases are git specific. 'e.g. 'git st'' . What if I want to make a non-git alias permanent. such as 'cd /c/code/git/mproject' ?
  • Andy Hayden
    Andy Hayden almost 10 years
    Is it possible to alias a number, e.g. git 8 ??
  • Sam
    Sam almost 10 years
    In case this is the first time anyone other than me has seen a git alias command starting with !, note that Since version 1.5.0, Git supports aliases executing non-git commands, by prefixing the value with "!" (ref)
  • tobek
    tobek over 9 years
    Similar to @hasenj, I prefer setting st to status -sb - short status but still displaying which branch you're on. @AndyHayden - that doesn't work, I get error: invalid key: alias.8. @HellishHeat - assuming you're using bash or similar, you're looking for bash aliases. Very similar, e.g. alias ll="ls -l". You can put these in your ~/.bashrc, that kind of thing.
  • Michael Dorst
    Michael Dorst over 9 years
    @HellishHeat These aliases are created by git, for git. If you want aliases for some other command line system, you'll have to look up how to do that one that system. (You appear to be using a Unix-like system, and I happen to know that creating aliases on Unices is quite simple. The syntax is different though. Try a Google search.)
  • ABVincita
    ABVincita almost 9 years
    Just another heads up, if you're using Git on Windows command line, then you will need to use double quotes " instead of single quotes when adding command with spaces, e.g. git config --global alias.ci "commit -v"
  • ahnbizcad
    ahnbizcad over 8 years
    how do you set this up? what do you put where to make this so?
  • ahnbizcad
    ahnbizcad over 8 years
    noob question: what does it mean to "be sourced from" ~/.bashrc file?
  • AlignedDev
    AlignedDev over 8 years
    I needed the --add and to use double quotes, not single quotes
  • jolvi
    jolvi over 8 years
    I prefer to set st as status -u no thereby omitting (a possibly long list of) untracked files to appear in the output.
  • shmup
    shmup about 8 years
    @ahnbizcad Place in ~/.gitconfig if you git config --global otherwise it goes in .git/config of current repository
  • biniam
    biniam almost 8 years
    Thank you for creating the github project.
  • Abdullah
    Abdullah about 7 years
    Why git st when you can use git s, get rid of that s :P
  • parasrish
    parasrish over 6 years
    ~/.bashrc : to really cut down the key-strokes. Exactly what was looking for.
  • Slate
    Slate about 6 years
    Use -- for additional input to the command, e.g. (Windows) git config --global alias.rename "branch -m --"
  • Romain Valeri
    Romain Valeri over 5 years
    Why not "simply" git push -f origin HEAD to push current branch to its remote counterpart? Also, a shortcut to push with force? If you have to push force frequently enough to benefit from a shortcut, isn't something amiss elsewhere in your setup or workflow?
  • H. de Jonge
    H. de Jonge over 4 years
    Bash meshed up creating the alias (replacing !git with the last git command), but manually editing the config file did the trick.
  • Bruno Bronosky
    Bruno Bronosky over 4 years
    This answer is a perfect example of why I suggest using a .gitconfig include in my answer! stackoverflow.com/a/59896430/117471
  • SherylHohman
    SherylHohman about 4 years
    You can do the same by adding shell aliases to your .profile, or .bashrc, or .bash_profile, depending on your system. Then all your aliases are in a single file. Also, that's the standard mechanism to accomplish your task. The section "command aliases" shows some commonly defined shell aliases used for git commands. There are better resources and tutorials on this, this just happens to be a link i had open.
  • Niket Pathak
    Niket Pathak almost 4 years
    If it might help, a complete .gitconfig and the reference tutorial to go along with it!
  • JakeWilson801
    JakeWilson801 over 3 years
    why even have git s? alias s="git status"
  • JakeWilson801
    JakeWilson801 over 3 years
    I like your style
  • Vasilii Suricov
    Vasilii Suricov about 3 years
    thank you man! i was looking how to do it by one copypaste action ;)
  • Zoe stands with Ukraine
    Zoe stands with Ukraine almost 3 years
    It's extremely disappointing that alias config is tied to ~/.gitconfig when it's the same file that contains the global user configuration. Makes it a pain in the ass to distribute as a part of a dotfile directory without breaking various local config, that may or may not have to be unique. Off the top of my head, a work/private configuration for the email and potentially signing key means the file can't just be copied without also needing to reconfigure the email and signing key on each machine after an update to the file.
  • htlbydgod
    htlbydgod about 2 years
    On Mac, if you cannot find the ~/.bashrc, you can find the ~/.bash_profile. Then, do the things described here in .bash_profile