我看了一段录像,里面有人

git st
git ci

去工作。当我这样做时,我得到一个错误,问我是否我指的是其他东西。 作为一个新手,我想知道你要怎么做才能完成这件事?


当前回答

需要执行命令git config alias。在Git存储库中执行以下命令:

git config alias.ci commit

对于全局别名:

git config --global alias.ci commit

其他回答

基本上,您只需要在~/.gitconfig中添加行即可

[alias]
    st = status
    ci = commit -v

或者你可以使用git config alias命令:

$ git config --global alias.st status 

在unix中,如果别名中有空格,则使用单引号:

$ git config --global alias.ci 'commit -v'

在windows中,如果别名有空格或命令行参数,则使用双引号:

c:\dev> git config --global alias.ci "commit -v"

alias命令甚至接受函数作为参数。看看别名。

这将为status创建一个别名st:

Git配置——添加别名。圣的地位

可以为git和非git命令设置别名。这看起来像是在1.5版中添加的。在我的Mac 2.5.4版本上,git配置帮助页面的一个片段显示:

如果别名展开以感叹号作为前缀,则它将被视为shell命令。

例如,在你的全局.gitconfig文件中,你可以有:

[alias]
    st = status
    hi = !echo 'hello'

然后运行它们:

$ git hi
hello
$ git st
On branch master

...

为了让别名比其他答案中提到的标准git配置方式更短,我创建了一个npm包mingit (npm install -g mingit),这样大多数命令将变成两个字符而不是两个单词。下面是一些例子:

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

其他命令也同样简短。这也保持了bash的完成。该包为您的dotfiles添加了一个bash函数,适用于osx, linux和windows。此外,与其他别名不同,它别名git -> g以及第二个参数。

在~/中添加以下行。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-"

一旦完成,你可以用git a代替git add。这同样适用于别名标题下的其他命令..