714

我看到了一个截屏视频,有人得到了

git st
git ci

去工作。当我这样做时,我收到一个错误,询问我是否还有其他意思。
作为一个 git newb,我需要知道你必须做什么才能完成这项工作?

4

25 回答 25

1122

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

于 2010-03-31T14:33:40.197 回答
198

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

此外,如果您使用的是 bash,我建议您通过复制git-completion.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'
于 2010-03-31T15:19:44.723 回答
73

我觉得最有用的 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
于 2014-02-20T14:12:41.277 回答
21

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

git config alias.ci commit

对于全局别名:

git config --global alias.ci commit
于 2010-03-31T14:34:43.317 回答
12

这对我有用:

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

在:

$ git --version

git version 1.7.7.5 (Apple Git-26)
于 2012-09-24T18:08:13.157 回答
10

以下是您用来节省时间的 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  

现在测试它们!

$ 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
于 2015-03-03T05:23:43.703 回答
9

对我来说(我正在使用带有终端的 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"
于 2019-03-13T14:39:19.230 回答
9

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

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

并按如下方式使用它:

git dog
于 2019-07-18T10:04:51.617 回答
8

这将创建一个别名ststatus

git config --add alias.st status

于 2010-03-31T14:34:40.570 回答
8

对于那些希望在 git alias 中执行 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>
于 2017-04-07T20:00:00.253 回答
8

将以下行添加到您的主目录中的 ~/.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例如。这同样适用于别名标题下的其他命令。

于 2018-10-19T16:02:49.640 回答
7

如果您使用“!”,您还可以链接命令 生成外壳的运算符:

aa = !git add -A && git status

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

为了方便地检查您的别名,请添加此别名:

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

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

于 2017-01-30T21:38:13.157 回答
6

您可以为 git 和非 git 命令设置别名。看起来这是在 1.5 版中添加的。git config --help我的 Mac 上 2.5.4 版页面的片段显示:

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

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

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

然后运行它们:

$ git hi
hello
$ git st
On branch master

...
于 2016-02-04T17:38:43.617 回答
4
$ git更新
git: 'update' 不是 git 命令。请参阅“git --help”。

你是这个意思吗?
    更新参考

$ git config --global alias.update 'pull -v'

$ git更新
来自 git://git.kernel.org/pub/scm/git/git
 = [最新] html -> 来源/html
 = [最新] 维护 -> 起源/维护
 = [最新] 人 -> 起源/人
 = [最新] master -> origin/master
 = [最新] 下一个 -> 原点/下一个
 = [最新] pu -> 原产地/pu
 = [最新] 待办事项 -> 来源/待办事项
已经是最新的了。
于 2010-03-31T14:35:16.023 回答
4

一条线设置

$ 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'

用法:

$ git st
$ git co
$ git br
$ git ci
$ git last
$ git unstage <file | dir>

一切都将设置为:

$ 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 --

希望这更快。

于 2020-07-26T01:37:15.090 回答
3

您可以使用 git 的配置设置自定义 git 别名。这是语法:

git config --global alias.<aliasName> "<git command>"

例如,如果您需要一个别名来显示具有合并冲突的文件列表,请运行:

git config --global alias.conflicts "diff --name-only --diff-filter=U"

现在您只能使用“冲突”来使用上述命令:

git conflicts
# same as running: git diff --name-only --diff-filter=U
于 2018-04-05T11:26:02.993 回答
2

只是为了让别名比其他答案中提到的标准 git config 方式更短,我创建了一个 npm 包mingit ( npm install -g mingit),以便大多数命令将变为 2 个字符而不是 2 个单词。以下是示例:

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以及第二个参数。

于 2015-11-17T02:24:06.760 回答
2

如果您想要替代该~/.gitconfig选项并愿意进一步挖掘,另一种选择是通过将它们包装在全局节点包中来编写完全自定义的 git 命令。

在您的 package.json 中,您将定义根命令(例如:),gt然后过滤特定命令以执行正确的 git 命令。例如,git checkout my-branch可能是gt co mybranch

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

于 2017-09-12T12:30:50.117 回答
2

在您的文件中包含多个别名文件.gitconfig

我建议为您的别名使用.gitconfig 包含。一旦你开始创建别名,你可能最终会得到很多。它们很可能是您想与他人分享的东西。将它们放在专用文件中可以轻松共享。您的团队甚至可以使用 git 存储库来保存共享别名。当然还有一些您不想共享的别名,因此请将它们保存在私有别名文件中。

[include]
    path=src/dotfiles/.gitaliases

[include]
    path=src/team-utils/gitaliases

[include]
    path=.gitaliases.private
于 2020-01-24T12:29:01.033 回答
2

要在 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

于 2021-07-15T09:03:30.137 回答
1

这里给出了别名。即使这里有很好的答案,我也添加了这个,因为它在 windows 和 linux 中有所不同

于 2013-08-15T03:14:30.570 回答
1

我的 .gitconfig 文件的 PFA 屏幕截图

具有以下别名

[alias]
    cb = checkout branch
    pullb = pull main branch
于 2018-11-23T08:48:54.657 回答
1

我在用户目录(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'

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

在 bash shell ( vim ~/.bashrc)

source ~/.profile

在 zsh shell ( vim ~/.zshrc )

source ~/.profile
于 2021-02-05T16:32:33.303 回答
0
alias s="git status"

你的食指会原谅你一生中给它带来的所有痛苦。

于 2020-10-01T17:56:22.323 回答
-1

Windows 的另一种可能性是创建一个目录,其中包含 .bat 文件,其中包含您的快捷方式。文件名是要使用的快捷方式。只需将目录添加到您的 PATH 环境变量中,您就可以在 cmd 窗口中使用所有快捷方式。

例如(gc.bat):

git commit -m %1

然后可以在控制台中执行以下命令:

gc "changed stuff"

我将其添加为答案的原因是因为使用它时您不仅限于git ...命令。

于 2017-03-23T23:50:11.310 回答