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

git st
git ci

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


当前回答

可以为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

...

其他回答

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

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

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

基本上,您只需要在~/.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命令甚至接受函数作为参数。看看别名。

这里给出了别名。即使这里有很好的答案,我加上这个是因为 它在Windows和Linux中有所不同

正如其他人所说,添加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'

我认为最有用的gitconfig是这样的,我们总是在git中使用20%的函数,你可以试试“g ll”,它是惊人的,细节:

[user]
    name = my name
    email = me@example.com
[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