GitHub秘籍 : git 篇

    xiaoxiao2024-01-12  144

    GitHub秘籍 : git 篇

    本秘籍收录了一些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 分支.

    Stripspace命令

    Git Stripspace命令可以:

    去掉行尾空白符多个空行压缩成一行必要时在文件末尾增加一个空行

    使用此命令时必须传入一个文件,像这样:

    $ git stripspace < README.md

    进一步了解 Git stripspace 命令.

    检出Pull Requests

    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到本地.

    提交空改动 :trollface:

    可以使用--allow-empty选项强制创建一个没有任何改动的提交:

    $ git commit -m "Big-ass commit" --allow-empty

    这样做在如下几种情况下是有意义的:

    标记一批工作或一个新功能的开始。记录你对项目进行了跟代码无关的改动。跟使用你仓库的其他人交流。作为仓库的第一次提交,因为第一次提交日后是不能被rebase的: git commit -m "init repo" --allow-empty.

    更直观的Git Status

    在命令行输入如下命令:

    $ git status

    可以看到:

    git status

    加上-sb选项:

    $ git status -sb

    这回得到:

    git status -sb

    进一步了解 Git status 命令.

    更直观的Git Log

    输入如下命令:

    $ 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查询运行你在之前的所有提交信息里进行搜索,找到其中和搜索条件相匹配的最近的一条。

    $ git show :/query

    这里 query (区别大小写)是你想要搜索的词语, 这条命令会找到包含这个词语的最后那个提交并显示变动详情。

    $ git show :/typo

    git 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配置

    所有Git配置都保存在你的.gitconfig 文件中。

    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 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 命令.

    Git资源

    TitleLinkOfficial Git Sitehttp://git-scm.com/Official Git Video Tutorialshttp://git-scm.com/videosCode School Try Githttp://try.github.com/Introductory Reference & Tutorial for Githttp://gitref.org/Official Git Tutorialhttp://git-scm.com/docs/gittutorialEveryday Githttp://git-scm.com/docs/everydayGit Immersionhttp://gitimmersion.com/Ry's Git Tutorialhttp://rypress.com/tutorials/git/index.htmlGit for Designerhttp://hoth.entp.com/output/git_for_designers.htmlGit for Computer Scientistshttp://eagain.net/articles/git-for-computer-scientists/Git Magichttp://www-cs-students.stanford.edu/~blynn/gitmagic/

    Git参考书籍

    TitleLinkPragmatic Version Control Using Githttp://www.pragprog.com/titles/tsgit/pragmatic-version-control-using-gitPro Githttp://git-scm.com/bookGit Internals Peepcodehttp://peepcode.com/products/git-internals-pdfGit in the Trencheshttp://cbx33.github.com/gitt/Version Control with Githttp://www.amazon.com/Version-Control-Git-collaborative-development/dp/1449316387Pragmatic Guide to Githttp://www.pragprog.com/titles/pg_git/pragmatic-guide-to-gitGit: Version Control for Everyonehttp://www.packtpub.com/git-version-control-for-everyone/book 原文发布时间:2014-11-15 本文来自云栖合作伙伴“linux中国”
    最新回复(0)