How do I alias commands in git?
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)
DevelopingChris
I'm a software developer mainly doing web based applications.
Updated on July 08, 2022Comments
-
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 about 14 yearsI highly recommend you use
git config --global
to place the aliases in~/.gitconfig
instead of.git/config
for your current repository. -
hasen about 14 yearsI prefer settings
st
tostatus -s
(short status) -
Amir Raminfar over 12 yearsThis is really awesome. I have been looking for this. Just a heads up, if you have a command with spaces you should use
'
likegit config --global alias.sr 'svn rebase'
-
Duncan Lock about 12 yearsFor linux, I did this to get the git-completion.bash stuff: blogs.oracle.com/linuxnstuff/entry/…
-
Russell over 11 yearsyou could also do: git config --global alias.bco 'checkout -b'. Then you could do: git bco new-branch. :)
-
floer_m almost 11 yearsSee also git-alias, in git-extras
-
jobwat over 10 yearsIf 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 about 10 yearsI 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 about 10 yearsThese 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 almost 10 yearsIs it possible to alias a number, e.g.
git 8
?? -
Sam almost 10 yearsIn case this is the first time anyone other than me has seen a git alias command starting with
!
, note thatSince version 1.5.0, Git supports aliases executing non-git commands, by prefixing the value with "!"
(ref) -
tobek over 9 yearsSimilar to @hasenj, I prefer setting
st
tostatus -sb
- short status but still displaying which branch you're on. @AndyHayden - that doesn't work, I geterror: invalid key: alias.8
. @HellishHeat - assuming you're usingbash
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 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 almost 9 yearsJust 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 over 8 yearshow do you set this up? what do you put where to make this so?
-
ahnbizcad over 8 yearsnoob question: what does it mean to "be sourced from"
~/.bashrc
file? -
AlignedDev over 8 yearsI needed the --add and to use double quotes, not single quotes
-
jolvi over 8 yearsI prefer to set
st
asstatus -u no
thereby omitting (a possibly long list of) untracked files to appear in the output. -
shmup about 8 years@ahnbizcad Place in ~/.gitconfig if you git config --global otherwise it goes in .git/config of current repository
-
biniam almost 8 yearsThank you for creating the github project.
-
Abdullah about 7 yearsWhy
git st
when you can usegit s
, get rid of thats
:P -
parasrish over 6 years
~/.bashrc
: to really cut down the key-strokes. Exactly what was looking for. -
Slate about 6 yearsUse
--
for additional input to the command, e.g. (Windows)git config --global alias.rename "branch -m --"
-
Romain Valeri over 5 yearsWhy 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 over 4 yearsBash meshed up creating the alias (replacing
!git
with the last git command), but manually editing the config file did the trick. -
Bruno Bronosky over 4 yearsThis answer is a perfect example of why I suggest using a .gitconfig include in my answer! stackoverflow.com/a/59896430/117471
-
SherylHohman about 4 yearsYou 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 almost 4 yearsIf it might help, a complete .gitconfig and the reference tutorial to go along with it!
-
JakeWilson801 over 3 yearswhy even have git s? alias s="git status"
-
JakeWilson801 over 3 yearsI like your style
-
Vasilii Suricov about 3 yearsthank you man! i was looking how to do it by one copypaste action ;)
-
Zoe stands with Ukraine almost 3 yearsIt'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 about 2 yearsOn Mac, if you cannot find the ~/.bashrc, you can find the ~/.bash_profile. Then, do the things described here in .bash_profile