我看到了一个截屏视频,有人得到了
git st
git ci
去工作。当我这样做时,我收到一个错误,询问我是否还有其他意思。
作为一个 git newb,我需要知道你必须做什么才能完成这项工作?
基本上你只需要添加行~/.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 命令甚至接受函数作为参数。看看别名。
正如其他人所说,添加 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'
我觉得最有用的 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
你需要git config alias
命令。在 Git 存储库中执行以下命令:
git config alias.ci commit
对于全局别名:
git config --global alias.ci commit
这对我有用:
bco = "!f(){ git branch ${1} && git checkout ${1}; };f"
在:
$ git --version
git version 1.7.7.5 (Apple Git-26)
以下是您用来节省时间的 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
对我来说(我正在使用带有终端的 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"
dog
我创建了显示日志图的别名:
git config --global alias.dog "log --all --decorate --oneline --graph"
并按如下方式使用它:
git dog
这将创建一个别名st
:status
git config --add alias.st status
对于那些希望在 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>
将以下行添加到您的主目录中的 ~/.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
例如。这同样适用于别名标题下的其他命令。
如果您使用“!”,您还可以链接命令 生成外壳的运算符:
aa = !git add -A && git status
这将添加所有文件并为您提供状态报告$ git aa
。
为了方便地检查您的别名,请添加此别名:
alias = config --get-regexp ^alias\\.
然后快速$ git alias
为您提供您当前的别名以及它们的作用。
您可以为 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
...
$ 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 = [最新] 待办事项 -> 来源/待办事项 已经是最新的了。
一条线设置
$ 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 --
希望这更快。
您可以使用 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
只是为了让别名比其他答案中提到的标准 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
以及第二个参数。
如果您想要替代该~/.gitconfig
选项并愿意进一步挖掘,另一种选择是通过将它们包装在全局节点包中来编写完全自定义的 git 命令。
在您的 package.json 中,您将定义根命令(例如:),gt
然后过滤特定命令以执行正确的 git 命令。例如,git checkout my-branch
可能是gt co mybranch
。
npm 上的“christian-git”包使用这种方法:https ://github.com/alexmacarthur/christian-git
.gitconfig
我建议为您的别名使用.gitconfig
包含。一旦你开始创建别名,你可能最终会得到很多。它们很可能是您想与他人分享的东西。将它们放在专用文件中可以轻松共享。您的团队甚至可以使用 git 存储库来保存共享别名。当然还有一些您不想共享的别名,因此请将它们保存在私有别名文件中。
[include]
path=src/dotfiles/.gitaliases
[include]
path=src/team-utils/gitaliases
[include]
path=.gitaliases.private
要在 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
这里给出了别名。即使这里有很好的答案,我也添加了这个,因为它在 windows 和 linux 中有所不同
我在用户目录(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
alias s="git status"
你的食指会原谅你一生中给它带来的所有痛苦。
Windows 的另一种可能性是创建一个目录,其中包含 .bat 文件,其中包含您的快捷方式。文件名是要使用的快捷方式。只需将目录添加到您的 PATH 环境变量中,您就可以在 cmd 窗口中使用所有快捷方式。
例如(gc.bat):
git commit -m %1
然后可以在控制台中执行以下命令:
gc "changed stuff"
我将其添加为答案的原因是因为使用它时您不仅限于git ...
命令。