Git那些不为人知的秘籍以及Sao操作

355 阅读1分钟

如需要了解其他git操作:juejin.cn/post/684490…

Git配置

Git命令自定义别名

别名用来帮助你定义自己的git命令。比如你可以定义 git a 来运行 git add --all

要添加一个别名, 一种方法是打开 ~/.gitconfig 文件并添加如下内容:

[alias]
  co = checkout
  cm = commit
  p = push
  # Show verbose output about tags, branches or remotes
  tags = tag -l
  branches = branch -a
  remotes = remote -v

...或者在命令行里键入:

$ git config --global alias.new_alias git_function

例如:

$ git config --global alias.cm commit

指向多个命令的别名可以用引号来定义:

$ git config --global alias.ac 'add -A . && commit'

下面列出了一些有用的别名:

别名 Alias命令 Command如何设置 What to Type
git cmgit commitgit config --global alias.cm commit
git cogit checkoutgit config --global alias.co checkout
git acgit add . -Agit commitgit config --global alias.ac '!git add -A && git commit'
git stgit status -sbgit config --global alias.st 'status -sb'
git tagsgit tag -lgit config --global alias.tags 'tag -l'
git branchesgit branch -agit config --global alias.branches 'branch -a'
git remotesgit remote -vgit config --global alias.remotes 'remote -v'
git lggit log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset' --abbrev-commit --git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset' --abbrev-commit --"

自动更正

如果键入 git comit 你会看到如下输出:

$ git comit -m "Message"
# git: 'comit' is not a git command. See 'git --help'.

# Did you mean this?
#   commit

为了在键入 comit 调用 commit 命令,只需启用自动纠错功能:

$ git config --global help.autocorrect 1

现在你就会看到:

$ git comit -m "Message"
# WARNING: You called a Git command named 'comit', which does not exist.
# Continuing under the assumption that you meant 'commit'
# in 0.1 seconds automatically...

带颜色输出

要在你的Git命令输出里加上颜色的话,可以用如下命令:

$ git config --global color.ui 1

进一步了解 Git config 命令.

更直观的Git Status

在命令行输入如下命令:

$ git status

可以看到:

加上 -sb 选项:

$ git status -sb

这回得到:

Git资源

TitleLink
Official Git Sitegit-scm.com/
Official Git Video Tutorialsgit-scm.com/videos
Code School Try Gittry.github.com/
Introductory Reference & Tutorial for Gitgitref.org/
Official Git Tutorialgit-scm.com/docs/gittut…
Everyday Gitgit-scm.com/docs/everyd…
Git Immersiongitimmersion.com/
Ry's Git Tutorialrypress.com/tutorials/g…
Git for Designerhoth.entp.com/output/git_…
Git for Computer Scientistseagain.net/articles/gi…
Git Magicwww-cs-students.stanford.edu/~blynn/gitm…

Git参考书籍

TitleLink
Pragmatic Version Control Using Gitwww.pragprog.com/titles/tsgi…
Pro Gitgit-scm.com/book
Git Internals Peepcodepeepcode.com/products/gi…
Git in the Trenchescbx33.github.com/gitt/
Version Control with Gitwww.amazon.com/Version-Con…
Pragmatic Guide to Gitwww.pragprog.com/titles/pg_g…
Git: Version Control for Everyonewww.packtpub.com/git-version…

如大家还有什么秘籍和Sao操作可以留言给我