1. 程式人生 > >GIT Cheat Sheet

GIT Cheat Sheet

http://files.cnblogs.com/lbsx/git-cheat-sheet-large.png.zip

一:綜述

    網路上GIT的教材不計其數,CheatSheet也很多。但其編輯思路都是對命令的講解。一直想要這樣一篇CheatSheet:日常使用GIT中,我會遇到哪些Use Case,這些Use Case的優先權是什麼,應該用什麼GIT命令。出於這個思路,編輯此文.

    本文的目標讀者是瞭解GIT基本概念,但使用GIT還不久 (半年以下),記不得如此多命令,該做事情時不知道該用哪個命令,需要一本速查表的使用者。

二:Use Case及命令

所有Use Case按照priority分類排列,但和branch相干的Use Case單列

級別  用例  命令及樣例  註釋
 P0  複製遠端Repository git clone git://git.xxx.com/xxx/xxx.git  也就是下載程式碼的意思
 P0  下載遠端Repository的更新 git fetch 和其他版本管理工具不同,git 中下載的程式碼在workspace中是看不見的,要merge或rebase後才看的見

P0

 將已經下載的遠端repository的更新和本地更新合併 git rebase 或 git merge  建議使用git rebase,因為這樣程式碼歷史記錄是線性的看的清楚。只有一些特殊情況建議用git merge,比如發生了程式碼衝突,並且你暫時沒有計劃向遠端repository上傳程式碼
 P0 同時執行下載遠端更新及本地合併 git pull 或 git pull --rebase  git pull  = git fetch + git merge
 git pull -- rebase = git fetch + git rebase
 P0  Staging  新建或修改後的程式碼 git add  git add . 可以staging本目錄及子目錄下所有改動
 P0  提交程式碼到本地repository git commit  -m ""  和其他版本管理工具不同,git 中commit的程式碼只在本地提交,別人是看不見的,要再執行git push後別人才看的見
 P0  上傳本地repository的程式碼到遠端repository git push  一般省略引數,亦即推送本branch的程式碼到origin repository的同名branch
 P0  解決程式碼合併衝突  建議在 Tortoise Git 或 EGit plugin等工具的圖形介面解決,命令列比較複雜
 P0 git 常用全域性配置 git config --global user.name 'xxxxx'
 git config --global user.email 'xxxxx'
 git config --global core.autocrlf input
 git config --global push.default upstream
 git config --global branch.autosetuprebase always
 git config --global core.editor vi
 git config --global credential.helper cache
詳盡清單請參閱 https://git-scm.com/docs/git-config

如果你想在不同的專案用不同的使用者名稱和郵箱,去掉 --global選項

In Windows platform, use 
git config --global credential.helper wincred
 P1  通知GIT準備刪除檔案 git rm
 P1  撤銷本地的改動1 git reset [--hard] <檔名> 撤銷本地檔案的staging狀態,變成modified狀態,但程式碼改變仍保留在工作區
 P1  撤銷本地的改動2 git checkout --force 撤銷本地檔案的staging狀態,並撤銷程式碼在工作區的改變。
 P1  撤銷本地的改動3 git clean -dn  刪除本地untraced file。結合git checkout --force,完美撤銷本地全部改動
 P1  檢視所有更改的程式碼(檔案級別) git status
 P1  檢視所有更改的程式碼(程式碼行級別)  git diff  git diff 的結果是標準的diff檔案格式
 P2  檢視程式碼更改歷史記錄  git log
 P2  檢視各分支HEAD的歷史記錄  git reflog  git reflog可以看見被刪除的commit,而git log 不能
 P2  配置文字檔案回車換行轉換模式  git config core.autocrlf=  僅在windows平臺下需要配置,Linux平臺無需配置。windows平臺建議false(需要一個支援linux格式文字的編輯器)
 P3 設定某檔案不提交git repository  編輯.gitignore檔案  注意:已經在repository的檔案會忽略.gitignore檔案
 P3 如果某檔案已經在git repository,通知git強行忽略對該檔案的修改   git update-index --assume-unchanged
 P3  通知git恢復跟蹤對該檔案的修改   git update-index --no-assume-unchanged
 P3  設定pull預設用 rebase 方式  修改 .git/config, 增加 rebase = true   或修改 ~/.gitconfig 增加 autosetuprebase = always
 P3  檢視歷史上某次提交的細節  git show
 P3  建立新的空的repository  git init --bare
 P3  打標籤  git tag
 P3 檢視某行程式碼的最後作者(即blame)  git blame <-L linea,lineb>
 P4  二分法定位引入錯誤的commit  git bisect

 一般用法

git bisect start
git bisect
git bisect good

 Br  列出所有的本地branch  git branch  "master" branch為主branch, 標*的為當前branch
 Br  列出所有的本地和遠端branch  git branch -a
 Br  切換到某branch開發  git checkout 一般是切換到本地branch
 Br  建立遠端branch  三步
git branch
git checkout
git push origin
 Br  基於遠端branch建立本地branch  git checkout  本命令其實是 git checkout -b --track /的縮寫
 Br  刪除遠端branch  git push origin :
 Br  合併分支  git merge
 Br  檢視本分支的直接上游分支  git branch --merged
 Br  檢視兩個branch之間的變動  git show branchA..branchB
 Br  檢視兩個branch之間的變動2  git whatchanged
 Br  對於https協議的遠端repository,快取密碼 git config [--global] credential.helper cache
 Br  從一個branch中挑一個commit合併到本branch  git cherry-pick

三:工作組通常的程式碼修改流程(簡單提交模型)

1. 編輯原始碼
2. 建議經常執行 git add 和 git commit 提交程式碼(一天通常提交幾次到十幾次)
3. 手動測試已經提交的程式碼,直到測試通過。
4. 執行 git pull. 如果有程式碼衝突,解決程式碼衝突,解決完畢再次執行git pull
5. 提交reviewboard,並且根據反饋修改程式碼。
6. 執行pre-commit hudson task,堅持沒有break build.
7. 再次執行git pull,確認在第5到第6步執行階段沒有新的程式碼衝突產生。
8. 執行 git push,完成程式碼的上傳。

四:標識一個或多個revision

 1. 使用revision的SHA-1值,例 734713bc047d87bf7eac9674765ae793478c50d3
 2. 使用review的SHA-1的一部分,前提是沒有歧義  例 724713
 3. HEAD 是一個指向你當前所在分支的引用識別符號
 4. HEAD 在五次前的值    [email protected]{5}
 5. HEAD在兩個月前的值   [email protected]{2.months.ago}
 6. HEAD的父提交             HEAD^ 或 HEAD~  
 7. HEAD的乾爹提交       HEAD^2   //HEAD由A和B merge而成,當時的工作branch為 A上,則HEAD^為A,HEAD^2為B
 8. HEAD的親爺爺             HEAD~2
 9. 所有在feather上但不在master上的提交         master..feather
 10. 所有在feather或master上之中一個branch存在而另一個branch不存在的提交   master...feather
 11. refspec        在.git/config中有一行 fetch = +refs/heads/*:refs/remotes/origin/*

  * +告訴 Git 在即使不能快速演進的情況下,也去強制更新它
  * 在遠端提取 refs/heads下所有內容,寫入本地的refs/remotes/origin/
  * 這時,以下三個命令是一致的

  * $ git log origin/master
  * $ git log remotes/origin/master
  * $ git log refs/remotes/origin/master

相關推薦

Git Cheat Sheet 中英譯(十九)

一、常用命令: 1.建立 ①  克隆現有的儲存庫 :Clone an existing repository 1 | $ git clone ssh://[email protected]/repo.git ②  建立一個新的本地倉庫 :Create

Git-cheat-sheet

Configure tooling git config --global user.name 'name' git config --global user.email 'email' git config --global color.ui auto Create repositories gi

GIT Cheat Sheet

http://files.cnblogs.com/lbsx/git-cheat-sheet-large.png.zip 一:綜述     網路上GIT的教材不計其數,CheatSheet也很多。但其編輯思路都是對命令的講解。一直想要這樣一篇CheatSheet:日常

Keras cheat sheet(Keras 速查手冊)

heat 打開 sset mage .com pdf .cn log amazon 轉自:https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Keras_Cheat_Sheet_Python.pdf 右擊在新標

MySQL SQL Injection Cheat Sheet

index rem not passwd tinc comm cas ret loading MySQL SQL Injection Cheat Sheet Some useful syntax reminders for SQL Injection into MySQ

Cheat sheet for Jupyter Notebook

cdi ref ax1 cdc rec 公眾 str 部分 ros 近期,DataCamp發布了jupyter notebook的 cheat sheet,【Python數據之道】第一時間與大家一起來分享下該cheat sheet的內容。 以下是該cheat

Web前端開發必備手冊(Cheat sheet

mysql .html cheat AC devel 表達式 則表達式 develop b前端開發 轉自:http://blog.bingo929.com/cheat-sheets-for-web-develop.html Cheat sheet這個詞組如果直譯成中

大O表示法演算法複雜度速查表(Big-O Algorithm Complexity Cheat Sheet)

原文網址:http://bigocheatsheet.com/ Word文件下載:http://download.csdn.net/detail/anshan1984/5583399     Searching(搜尋演算法) Algo

Flutter — BoxDecoration Cheat Sheet

Flutter — BoxDecoration Cheat Sheet https://medium.com/jlouage/flutter-boxdecoration-cheat-sheet-72cedaa1ba20 The BoxDecoration class provide

Linux server quick cheat sheet

系統管理:  shell命令優先查詢順序: ./   /usr/local/bin  /usr/bin  /bin  /sbin 恢復模式:開機時按cms+R  ps /

Linux Shell Cheat Sheet

1. 檢視Linux作業系統資訊: uname -a cat /proc/version lsb_release -a   2. 設定ls顯示的資料夾的顏色(將下面這條目錄加在 .bashrc 檔案最後): LS_COLORS=$LS_COLORS:'di=0;35

Linear algebra cheat sheet for deep learning

Linear algebra cheat sheet for deep learningBeginner’s guide to commonly used operationsDuring Jeremy Howard’s excellent deep learning course I realized I

How to become a machine learning engineer: A cheat sheet

Machine learning engineers--i.e., advanced programmers who develop artificial intelligence (AI) machines and systems that can learn and apply knowledge--ar

Deep Learning Performance Cheat Sheet

Deep Learning Performance Cheat SheetSimple and complex tricks that can help you boost your deep learning models accuracyThe question that I get the most f

random Beasts and Where to Find Them: A Cheat Sheet for ES6 & Python 3

I love generative art, and I frequently use a pseudo-random function to add a bit of noise to an image or behavior, but every time I implement a pseudo-ran

【scikit-learn algorithm cheat sheet】【漢化版】scikit-learn演算法選擇路徑圖

英文原版連結:http://scikit-learn.org/stable/tutorial/machine_learning_map/ 看得有點累,做了個漢化版 漢化版: (有很多名詞合理地強行翻譯了一下,否則全英文的詞彙太多,不通順的時候,請看看下面的原版) 英文原

15 Statistical Hypothesis Tests in Python (Cheat Sheet)

Tweet Share Share Google Plus Quick-reference guide to the 15 statistical hypothesis tests that

pandas庫隨筆——Cheat Sheet與資料讀取

由於自己是從R轉Python,平時用慣了R對資料進行處理、轉化等操作,如今過渡到Python上還有些許的不適應。但還是不得不感嘆pandas的強大之處,一個庫頂的上R中數個包合併在一起使用。 因為平時的使用比較雜,都是隨用隨查,所以在pandas庫的使用上也是

5 個最好的 Vim 速查卡 (Cheat Sheet)

Vim(Vi Improved) 早已替代了 Vi, 它存在於大多數的 Linux 發行版中。所以基本上 Vi 和 Vim 在你的系統中就是同一個程式,我用的 Mac, vi 命令就是一個指向到 vim 的連結ls -l $(which vi)lrwxr-xr-x 1 root wheel 3 Sep 20

python cheat sheet

__slots__=(),限制可動態繫結的屬性 def __str__(self),print的列印方式 def __iter__(self),def next(self),迴圈呼叫iter返回迭代