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?
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?
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'
~/.bashrc
file? –
Centralization ~/.bashrc
: to really cut down the key-strokes. Exactly what was looking for. –
Distance 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.
git config --global
to place the aliases in ~/.gitconfig
instead of .git/config
for your current repository. –
Jude st
to status -s
(short status) –
Jordonjorey '
like git config --global alias.sr 'svn rebase'
–
Procora git 8
?? –
Scansion 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 "
instead of single quotes when adding command with spaces, e.g. git config --global alias.ci "commit -v"
–
Selsyn st
as status -u no
thereby omitting (a possibly long list of) untracked files to appear in the output. –
Wawro --
for additional input to the command, e.g. (Windows) git config --global alias.rename "branch -m --"
–
Roorback ~/.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 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'
~/.bashrc
file? –
Centralization ~/.bashrc
: to really cut down the key-strokes. Exactly what was looking for. –
Distance 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
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
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.
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
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)
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 –
Chlordane !
, note that Since version 1.5.0, Git supports aliases executing non-git commands, by prefixing the value with "!"
(ref) –
Statist 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
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"
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..
This will create an alias st
for status
:
git config --add alias.st status
git st
when you can use git s
, get rid of that s
:P –
Juggle 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>
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 !git
with the last git command), but manually editing the config file did the trick. –
Invasion 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
...
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
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.
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
$ 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.
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
.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
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.
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
PFA screenshot of my .gitconfig file
with the below aliases
[alias]
cb = checkout branch
pullb = pull main branch
It is given here Aliases.Even there are great answers here, I added this because it differs in windows and linux
alias s="git status"
Your pointer finger will forgive you for all the pain you've put it through your whole life.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.