我如何在git中使用别名命令?

Dev*_*ris 592 git

我看到有人收到的截屏视频

git st
git ci
Run Code Online (Sandbox Code Playgroud)

上班.当我这样做时,我得到一个错误,问我是否有其他意思.
作为一个git newb,我需要知道你要做什么来完成这项工作?

Die*_*ias 928

基本上你只需要添加行 ~/.gitconfig

[alias]
    st = status
    ci = commit -v
Run Code Online (Sandbox Code Playgroud)

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

$ git config --global alias.st status 
Run Code Online (Sandbox Code Playgroud)

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

$ git config --global alias.ci 'commit -v'
Run Code Online (Sandbox Code Playgroud)

在Windows上,如果别名有空格或命令行参数,请使用双引号:

c:\dev> git config --global alias.ci "commit -v"
Run Code Online (Sandbox Code Playgroud)

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

  • 我强烈建议您使用`git config --global`将别名放在`〜/ .gitconfig`而不是`.git/config`中,用于当前的存储库. (83认同)
  • 我更喜欢设置`st`到`status -s`(简短状态) (25认同)
  • 这真的很棒.我一直在寻找这个.只是抬头,如果你有一个带空格的命令,你应该使用`'`like`git config --global alias.sr'svn rebase'` (18认同)
  • 如果您在Windows命令行上使用Git,那么在添加带空格的命令时,您将需要使用双引号""而不是单引号,例如`git config --global alias.ci"commit - v"` (13认同)

Mat*_*kin 167

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

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

[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    unstage = reset HEAD --
    last = log -1 HEAD
Run Code Online (Sandbox Code Playgroud)

另外,如果您正在使用bash,我建议git-completion.bash您通过复制到主目录并从您的主目录中获取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
Run Code Online (Sandbox Code Playgroud)

注意: 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'
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用zsh,优秀的oh-my-zsh套件包含一个带有所有"标准"git别名的插件 - https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git .plugin.zsh - 对于bash,请看一下https://github.com/revans/bash-it (9认同)
  • 对于linux,我这样做是为了得到git-completion.bash的东西:https://blogs.oracle.com/linuxnstuff/entry/recommended_git-completionbash (2认同)

wcc*_*526 62

我认为最有用的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
Run Code Online (Sandbox Code Playgroud)

  • @ahnbizcad放在〜/ .gitconfig中如果你git config --global,否则它进入当前存储库的.git/config (3认同)

Ala*_*avi 16

你需要这个git config alias命令.在Git存储库中执行以下命令:

git config alias.ci commit
Run Code Online (Sandbox Code Playgroud)

对于全局别名:

git config --global alias.ci commit
Run Code Online (Sandbox Code Playgroud)


Nic*_*ich 10

这对我有用:

bco = "!f(){ git branch ${1} && git checkout ${1}; };f"
Run Code Online (Sandbox Code Playgroud)

上:

$ git --version

git version 1.7.7.5 (Apple Git-26)
Run Code Online (Sandbox Code Playgroud)

  • 如果这是第一次我以外的任何人看到以`!`开头的git alias命令,请注意`从版本1.5.0开始,Git支持执行非git命令的别名,前缀为"!". ([参考](https://git.wiki.kernel.org/index.php/Aliases#Advanced)) (4认同)
  • 我喜欢`git cob`.让我想起夏天,就像在玉米棒上一样.实际上这是一个很好的词,我们没有考虑到足够的...这是 (3认同)

Gus*_*Gus 8

对于那些希望在 git alias 中执行 shell 命令的人,例如:

$ git pof
Run Code Online (Sandbox Code Playgroud)

在我的终端中将强制当前分支送到我的原始存储库:

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

哪里

$(git branch | grep \\* | cut -d ' ' -f2)
Run Code Online (Sandbox Code Playgroud)

命令返回当前分支。

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

git push origin -f <current-branch>
Run Code Online (Sandbox Code Playgroud)

  • 为什么不“简单地”`git push -f origin HEAD` 将当前分支推送到其远程对应分支?还有,用力推的捷径?如果您必须频繁地用力才能从快捷方式中受益,那么您的设置或工作流程中的其他地方是否有问题? (2认同)

Nic*_*ore 7

这将创建一个别名ststatus:

git config --add alias.st status


Pra*_*thi 6

Follwing是用于节省时间的4个git快捷方式或别名.

打开命令行并键入以下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  
Run Code Online (Sandbox Code Playgroud)

现在测试一下!

$ 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
Run Code Online (Sandbox Code Playgroud)


mko*_*bit 6

您可以为git和non-git命令添加别名.看起来这是在1.5版本中添加的.git config --help我的Mac上2.5.4版页面上的代码段显示:

如果别名扩展以感叹号为前缀,则将其视为shell命令.

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

[alias]
    st = status
    hi = !echo 'hello'
Run Code Online (Sandbox Code Playgroud)

然后运行它们:

$ git hi
hello
$ git st
On branch master

...
Run Code Online (Sandbox Code Playgroud)


Jos*_*eek 6

如果您使用 '!',您还可以链接命令 操作符产生一个shell:

aa = !git add -A && git status
Run Code Online (Sandbox Code Playgroud)

这将添加所有文件并为您提供带有$ git aa.

要方便地检查别名,请添加以下别名:

alias = config --get-regexp ^alias\\.
Run Code Online (Sandbox Code Playgroud)

然后快速$ git alias为您提供您当前的别名以及它们的作用。


Mar*_*ina 6

对我来说(我在终端上使用 mac)仅在我添加.bash_profile并打开另一个选项卡以加载更改时才有效:

alias gst="git status"
alias gd="git diff"
alias gl="git log"
alias gco="git commit"
alias gck="git checkout"
alias gl="git pull"
alias gpom="git pull origin master"
alias gp="git push"
alias gb="git branch"
Run Code Online (Sandbox Code Playgroud)


小智 6

我在用户目录( vim ~/.profile )的 .profile 中添加了所有别名命令。

alias gs='git status'
alias gp='git pull'
alias gph='git push'
alias gd='git diff | mate'
alias gau='git add --update'
alias gc='git commit -m'
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'
alias gfo='git fetch origin'
Run Code Online (Sandbox Code Playgroud)

然后,我在 bash 和 zsh shell 中添加了 source 命令。

在 bash shell 中(vim ~/.bashrc)

source ~/.profile
Run Code Online (Sandbox Code Playgroud)

在 zsh shell 中( vim ~/.zshrc )

source ~/.profile
Run Code Online (Sandbox Code Playgroud)


Str*_*ker 5

将以下行添加到您的主目录中的 ~/.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-"
Run Code Online (Sandbox Code Playgroud)

完成后,您可以做git a而不是git add例如。这同样适用于别名标题下的其他命令。


Sey*_*avi 5

我创建了dog用于显示日志图的别名:

git config --global alias.dog "log --all --decorate --oneline --graph"
Run Code Online (Sandbox Code Playgroud)

并按如下方式使用它:

git dog
Run Code Online (Sandbox Code Playgroud)


Bin*_* Ho 5

一条线设置

$ git config --global alias.co checkout && git config --global alias.br branch && git config --global alias.ci commit && git config --global alias.st status && git config --global alias.unstage 'reset HEAD --' && git config --global alias.last 'log -1 HEAD'
Run Code Online (Sandbox Code Playgroud)

用法:

$ git st
$ git co
$ git br
$ git ci
$ git last
$ git unstage <file | dir>
Run Code Online (Sandbox Code Playgroud)

一切都将设置为:

$ cat ~/.gitconfig

[user]
    name = Sample User
    email = sample@gmail.com
[core]
    filemode = false
    compression = 1
    quotepath = off
    ignorecase = false
[color]
    ui = auto
[alias]
    co = checkout
    br = branch
    ci = commit
    st = status
    last = log -1 HEAD
    unstage = reset HEAD --
Run Code Online (Sandbox Code Playgroud)

希望这更快。


小智 5

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

git config --local alias.s status

git config --local alias.c commit
Run Code Online (Sandbox Code Playgroud)
git s

On branch master

nothing to commit, working tree clean
Run Code Online (Sandbox Code Playgroud)
git status

On branch master

nothing to commit, working tree clean

Run Code Online (Sandbox Code Playgroud)