Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16个不错的 git 别名 #3

Open
dwqs opened this issue Jul 30, 2016 · 0 comments
Open

16个不错的 git 别名 #3

dwqs opened this issue Jul 30, 2016 · 0 comments

Comments

@dwqs
Copy link
Owner

dwqs commented Jul 30, 2016

git 是一个非常棒的源代码管理工具,它的使用已经完全整合到开发的工作流当中;同时,git 还是一个 review/OTAP/deployment 工具。

当在 CLI 中使用 git 时,有时必须输入很多比较长的命令来完成一些事情。尽管大部分 git 命令是很简单的,但是也有一些非常复杂并难以输入的命令。而开发人员是比较懒的,并会尽可能的少输入命令。

因为上述情况很符合我,所以对于我经常使用的 git 命令,我会使用其别名来代替,并会改进命令的返回信息。在这篇文章中,我会介绍一些简单但非常有用的 git 别名。

简化常用的 git 命令的别名

一些 git 别名非常简单,用这些别名替换原始的 git 命令也意味着减少你的输入,这有一个别名列表:

co  = checkout       # Checkout a branch
cob = checkout -b    # Checkout a new not yet existing branch
f   = fetch -p       # Fetch from a repository and prune any remote-tracking references that no longer exist on the remote.
c   = commit         # Commit you changes
p   = push           # Push you changes to a remote
ba  = branch -a      # List both remote-tracking branches and local branches.
bd  = branch -d      # Delete a branch only if it has been merged
bD  = branch -D      # Force delete a branch
dc  = diff --cached  # Display the staged changes

查看 git 的提交状态

查看当前工作目录的提交状态是大多数开发者的一项日常工作。我们会分段查看自己对哪些文件做了改变,并判断我们是否改变了原本不打算改变的文件。

将命令简化,并让其显示更多的信息:

st = status -sb

例如:
st-example

在 patches 中呈现 git 改变

我喜欢在添加改变之前进行 review,或许你也这么做。git diff 是能实现这个需求的一个不错的工具。但如果你只想添加你改变的那部分,或者每次只想 review 一小部分,你可以将你改变的那部分分多次提交。

在这种情况下,git add -p 能解决你的需求:

a = add -p

例如:
a-example

更有用的 git 日志

git 日志能帮助我们查看在工作目录下对代码做了哪些改变,日志不仅记录了代码的提交历史,而且会记录分支的合并情况。当我们 输入规范的提交信息 时,git 日志就成为一个查看代码变更和为什么变更的重要工具。

改善 git 日志的输出能快速定位这些改变:

plog = log --graph --pretty='format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%aN%C(reset) %s'

例如:
log-example

lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

例如:
lg-example

tlog = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative

例如:
t-log

按照合并数量给贡献者排序

当你对某个 project 有问题并想找一个人说明时,怎么找到这个 project 的第一贡献者就非常有必要了。

rank = shortlog -sn --no-merges

例如:
rank-example

删除所有已合并的分支

在提交 PR 之前,我们需要创建一个本地分支来提交代码,时间久了之后,你就会发现有很多已经合并到 master 上的分支,一个一个删除这些分支是比较麻烦的事。

当这些已经合并的分支的数量很大时,用一个简单的别名来删除这些分时是很方便的。这个别名会删除所有已经合并到你当前所在分支的分支:

bdm = "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d"

例如:
bdm-example

[alias]
    # Shortening aliases
    co = checkout
    cob = checkout -b
    f = fetch -p
    c = commit
    p = push
    ba = branch -a
    bd = branch -d
    bD = branch -D
    dc = diff --cached

    # Feature improving aliases
    st = status -sb
    a = add -p

    # Complex aliases
    plog = log --graph --pretty='format:%C(red)%d%C(reset) %C(yellow)%h%C(reset) %ar %C(green)%aN%C(reset) %s'
    tlog = log --stat --since='1 Day Ago' --graph --pretty=oneline --abbrev-commit --date=relative
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    rank = shortlog -sn --no-merges
    bdm = "!git branch --merged | grep -v '*' | xargs -n 1 git branch -d"

相关文章

原文:16 awesome git aliases that you will love

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant