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

git st
git ci

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


当前回答

为了让别名比其他答案中提到的标准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以及第二个参数。

其他回答

要在Git中创建别名,请使用以下命令:

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

在~/中添加以下行。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。这同样适用于别名标题下的其他命令..

如果您想要~/。Gitconfig选项,可以更深入地挖掘,另一个选项是通过将它们包装在全局节点包中来编写完全自定义的git命令。

在你的包裹里。Json,您将定义根命令(例如:gt),然后筛选特定的命令以执行正确的git命令。例如,git checkout my-branch可以是gt co mybranch。

npm上的“christian-git”包使用这个方法:https://github.com/alexmacarthur/christian-git

正如其他人所说,添加git别名的适当方法是在全局的.gitconfig文件中编辑~/。Gitconfig或者使用gitconfig——global别名。<alias> <git-command>命令

下面是我的~/的别名部分的副本。gitconfig文件:

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD

此外,如果您正在使用bash,我建议通过复制git-completion来设置bash补全。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

注意:bash补全不仅适用于标准的git命令,也适用于你的git别名。

最后,为了真正减少击键次数,我在~/中添加了以下代码。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'

如果你使用'!'操作符来生成一个shell:

aa = !git add -A && git status

这将添加所有文件,并为您提供$ git aa的状态报告。

为了方便地检查你的别名,添加这个别名:

alias = config --get-regexp ^alias\\.

然后一个快速的$ git别名会告诉你当前的别名以及它们的用途。