Git 常用命令列清單
$ mkdir learngit//建立資料夾 $ cd learngit//進入 $ pwd//檢視當前目錄 $ git init//把該目錄變為git可管理的目錄(初始化) $ vi 檔名//編輯內容 $ git add 檔名1//把檔案1新增到倉庫 $ git add 檔名2//把檔案2新增到倉庫 $ git commit -m"提交說明" //把檔案提交到倉庫 $ git status//檢視倉庫當前狀態 $ git diff 檔名//檢視不同 $ git log//從最近到最遠的提交日誌 $ git log --pretty=oneline//提交日誌簡單顯示 $ git reset --hard HEAD^ //回退到上一版本 $ cat檔名//檢視檔案內容 $ git reset --hard 1094a//回退到指定版本 $ git reflog//記錄每一次命令 $ git diff HEAD -- readme.txt //檢視工作區和版本庫裡面最新版本的區別 $ git checkout -- readme.txt //丟棄工作區的修改(恢復工作區) $ git reset HEAD readme.txt //把暫存區的修改撤銷掉(unstage),重新放回工作區 $ rm test.txt//在檔案管理器中把沒用的檔案刪了 $ git rm test.txt//從版本庫中刪除該檔案 遠端倉庫 $ ssh-keygen -t rsa -C "[email protected]" //建立SSH Key $ git remote add origin [email protected]:sunjunzhou/learngit.git //本地倉庫與遠端倉庫關聯 $ git push -u origin master //第一次把本地庫的所有內容推送到遠端庫上 $ git push origin master//把本地master分支的最新修改推送至GitHub $ git clone [email protected]:sunjunzhou/gitskills.git //克隆一個本地庫 $ git branch//檢視分支 $ git branch <name>//建立分支 $ git checkout <name>//切換分支 $ git checkout -b <name> //建立+切換分支 $ git merge <name>//合併某分支到當前分支 $ git branch -d <name> //刪除分支 $ git log --graph --pretty=oneline --abbrev-commit //分支的合併情況 $ git merge --no-ff -m "merge with no-ff" dev //合併分支禁用Fast forward $ git stash//把當前工作現場“儲藏”起來 $ git stash list//檢視工作現場列表 $ git stash apply//恢復現場 $ git stash drop//刪除現場 $ git stash pop//恢復的同時把stash內容也刪了 $ git stash apply stash@{0}//恢復指定的stash $ git branch -d feature-vulcan //刪除指定分支 $ git branch -D <name>//丟棄一個沒有被合併過的分支,強行刪除 $ git remote//檢視遠端庫的資訊 $ git remote -v//顯示更詳細的資訊 $ git push origin master //推送分支,就是把該分支上的所有本地提交推送到遠端庫,推送時要指定本地分支 $ git checkout -b dev origin/dev//建立遠端origin的dev分支到本地 $ git pull//把最新的提交從origin/dev抓下來 $ git branch --set-upstream-to=origin/dev dev //設定dev和origin/dev的連結 $ git rebase //把分叉的提交歷史“整理”成一條直線,看上去更直觀。 缺點是本地的分叉提交已經被修改過了。 $ git tag v1.0//當前分支打標籤 $ git tag//檢視所有標籤 $ git log --pretty=oneline --abbrev-commit $ git tag v0.9 f52c633//指定提交打標籤 $ git show v0.9//檢視標籤資訊 $ git tag -a v0.1 -m "version 0.1 released" 1094adb //建立帶有說明的標籤,用-a指定標籤名,-m指定說明文字 $ git tag -d v0.1//刪除標籤 $ git push origin v1.0//推送某個標籤到遠端 $ git push origin --tags//一次性推送全部尚未推送到遠端的本地標籤 $ git tag -d v0.9//如果標籤已經推送到遠端,要刪除遠端標籤就先從本地刪除 $ git push origin :refs/tags/v0.9 //然後,從遠端刪除。 $ git config --global color.ui true //讓Git顯示顏色,會讓命令輸出看起來更醒目
提交過程status變化
$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified:readme.txt no changes added to commit (use "git add" and/or "git commit -a") $ git add readme.txt $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified:readme.txt $ git commit -m "add distributed" $ git status On branch master nothing to commit, working tree clean
檢視提交日誌記錄
$ git log commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) Author: Michael Liao <[email protected]> Date:Fri May 18 21:06:15 2018 +0800 append GPL commit e475afc93c209a690c39c13a46716e8fa000c366 Author: Michael Liao <[email protected]> Date:Fri May 18 21:03:36 2018 +0800 add distributed commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 Author: Michael Liao <[email protected]> Date:Fri May 18 20:59:18 2018 +0800 wrote a readme file $ git log --pretty=oneline 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL e475afc93c209a690c39c13a46716e8fa000c366 add distributed eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
版本庫(Repository)
工作區有一個隱藏目錄.git,這個不算工作區,而是Git的版本庫。Git的版本庫裡存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區,還有Git為我們自動建立的第一個分支master,以及指向master的一個指標叫HEAD。

第一次修改 -> git add
-> 第二次修改 -> git add
-> git commit
命令 git checkout -- readme.txt
意思就是,把 readme.txt
檔案在工作區的修改全部撤銷,這裡有兩種情況:
一種是 readme.txt
自修改後還沒有被放到暫存區,現在,撤銷修改就回到和版本庫一模一樣的狀態;
一種是 readme.txt
已經新增到暫存區後,又作了修改,現在,撤銷修改就回到新增到暫存區後的狀態。
總之,就是讓這個檔案回到最近一次 git commit
或 git add
時的狀態。
撤銷修改
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified:readme.txt $ git reset HEAD readme.txt Unstaged changes after reset: Mreadme.txt $ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified:readme.txt $ git checkout -- readme.txt $ git status On branch master nothing to commit, working tree clean
刪除檔案
$ rm test.txt $ git status On branch master Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted:test.txt no changes added to commit (use "git add" and/or "git commit -a") $ git rm test.txt rm 'test.txt' $ git commit -m "remove test.txt" [master d46f35e] remove test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt //誤刪時 $ git checkout -- test.txt git checkout其實是用版本庫裡的版本替換工作區的版本,無論工作區是修改還是刪除,都可以“一鍵還原”。
遠端倉庫:
第1步:建立SSH Key。在使用者主目錄下,看看有沒有.ssh目錄,如果有,再看看這個目錄下有沒有 id_rsa
和 id_rsa.pub
這兩個檔案,如果已經有了,可直接跳到下一步。如果沒有,開啟Shell(Windows下開啟Git Bash),建立SSH Key:
$ ssh-keygen -t rsa -C "[email protected]"
你需要把郵件地址換成你自己的郵件地址,一路回車,使用預設值即可,由於這個Key也不是用於軍事目的,所以也無需設定密碼。如果一切順利的話,可以在使用者主目錄裡找到 .ssh
目錄,裡面有 id_rsa
和 id_rsa.pub
兩個檔案,這兩個就是SSH Key的祕鑰對, id_rsa
是私鑰,不能洩露出去, id_rsa.pub
是公鑰,可以放心地告訴任何人。
第2步:登陸GitHub,開啟“Account settings”,“SSH Keys”頁面:
然後,點“Add SSH Key”,填上任意Title,在Key文字框裡貼上 id_rsa.pub
檔案的內容:
點“Add Key”,你就應該看到已經新增的Key:
為什麼GitHub需要SSH Key呢?因為GitHub需要識別出你推送的提交確實是你推送的,而不是別人冒充的,而Git支援SSH協議,所以,GitHub只要知道了你的公鑰,就可以確認只有你自己才能推送。當然,GitHub允許你新增多個Key。假定你有若干電腦,你一會兒在公司提交,一會兒在家裡提交,只要把每臺電腦的Key都新增到GitHub,就可以在每臺電腦上往GitHub推送了。
把本地庫的內容推送到遠端,用git push命令,實際上是把當前分支master推送到遠端。由於遠端庫是空的,我們第一次推送master分支時,加上了-u引數,Git不但會把本地的master分支內容推送的遠端新的master分支,還會把本地的master分支和遠端的master分支關聯起來,在以後的推送或者拉取時就可以簡化命令。
SSH警告
當你第一次使用Git的 clone
或者 push
命令連線GitHub時,會得到一個警告:
The authenticity of host 'github.com (xx.xx.xx.xx)' can't be established. RSA key fingerprint is xx.xx.xx.xx.xx. Are you sure you want to continue connecting (yes/no)?
這是因為Git使用SSH連線,而SSH連線在第一次驗證GitHub伺服器的Key時,需要你確認GitHub的Key的指紋資訊是否真的來自GitHub的伺服器,輸入 yes
回車即可。Git會輸出一個警告,告訴你已經把GitHub的Key新增到本機的一個信任列表裡了:
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
這個警告只會出現一次,後面的操作就不會有任何警告了。
如果你實在擔心有人冒充GitHub伺服器,輸入 yes
前可以對照 ofollow,noindex">GitHub的RSA Key的指紋資訊 是否與SSH連線給出的一致。
git clone
你也許還注意到,GitHub給出的地址不止一個,還可以用 https://github.com/michaelliao/gitskills.git
這樣的地址。實際上,Git支援多種協議,預設的 git://
使用ssh,但也可以使用 https
等其他協議。
使用 https
除了速度慢以外,還有個最大的麻煩是每次推送都必須輸入口令,但是在某些只開放http埠的公司內部就無法使用 ssh
協議而只能用 https
。
分支策略
在實際開發中,我們應該按照幾個基本原則進行分支管理:
首先, master
分支應該是非常穩定的,也就是僅用來發布新版本,平時不能在上面幹活;
那在哪幹活呢?幹活都在 dev
分支上,也就是說, dev
分支是不穩定的,到某個時候,比如1.0版本釋出時,再把 dev
分支合併到 master
上,在 master
分支釋出1.0版本;
你和你的小夥伴們每個人都在 dev
分支上幹活,每個人都有自己的分支,時不時地往 dev
分支上合併就可以了。
所以,團隊合作的分支看起來就像這樣:

推送分支
並不是一定要把本地分支往遠端推送,那麼,哪些分支需要推送,哪些不需要呢?
master dev
總之,就是在Git中,分支完全可以在本地自己藏著玩,是否推送,視你的心情而定
多人協作的工作模式通常是這樣:
git push origin <branch-name> git pull git push origin <branch-name>
如果 git pull
提示 no tracking information
,則說明本地分支和遠端分支的連結關係沒有建立,用命令 git branch --set-upstream-to <branch-name> origin/<branch-name>
。
這就是多人協作的工作模式,一旦熟悉了,就非常簡單。
Git的官方網站: http://git-scm.com
整理自“廖雪峰的官方網站”(推薦)
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000