歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> GitHub秘籍 : Git 篇

GitHub秘籍 : Git 篇

日期:2017/2/28 14:25:52   编辑:Linux教程

本秘籍收錄了一些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://www.linuxidc.com/Linux/2014-11/109489.htm

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

如何在 GitHub 建立組織 http://www.linuxidc.com/Linux/2013-08/88197.htm

GitHub Linux下使用方法 http://www.linuxidc.com/Linux/2013-06/86417.htm

Windows下Eclipse搭建GitHub開發環境圖文教程 http://www.linuxidc.com/Linux/2013-06/85372.htm

前一個分支

快速檢出上一個分支:

  1. $ git checkout -
  2. # Switched to branch 'master'
  3. $ git checkout -
  4. # Switched to branch 'next'
  5. $ git checkout -
  6. # Switched to branch 'master'

進一步了解 Git 分支.

Stripspace命令

Git Stripspace命令可以:

  • 去掉行尾空白符
  • 多個空行壓縮成一行
  • 必要時在文件末尾增加一個空行

使用此命令時必須傳入一個文件,像這樣:

  1. $ git stripspace < README.md

進一步了解 Git stripspace 命令.

檢出Pull Requests

Pull Request是一種GitHub上可以通過以下多種方式在本地被檢索的特別分支:

檢索某個分支並臨時儲存在本地的FETCH_HEAD中以便快速查看更改(diff)以及合並(merge):

  1. $ git fetch origin refs/pull/[PR-Number]/head

通過refspec獲取所有的Pull Request為本地分支:

  1. $ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'

或在倉庫的.git/config中加入下列設置來自動獲取遠程倉庫中的Pull Request

  1. [remote "origin"]
  2. fetch =+refs/heads/*:refs/remotes/origin/*
  3. url = [email protected]:tiimgreen/github-cheat-sheet.git
  1. [remote "origin"]
  2. fetch =+refs/heads/*:refs/remotes/origin/*
  3. url = [email protected]:tiimgreen/github-cheat-sheet.git
  4. fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

對基於派生庫的Pull Request,可以通過先checkout代表此Pull Request的遠端分支再由此分支建立一個本地分支:

  1. $ git checkout pr/42 pr-42

進一步了解如何檢出pull request到本地.

提交空改動 :trollface:

可以使用--allow-empty選項強制創建一個沒有任何改動的提交:

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

這樣做在如下幾種情況下是有意義的:

  • 標記一批工作或一個新功能的開始。
  • 記錄你對項目進行了跟代碼無關的改動。
  • 跟使用你倉庫的其他人交流。
  • 作為倉庫的第一次提交,因為第一次提交日後是不能被rebase的: git commit -m "init repo" --allow-empty.

更直觀的Git Status

在命令行輸入如下命令:

  1. $ git status

可以看到:

加上-sb選項:

  1. $ git status -sb

這回得到:

進一步了解 Git status 命令.

更直觀的Git Log

輸入如下命令:

  1. $ 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查詢運行你在之前的所有提交信息裡進行搜索,找到其中和搜索條件相匹配的最近的一條。

  1. $ git show :/query

這裡 query (區別大小寫)是你想要搜索的詞語, 這條命令會找到包含這個詞語的最後那個提交並顯示變動詳情。

  1. $ git show :/typo

  • 按 q 鍵退出命令。*

合並分支

輸入命令:

  1. $ git branch --merged

這會顯示所有已經合並到你當前分支的分支列表。

相反地:

  1. $ git branch --no-merged

會顯示所有還沒有合並到你當前分支的分支列表。

進一步了解 Git branch 命令.

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-11/109490p2.htm

Copyright © Linux教程網 All Rights Reserved