How do I alias commands in git?

GitGit Alias

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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 - Git

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 - Git

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)

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

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.

Solution 9 - Git

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"

Solution 10 - Git

This will create an alias st for status:

git config --add alias.st status

Solution 11 - Git

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>

Solution 12 - Git

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..

Solution 13 - Git

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

...

Solution 14 - Git

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.

Solution 15 - Git

$ 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.

Solution 16 - Git

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

Solution 17 - Git

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

Solution 18 - Git

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.

Solution 19 - Git

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

Solution 20 - Git

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

Solution 21 - Git

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

Solution 22 - Git

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

Solution 23 - Git

PFA screenshot of my .gitconfig file

with the below aliases

[alias]
	cb = checkout branch
	pullb = pull main branch

Solution 24 - Git

alias s="git status"

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

Solution 25 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDevelopingChrisView Question on Stackoverflow
Solution 1 - GitDiego DiasView Answer on Stackoverflow
Solution 2 - GitMatthew RankinView Answer on Stackoverflow
Solution 3 - Gitwcc526View Answer on Stackoverflow
Solution 4 - GitAlan Haggai AlaviView Answer on Stackoverflow
Solution 5 - GitNicolas GramlichView Answer on Stackoverflow
Solution 6 - GitSeyed Morteza MousaviView Answer on Stackoverflow
Solution 7 - GitPrabhakar UndurthiView Answer on Stackoverflow
Solution 8 - GitJoseph CheekView Answer on Stackoverflow
Solution 9 - GitMarinaView Answer on Stackoverflow
Solution 10 - GitNick MooreView Answer on Stackoverflow
Solution 11 - GitGusView Answer on Stackoverflow
Solution 12 - GitStrykerView Answer on Stackoverflow
Solution 13 - GitmkobitView Answer on Stackoverflow
Solution 14 - GitBinh HoView Answer on Stackoverflow
Solution 15 - GitGreg BaconView Answer on Stackoverflow
Solution 16 - GitAkshay BosamiyaView Answer on Stackoverflow
Solution 17 - GitAshok KumarView Answer on Stackoverflow
Solution 18 - GitironicaldictionView Answer on Stackoverflow
Solution 19 - GitAlex MacArthurView Answer on Stackoverflow
Solution 20 - GitBruno BronoskyView Answer on Stackoverflow
Solution 21 - GitAnkit kaushikView Answer on Stackoverflow
Solution 22 - Gitmadhu131313View Answer on Stackoverflow
Solution 23 - GitSRKView Answer on Stackoverflow
Solution 24 - GitJakeWilson801View Answer on Stackoverflow
Solution 25 - GitRickView Answer on Stackoverflow