本秘籍收录了一些Git和Github非常酷同时又少有人知的功能。灵感来自于Zach Holman在2012年Aloha Ruby Conference和2013年WDCNZ上所做的演讲:Git and GitHub Secrets(slides)和More Git and GitHub Secrets(slides)。
Read this in other languages: English, 한국어, 日本語, 简体中文.
前一部分请看:http://linux.cn/article-4229-1.html
快速检出上一个分支:
$ git checkout - # Switched to branch 'master' $ git checkout - # Switched to branch 'next' $ git checkout - # Switched to branch 'master'进一步了解 Git 分支.
Git Stripspace命令可以:
去掉行尾空白符多个空行压缩成一行必要时在文件末尾增加一个空行使用此命令时必须传入一个文件,像这样:
$ git stripspace < README.md进一步了解 Git stripspace 命令.
Pull Request是一种GitHub上可以通过以下多种方式在本地被检索的特别分支:
检索某个分支并临时储存在本地的FETCH_HEAD中以便快速查看更改(diff)以及合并(merge):
$ git fetch origin refs/pull/[PR-Number]/head通过refspec获取所有的Pull Request为本地分支:
$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'或在仓库的.git/config中加入下列设置来自动获取远程仓库中的Pull Request
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:tiimgreen/github-cheat-sheet.git [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:tiimgreen/github-cheat-sheet.git fetch = +refs/pull/*/head:refs/remotes/origin/pr/*对基于派生库的Pull Request,可以通过先checkout代表此Pull Request的远端分支再由此分支建立一个本地分支:
$ git checkout pr/42 pr-42进一步了解如何检出pull request到本地.
可以使用--allow-empty选项强制创建一个没有任何改动的提交:
$ git commit -m "Big-ass commit" --allow-empty这样做在如下几种情况下是有意义的:
标记一批工作或一个新功能的开始。记录你对项目进行了跟代码无关的改动。跟使用你仓库的其他人交流。作为仓库的第一次提交,因为第一次提交日后是不能被rebase的: git commit -m "init repo" --allow-empty.在命令行输入如下命令:
$ git status可以看到:
git status
加上-sb选项:
$ git status -sb这回得到:
git status -sb
进一步了解 Git status 命令.
输入如下命令:
$ git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative可以看到:
git log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
这要归功于Palesz在stackoverflow的回答。
这个命令可以被用作别名,详细做法见这里。
进一步了解 Git log 命令.
Git查询运行你在之前的所有提交信息里进行搜索,找到其中和搜索条件相匹配的最近的一条。
$ git show :/query这里 query (区别大小写)是你想要搜索的词语, 这条命令会找到包含这个词语的最后那个提交并显示变动详情。
$ git show :/typogit show :/query
按 q 键退出命令。*输入命令:
$ git branch --merged这会显示所有已经合并到你当前分支的分支列表。
相反地:
$ git branch --no-merged会显示所有还没有合并到你当前分支的分支列表。
进一步了解 Git branch 命令.
使用Git的 instaweb 可以立即在 gitweb中浏览你的工作仓库。这个命令是个简单的脚步,配置了gitweb和用来浏览本地仓库的Web服务器。(译者注:默认需要lighttpd支持)
$ git instaweb执行后打开:
Git instaweb
进一步了解 Git instaweb 命令.
所有Git配置都保存在你的.gitconfig 文件中。
别名用来帮助你定义自己的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 Typegit cmgit commitgit config --global alias.cm commitgit cogit checkoutgit config --global alias.co checkoutgit acgit add . -A git 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)<%an>%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)<%an>%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 命令.