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

git st
git ci

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


当前回答

对于那些希望以git别名执行shell命令的人,例如:

$ git pof

在我的终端将推动当前分支到我的原点回购:

[alias]
    pof = !git push origin -f $(git branch | grep \\* | cut -d ' ' -f2)

在哪里

$(git branch | grep \\* | cut -d ' ' -f2)

命令返回当前分支。

这是手动输入分支名称的快捷方式:

git push origin -f <current-branch>

其他回答

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

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

...

这招对我很管用:

bco = "!f(){ git branch ${1} && git checkout ${1}; };f"

on:

$ git --version

git version 1.7.7.5 (Apple Git-26)
$ 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.

我创建了别名dog来显示日志图:

git config --global alias.dog "log --all --decorate --oneline --graph"

使用方法如下:

git dog