784

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?

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
DevelopingChris
  • 39,797
  • 30
  • 87
  • 118
  • 3
    You can also see it here https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases – JayRizzo Nov 21 '17 at 17:43
  • Also see further questions on more advanced usage of git alias here: https://stackoverflow.com/questions/46528736/git-alias-returns-an-error-when-using-pipe-command/ – NeilG Aug 22 '19 at 04:01

26 Answers26

1235

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.

Warren P
  • 65,725
  • 40
  • 181
  • 316
Diego Dias
  • 21,634
  • 6
  • 33
  • 36
  • 99
    I highly recommend you use `git config --global` to place the aliases in `~/.gitconfig` instead of `.git/config` for your current repository. – Cascabel Mar 31 '10 at 14:56
  • 30
    I prefer settings `st` to `status -s` (short status) – hasen Mar 31 '10 at 15:59
  • 22
    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'` – Amir Raminfar Dec 01 '11 at 19:39
  • See also [git-alias, in git-extras](https://github.com/visionmedia/git-extras#git-alias) – floer32 Jul 09 '13 at 14:07
  • 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' ? – HellishHeat May 09 '14 at 15:43
  • Is it possible to alias a number, e.g. `git 8` ?? – Andy Hayden Aug 09 '14 at 23:15
  • 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. – tobek Sep 09 '14 at 19:42
  • 1
    @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.) – Michael Dorst Oct 13 '14 at 16:01
  • 18
    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"` – ABVincita Aug 06 '15 at 05:01
  • I prefer to set `st` as `status -u no` thereby omitting (a possibly long list of) untracked files to appear in the output. – jolvi Jan 23 '16 at 17:05
  • Use `--` for additional input to the command, e.g. (Windows) `git config --global alias.rename "branch -m --"` – Slate Mar 14 '18 at 10:01
  • 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. – Zoe Jul 17 '21 at 20:00
215

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'
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • 2
    For linux, I did this to get the git-completion.bash stuff: https://blogs.oracle.com/linuxnstuff/entry/recommended_git-completionbash – Duncan Lock Mar 09 '12 at 15:25
  • 14
    If you use zsh, the excellent oh-my-zsh suite contains a plugin with all those "standard" git aliases - https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh -- for bash, have a look at https://github.com/revans/bash-it – jobwat Aug 26 '13 at 00:22
  • noob question: what does it mean to "be sourced from" `~/.bashrc` file? – ahnbizcad Aug 25 '15 at 06:27
  • 1
    `~/.bashrc` : to really cut down the key-strokes. Exactly what was looking for. – parasrish Sep 20 '17 at 08:39
  • This answer is a perfect example of why I suggest using a .gitconfig include in my answer! https://stackoverflow.com/a/59896430/117471 – Bruno Bronosky Jan 24 '20 at 12:31
  • On Mac, if you cannot find the ~/.bashrc, you can find the ~/.bash_profile. Then, do the things described here in .bash_profile – htlbydgod Apr 02 '22 at 23:39
  • FYI linke to Pro Git is broken. – fatfrog Jun 19 '23 at 15:27
80

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 = me@example.com
[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
Orangetronic
  • 300
  • 5
  • 10
wcc526
  • 3,915
  • 2
  • 31
  • 29
  • how do you set this up? what do you put where to make this so? – ahnbizcad Aug 25 '15 at 05:57
  • 3
    @ahnbizcad Place in ~/.gitconfig if you git config --global otherwise it goes in .git/config of current repository – shmup Apr 27 '16 at 12:55
  • If it might help, a complete **[.gitconfig](https://github.com/niketpathak/devpreferences/blob/master/git/.gitconfig)** and the **[reference tutorial](https://digitalfortress.tech/tutorial/create-global-gitconfig-git-alias/)** to go along with it! – Niket Pathak Jul 09 '20 at 08:57
22

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
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
17

You can also chain commands if you use the '!' operator to spawn a shell:

aa = !git add -A && git status

This will both add all files and give you a status report with $ git aa.

For a handy way to check your aliases, add this alias:

alias = config --get-regexp ^alias\\.

Then a quick $ git alias gives you your current aliases and what they do.

Joseph Cheek
  • 554
  • 5
  • 11
13

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)
Nicolas Gramlich
  • 2,790
  • 19
  • 19
  • 1
    you could also do: git config --global alias.bco 'checkout -b'. Then you could do: git bco new-branch. :) – Russell Feb 12 '13 at 22:53
  • 4
    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 – Matthew Turner Feb 26 '14 at 18:22
  • 5
    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](https://git.wiki.kernel.org/index.php/Aliases#Advanced)) – Sam Aug 15 '14 at 11:13
13

I created the alias dog for showing the log graph:

git config --global alias.dog "log --all --decorate --oneline --graph"

And use it as follows:

git dog
Graeme
  • 169
  • 1
  • 9
Seyed Morteza Mousavi
  • 6,855
  • 8
  • 43
  • 69
10

Follwing are the 4 git shortcuts or aliases youc an use to save time.

Open the commandline and type these below 4 commands and use the shortcuts after.

git config --global alias.co checkout  
git config --global alias.ci commit    
git config --global alias.st status    
git config --global alias.br branch  

Now test them!

$ git co              # use git co instead of git checkout
$ git ci              # use git ci instead of git commit
$ git st              # use git st instead of git status
$ git br              # use git br instead of git branch
Prabhakar
  • 6,458
  • 2
  • 40
  • 51
10

For me (I'm using mac with terminal) only worked when I added on .bash_profile and opened another tab to load the change:

alias gst="git status"
alias gd="git diff"
alias gl="git log"
alias gco="git commit"
alias gck="git checkout"
alias gl="git pull"
alias gpom="git pull origin master"
alias gp="git push"
alias gb="git branch"
Marina
  • 1,682
  • 1
  • 20
  • 25
9

Add the following lines to your ~/.gitconfig in your home directory

[alias]
# one-line log
l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
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

a = add
ap = add -p
c = commit --verbose
ca = commit -a --verbose
cm = commit -m
cam = commit -a -m
m = commit --amend --verbose

d = diff
ds = diff --stat
dc = diff --cached

s = status -s
co = checkout
cob = checkout -b
# list branches sorted by last modified
b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

# list aliases
la = "!git config -l | grep alias | cut -c 7-"

Once that is done, you can do git a instead of git add for example. The same applies to other commands under the alias heading..

Stryker
  • 5,732
  • 1
  • 57
  • 70
8

This will create an alias st for status:

git config --add alias.st status

Nick Moore
  • 15,547
  • 6
  • 61
  • 83
8

For those looking to execute shell commands in a git alias, for example:

$ git pof

In my terminal will push force the current branch to my origin repo:

[alias]
    pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)

Where the

$(git branch | grep \\* | cut -d ' ' -f2)

command returns the current branch.

So this is a shortcut for manually typing the branch name:

git push origin -f <current-branch>
Gus
  • 6,545
  • 4
  • 36
  • 39
  • 2
    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? – Romain Valeri Jan 17 '19 at 17:13
  • Bash meshed up creating the alias (replacing `!git` with the last git command), but manually editing the config file did the trick. – H. de Jonge Sep 27 '19 at 09:09
7

You can alias both git and non-git commands. It looks like this was added in version 1.5. A snippet from the git config --help page on version 2.5.4 on my Mac shows:

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

For example, in your global .gitconfig file you could have:

[alias]
    st = status
    hi = !echo 'hello'

And then run them:

$ git hi
hello
$ git st
On branch master

...
mkobit
  • 43,979
  • 12
  • 156
  • 150
6

I added all the aliases command in .profile in user directory ( vim ~/.profile).

alias gs='git status'
alias gp='git pull'
alias gph='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -m'
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'
alias gfo='git fetch origin'

Then , I added source command in bash as well as zsh shell.

In bash shell ( vim ~/.bashrc)

source ~/.profile

In zsh shell ( vim ~/.zshrc )

source ~/.profile
Ankit kaushik
  • 1,043
  • 10
  • 6
  • You are detailing _shell_ aliases, not _git_ aliases. Those are different things. – Manur Jul 15 '22 at 13:32
  • ^ yes, but this is still pretty relevant to the question. It would make sense to clarify the distinction with benefits/drawbacks in the answer – Robin Métral Feb 24 '23 at 12:44
5

One line setup

$ git config --global alias.co checkout && git config --global alias.br branch && git config --global alias.ci commit && git config --global alias.st status && git config --global alias.unstage 'reset HEAD --' && git config --global alias.last 'log -1 HEAD'

Usage:

$ git st
$ git co
$ git br
$ git ci
$ git last
$ git unstage <file | dir>

Everything will set into:

$ cat ~/.gitconfig

[user]
    name = Sample User
    email = sample@gmail.com
[core]
    filemode = false
    compression = 1
    quotepath = off
    ignorecase = false
[color]
    ui = auto
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    last = log -1 HEAD
    unstage = reset HEAD --

Hope this faster.

Binh Ho
  • 3,690
  • 1
  • 31
  • 31
4
$ git update
git: 'update' is not a git command. See 'git --help'.

Did you mean this?
    update-ref

$ git config --global alias.update 'pull -v'

$ git update
From git://git.kernel.org/pub/scm/git/git
 = [up to date]      html       -> origin/html
 = [up to date]      maint      -> origin/maint
 = [up to date]      man        -> origin/man
 = [up to date]      master     -> origin/master
 = [up to date]      next       -> origin/next
 = [up to date]      pu         -> origin/pu
 = [up to date]      todo       -> origin/todo
Already up-to-date.
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
4

To create any alias in Git use following commands:

git config --local alias.s status

git config --local alias.c commit
git s

On branch master

nothing to commit, working tree clean

git status

On branch master

nothing to commit, working tree clean

TUSqasi
  • 750
  • 6
  • 13
3

You can set custom git aliases using git's config. Here's the syntax:

git config --global alias.<aliasName> "<git command>"

For example, if you need an alias to display a list of files which have merge conflicts, run:

git config --global alias.conflicts "diff --name-only --diff-filter=U"

Now you can use the above command only using "conflicts":

git conflicts
# same as running: git diff --name-only --diff-filter=U
3

Include multiple alias files in your .gitconfig

I suggest using a .gitconfig include for your aliases. Once you start creating aliases, you'll probably end up with a lot of them. They will likely be something you want to share with others. Putting them in a dedicated file makes it easy to share. Your team can even use a git repo to hold shared aliases. And of course some aliases you will not want to share, so keep them in a private alias file.

[include]
    path=src/dotfiles/.gitaliases

[include]
    path=src/team-utils/gitaliases

[include]
    path=.gitaliases.private
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
2

Just to get the aliases even shorter than the standard git config way mentioned in other answers, I created an npm package mingit (npm install -g mingit) so that most commands would become 2 characters instead of 2 words. Here's the examples:

g a .                   // git add .
g b other-branch        // git branch other-branch
g c "made some changes" // git commit -m "made some changes"
g co master             // git checkout master
g d                     // git diff
g f                     // git fetch
g i                     // git init 
g m hotfix              // git merge hotfix
g pll                   // git pull
g psh                   // git push
g s                     // git status

and other commands would be similarly short. This also keeps bash completions. The package adds a bash function to your dotfiles, works on osx, linux, and windows. Also, unlike the other aliases, it aliases git -> g as well as the second parameter.

ironicaldiction
  • 1,200
  • 4
  • 12
  • 27
2

If you want an alternative to the ~/.gitconfig option and open to digging in a little more, another option is to write entirely custom git commands by wrapping them in a global node package.

In your package.json, you'd define the root command (example: gt), and then filter the specific commands to execute the correct git commands. For example, git checkout my-branch could be gt co mybranch.

The "christian-git" package on npm uses this method: https://github.com/alexmacarthur/christian-git

Alex MacArthur
  • 2,220
  • 1
  • 18
  • 22
2

PFA screenshot of my .gitconfig file

with the below aliases

[alias]
    cb = checkout branch
    pullb = pull main branch
SRK
  • 41
  • 7
1

It is given here Aliases.Even there are great answers here, I added this because it differs in windows and linux

Community
  • 1
  • 1
madhu131313
  • 7,003
  • 7
  • 40
  • 53
0
alias s="git status"

Your pointer finger will forgive you for all the pain you've put it through your whole life.

JakeWilson801
  • 1,036
  • 7
  • 20
0

It seems there is one case missing in the various answers given here. I.e. what if the options given to the alias need to be inserted somewhere in the middle of the command. E.g. git grep hello -- :/ searches for hello starting at the root folder of the current repository. For Linux systems we can create an alias like

git config --global alias.ggrep '!git grep "$@" -- :/'

such that git ggrep hello will do the same.

Claas Bontus
  • 1,628
  • 1
  • 15
  • 29
-1

Another possibility for windows would be to have a directory filled with .bat files that have your shortcuts in them. The name of the file is the shortcut to be used. Simply add the directory to your PATH environment variable and you have all the shortcuts to your disposal in the cmd window.

For example (gc.bat):

git commit -m %1

Then you can execute the following command in the console:

gc "changed stuff"

The reason I'm adding this as an answer is because when using this you aren't limited to git ... only commands.

Rick
  • 1,172
  • 11
  • 25
  • 1
    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](https://githowto.com/aliases) used for git commands. There are better resources and tutorials on this, this just happens to be a link i had open. – SherylHohman Mar 30 '20 at 23:24