如何在 git 中给命令别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2553786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I alias commands in git?
提问by DevelopingChris
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 newb,我需要知道你必须做什么才能完成这项工作?
回答by Diego Dias
Basically you just need to add lines to ~/.gitconfig
基本上你只需要添加行 ~/.gitconfig
[alias]
st = status
ci = commit -v
Or you can use the git config alias command:
或者您可以使用 git config alias 命令:
$ git config --global alias.st status
On unix, use single quotes if the alias has a space:
在 unix 上,如果别名有空格,请使用单引号:
$ git config --global alias.ci 'commit -v'
On windows, use double quotes if the alias has a space or a command line argument:
在 Windows 上,如果别名有空格或命令行参数,请使用双引号:
c:\dev> git config --global alias.ci "commit -v"
The alias command even accepts functions as parameters. Take a look at aliases.
alias 命令甚至接受函数作为参数。看看别名。
回答by Matthew Rankin
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
正如其他人所说,添加 git 别名的适当方法.gitconfig
是通过编辑~/.gitconfig
或使用git config --global alias.<alias> <git-command>
命令在您的全局文件中
Below is a copy of the alias section of my ~/.gitconfig
file:
以下是我的~/.gitconfig
文件别名部分的副本:
[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 Gitonline book.) On Mac OS X, I accomplished this with the following commands:
此外,如果您使用 bash,我建议git-completion.bash
您通过复制到您的主目录并从您的~/.bashrc
. (我相信我是从Pro Git在线书籍中了解到的。)在 Mac OS X 上,我使用以下命令完成了此操作:
# 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.
注意:bash 补全不仅适用于标准 git 命令,也适用于您的 git 别名。
Finally, to really cut down on the keystrokes, I added the following to my ~/.bash_aliases
file, which is sourced from ~/.bashrc
:
最后,为了真正减少击键次数,我将以下内容添加到我的~/.bash_aliases
文件中,该文件来自~/.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'
回答by wcc526
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:
我觉得最有用的gitconfig是这样的,我们在git中总是使用20%的功能,你可以试试“g ll”,很神奇,详情:
[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
回答by Alan Haggai Alavi
You need the git config alias
command. Execute the following in a Git repository:
你需要这个git config alias
命令。在 Git 存储库中执行以下操作:
git config alias.ci commit
For global alias:
对于全局别名:
git config --global alias.ci commit
回答by Nicolas Gramlich
This worked for me:
这对我有用:
bco = "!f(){ git branch && git checkout ; };f"
on:
在:
$ git --version
git version 1.7.7.5 (Apple Git-26)
回答by Nick Moore
This will create an alias st
for status
:
这将创建一个别名st
为status
:
git config --add alias.st status
git config --add alias.st status
回答by Prabhakar Undurthi
Follwing are the 4 git shortcuts or aliases youc an use to save time.
以下是 4 个可用于节省时间的 git 快捷方式或别名。
Open the commandline and type these below 4 commands and use the shortcuts after.
打开命令行并键入以下 4 个命令,然后使用快捷方式。
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
回答by Gus
For those looking to execute shell commands in a git alias, for example:
对于那些希望在 git alias 中执行 shell 命令的人,例如:
$ git pof
In my terminal will push forcethe 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>
回答by mkobit
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:
您可以为 git 和非 git 命令设置别名。看起来这是在 1.5 版中添加的。git config --help
我的 Mac 上 2.5.4 版页面的片段显示:
If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.
如果别名扩展以感叹号为前缀,它将被视为一个 shell 命令。
For example, in your global .gitconfig
file you could have:
例如,在您的全局.gitconfig
文件中,您可以拥有:
[alias]
st = status
hi = !echo 'hello'
And then run them:
然后运行它们:
$ git hi
hello
$ git st
On branch master
...
回答by Stryker
Add the following lines to your ~/.gitconfig in your home directory
将以下行添加到您的主目录中的 ~/.gitconfig
[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..
完成后,您可以做git a
而不是git add
例如。这同样适用于别名标题下的其他命令。