How do I alias commands in git?
Asked Answered
K

26

807

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?

Kratz answered 31/3, 2010 at 14:31 Comment(2)
You can also see it here git-scm.com/book/en/v2/Git-Basics-Git-AliasesTransduction
Also see further questions on more advanced usage of git alias here: stackoverflow.com/questions/46528736/…Jeneejenei
M
225

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'
Mcconaghy answered 31/3, 2010 at 15:19 Comment(7)
For linux, I did this to get the git-completion.bash stuff: blogs.oracle.com/linuxnstuff/entry/…Ensconce
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-itVying
noob question: what does it mean to "be sourced from" ~/.bashrc file?Centralization
~/.bashrc : to really cut down the key-strokes. Exactly what was looking for.Distance
This answer is a perfect example of why I suggest using a .gitconfig include in my answer! https://mcmap.net/q/12143/-how-do-i-alias-commands-in-gitHerodotus
On Mac, if you cannot find the ~/.bashrc, you can find the ~/.bash_profile. Then, do the things described here in .bash_profilePsych
FYI linke to Pro Git is broken.Demona
P
1266

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.

Punctate answered 31/3, 2010 at 14:33 Comment(12)
I highly recommend you use git config --global to place the aliases in ~/.gitconfig instead of .git/config for your current repository.Jude
I prefer settings st to status -s (short status)Jordonjorey
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'Procora
See also git-alias, in git-extrasLeadwort
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' ?Haskins
Is it possible to alias a number, e.g. git 8 ??Scansion
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. @Haskins - 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.Adulterant
@Haskins 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.)Elka
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"Selsyn
I prefer to set st as status -u no thereby omitting (a possibly long list of) untracked files to appear in the output.Wawro
Use -- for additional input to the command, e.g. (Windows) git config --global alias.rename "branch -m --"Roorback
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.Camshaft
M
225

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'
Mcconaghy answered 31/3, 2010 at 15:19 Comment(7)
For linux, I did this to get the git-completion.bash stuff: blogs.oracle.com/linuxnstuff/entry/…Ensconce
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-itVying
noob question: what does it mean to "be sourced from" ~/.bashrc file?Centralization
~/.bashrc : to really cut down the key-strokes. Exactly what was looking for.Distance
This answer is a perfect example of why I suggest using a .gitconfig include in my answer! https://mcmap.net/q/12143/-how-do-i-alias-commands-in-gitHerodotus
On Mac, if you cannot find the ~/.bashrc, you can find the ~/.bash_profile. Then, do the things described here in .bash_profilePsych
FYI linke to Pro Git is broken.Demona
B
81

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
Bernicebernie answered 20/2, 2014 at 14:12 Comment(3)
how do you set this up? what do you put where to make this so?Centralization
@Centralization Place in ~/.gitconfig if you git config --global otherwise it goes in .git/config of current repositoryGoldeye
If it might help, a complete .gitconfig and the reference tutorial to go along with it!Thao
M
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
Mayers answered 31/3, 2010 at 14:34 Comment(0)
M
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.

Marmawke answered 30/1, 2017 at 21:38 Comment(0)
A
14

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
Adverbial answered 18/7, 2019 at 10:4 Comment(0)
U
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)
Undertow answered 24/9, 2012 at 18:8 Comment(3)
you could also do: git config --global alias.bco 'checkout -b'. Then you could do: git bco new-branch. :)Manysided
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 isChlordane
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)Statist
S
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
Showroom answered 3/3, 2015 at 5:23 Comment(1)
thank you man! i was looking how to do it by one copypaste action ;)Disillusionize
E
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"
Extrasystole answered 13/3, 2019 at 14:39 Comment(1)
I like your styleCovenant
A
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..

Auscultation answered 19/10, 2018 at 16:2 Comment(0)
V
8

This will create an alias st for status:

git config --add alias.st status

Verrucose answered 31/3, 2010 at 14:34 Comment(3)
I needed the --add and to use double quotes, not single quotesParent
Why git st when you can use git s, get rid of that s :PJuggle
why even have git s? alias s="git status"Covenant
S
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>
Spates answered 7/4, 2017 at 20:0 Comment(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?Pedestrianism
Bash meshed up creating the alias (replacing !git with the last git command), but manually editing the config file did the trick.Invasion
D
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

...
Diapositive answered 4/2, 2016 at 17:38 Comment(0)
E
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
Elysha answered 5/2, 2021 at 16:32 Comment(2)
You are detailing shell aliases, not git aliases. Those are different things.Bebeeru
^ yes, but this is still pretty relevant to the question. It would make sense to clarify the distinction with benefits/drawbacks in the answerCatiline
Y
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 = [email protected]
[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.

Yellowhammer answered 26/7, 2020 at 1:37 Comment(0)
L
5

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

Latvian answered 15/7, 2021 at 9:3 Comment(0)
H
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.
Hyder answered 31/3, 2010 at 14:35 Comment(0)
N
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
Neuroblast answered 5/4, 2018 at 11:26 Comment(0)
H
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
Herodotus answered 24/1, 2020 at 12:29 Comment(0)
J
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.

Jacquelinejacquelyn answered 17/11, 2015 at 2:24 Comment(1)
Thank you for creating the github project.Scapolite
I
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

Iceland answered 12/9, 2017 at 12:30 Comment(0)
S
2

PFA screenshot of my .gitconfig file

with the below aliases

[alias]
    cb = checkout branch
    pullb = pull main branch
Schroer answered 23/11, 2018 at 8:48 Comment(0)
L
1

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

Lamarlamarck answered 15/8, 2013 at 3:14 Comment(0)
C
0
alias s="git status"

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

Covenant answered 1/10, 2020 at 17:56 Comment(0)
R
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.

Rainbolt answered 19/7, 2023 at 11:0 Comment(0)
J
-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.

Jilleen answered 23/3, 2017 at 23:50 Comment(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 used for git commands. There are better resources and tutorials on this, this just happens to be a link i had open.Feudal

© 2022 - 2024 — McMap. All rights reserved.