git基本命令
- 查看當(dāng)前文件的狀態(tài):git status
- 從工作區(qū)提交到暫存區(qū):git add 文件名
- 將暫存區(qū)的文件提交到倉(cāng)庫(kù):git commit -m "提交描述"
初始化git
當(dāng)git第一次安裝時(shí)需要初始化用戶名和郵箱
- 設(shè)置用戶名:
git config --global user.name '用戶名' - 設(shè)置用戶名郵箱
git config --global user.email '郵箱地址'
該設(shè)置在github倉(cāng)庫(kù)主頁(yè)顯示誰(shuí)提交了該文件
- 查看設(shè)置
git config -list
初始化一個(gè)新倉(cāng)庫(kù)
- mkdir 文件夾名
- 在文件內(nèi)初始化git
- 進(jìn)入到文件夾內(nèi):cd 文件夾名
- 初始化: git init
如此倉(cāng)庫(kù)即在test里了
向倉(cāng)庫(kù)中添加文件
- 將文件從工作區(qū)添加到暫存區(qū)
刪除倉(cāng)庫(kù)文件
- 刪除工作目錄:rm -f 文件名
- 從git中刪除文件 git rm 文件名
- 提交修改:git commit -m '描述'
git分支
分支的創(chuàng)建查看及切換
當(dāng)使用git init的時(shí)候會(huì)初始化一個(gè)分支為master分支,但是這個(gè)分支只有提交一次之后才會(huì)生效,所以必須先寫點(diǎn)什么東西然后在提交,如果是空提交是不行的比如下面
$ git commit -m 'init commit'
On branch master
Initial commit
nothing to commit
此時(shí)master分支并沒有生效,可以使用git branch命令查看所有分支
$ git branch
* master
只有看到這樣的結(jié)果才說明master分支生效了。*代表當(dāng)前分支
創(chuàng)建分支和查看分支使用的是一個(gè)命令:
$ git branch test_branch #創(chuàng)建分支
$ git branch #查看分支
* master
test_branch
切換分支:
通過git checkout 分支名稱切換分支
$ git checkout test_branch
Switched to branch 'test_branch'
不同的分支在沒有合并之前是獨(dú)立的。
分支的重要操作
刪除分支
git branch -d 分支名稱
git不允許刪除當(dāng)前所處的分支
如果要?jiǎng)h除的分支并沒有完全被合并那么要?jiǎng)h除就需要使用 -D選項(xiàng)
git branch -D 分支名稱
創(chuàng)建分支并切換的簡(jiǎn)寫命令為:
git checkout -b 分支名
使用git checkout -可以回退切換分支,例如:
$ git checkout -b test_branch2
Switched to a new branch 'test_branch2'
#在使用git checkout - 即可回退到之前所處的分支
$ git checkout -
Switched to branch 'master'
使用git merge 分支名可以將指定的分支合并到當(dāng)前分支上
例如我在master分支上創(chuàng)建了一個(gè)文件名叫test_branch.txt內(nèi)容為:
this is master test_branch i want to merge test_branch2 branch file
然后再創(chuàng)建了一個(gè)叫test_branch2的分支在此分支上創(chuàng)建一個(gè)相同名稱的test_branch.txt內(nèi)容為:
this is test_branch2 branch
此時(shí)切換回master分支使用git merge test_branch2
$ git merge test_branch2 #合并分支
$ cat test_branch.txt #輸出master分支的文件內(nèi)容
this is master test_branch i want to merge test_branch2 branch file
this is test_branch2 branch
如果此時(shí)發(fā)生沖突,需要手動(dòng)解決然后用 git add 沖突的文件名告訴git沖突已經(jīng)解決了然乎使用git commit完成最終合并
分支中的HEAD
使用
git reset HEAD 文件名
可以將已經(jīng)在暫存區(qū)中的文件挪出來即從暫存區(qū)刪除恢復(fù)原來的狀態(tài)
在只有一個(gè)分支的情況下HEAD指向的是當(dāng)前分支而master指向提交

如果此時(shí)在master分支上在創(chuàng)建一個(gè)dev分支并切換到dev分支上

如果此時(shí)在dev分支上做一次提交那么狀態(tài)又如下所示:

總結(jié)而言就是HEAD是指向當(dāng)前分支的,而當(dāng)前分支指向提交
如果將dev分支合并到master在沒有任何沖突的情況下的狀態(tài)如下圖:

產(chǎn)生沖突后分支合并沖突:

分支進(jìn)階與版本回退
fast-forward
如果可能,合并分支時(shí)Git會(huì)使用fast-forward模式
在這種模式下,刪除分支時(shí)會(huì)丟掉分支信息
-
合并時(shí)加上--no-ff參數(shù)會(huì)禁用fast-forward,這樣會(huì)多出一個(gè)commit id
git merge --no-ff dev -
查看log
git log --graph
git add .和git commit -m的簡(jiǎn)寫形式
git commit -am '提交描述'
版本回退
回退到上一版本
git reset --hard HEAD^
# 或者
git reset --hard HEAD~1 # 1位置表示回到第幾個(gè)提交
# 或者
git reset --hard commit_id
如果想回退兩個(gè)版本
git reset --hard HEAD^^
回退之后還可以通過commitId前進(jìn),通過commitId可以回退到任何提交例如:
git reset --hard ef34 #id前幾位就可以
返回到某一個(gè)版本
#reflog記錄的是操作日志就算版本回退了所有操作的commit依然存在
git reflog
例如:
$ git reflog
ef34732 (HEAD -> master) HEAD@{0}: reset: moving to ef347
a9b0f96 HEAD@{1}: reset: moving to HEAD^^
ef34732 (HEAD -> master) HEAD@{2}: reset: moving to HEAD^
690b3a8 HEAD@{3}: commit: fouth commit
ef34732 (HEAD -> master) HEAD@{4}: commit: third commit
1fa07d2 HEAD@{5}: commit: first commit:create some test file
a9b0f96 HEAD@{6}: commit (initial): init commit
然后就可以通過cmmitId進(jìn)行版本切換
checkout進(jìn)階與stash
使用下面的命令,可以丟棄修改(未添加到暫存區(qū)的修改)
git checkout -- 文件名
checkout進(jìn)行版本切換
git checkout commitId
但是使用checkout切換版本屬于游離狀態(tài)與之前的版本切換是不一樣的。游離狀態(tài)的分支是沒有名字的只有commitId
分支改名:
git branch -m 原分支名 需要更改的名稱
例如將test_branch分支該名為test分支
git branch -m test_branch test
git stash可以將當(dāng)前分支未提交的內(nèi)容保存起來然后切換到其他分支,如果在當(dāng)前分支上做出修改沒有提交想要切換到其他分支可以使用此命令。
當(dāng)切換回來的時(shí)候使用git stash list恢復(fù)保存的內(nèi)容
例如:在dev分支寫的內(nèi)容已經(jīng)添加到暫存區(qū)了但是還沒寫完不能提交此時(shí)想要切換到其他分支
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
hello.txt
Please commit your changes or stash them before you switch branches.
Aborting
就會(huì)報(bào)錯(cuò)提示無(wú)法切換修改還沒有提交
那么就可以使用git stash命令將狀態(tài)保存起來
$ git stash
Saved working directory and index state WIP on dev: a773231 create a file
#也可以保存時(shí)輸入一下備注信息
# git stash '這是一段備注'
然后再其他分支做了一些事情之后,再切換來還原狀態(tài)
$ git checkout master
Switched to branch 'master'
# ....做事一些事情,然后切換回dev分支
$ git checkout dev
Switched to branch 'dev'
$ git stash list
stash@{0}: WIP on dev: a773231 create a file
stash@{1}: WIP on master: 690b3a8 fouth commit
保存工作現(xiàn)場(chǎng)總結(jié)
- 保存現(xiàn)場(chǎng)
git stash
git stash list
- 恢復(fù)現(xiàn)場(chǎng)
git stash apply(stash內(nèi)容并不刪除,需要通過git stash drop stash@(0}手動(dòng)刪除)
git stash pop(恢復(fù)的同時(shí)也將stash內(nèi)容刪除)
git stash apply stash@(0}
標(biāo)簽與diff
新建標(biāo)簽,標(biāo)簽有兩種:輕量級(jí)標(biāo)簽(lightweight)與帶有附注標(biāo)簽
(annotated)-
創(chuàng)建一個(gè)輕量級(jí)標(biāo)簽
git tag v1.0.1 -
創(chuàng)建一個(gè)帶有附注的標(biāo)簽
git tag-a v1.0.2 -m 'release version' -
刪除標(biāo)簽
git tag -d tag_name
查看文件都有誰(shuí)修改過:
$ git blame test.txt
690b3a81 (guqing 2019-03-17 20:29:26 +0800 1) 我在這里添加了一行
690b3a81 (guqing 2019-03-17 20:29:26 +0800 2) 這是第四次修改
差異性比較diff
比較工作區(qū)和暫存區(qū)的差異
$ git diff
diff --git a/test.txt b/test.txt
index b704362..e5c0dec 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
我在這里添加了一行
這是第四次修改
hello world
+hello java
代表有三行相同hello java這一行不同
比較工作區(qū)與特定的commitId的差異
git diff commit_id
例如:
$ git diff HEAD # HEAD指向的是一次提交
diff --git a/test.txt b/test.txt
index d0a5143..e5c0dec 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,4 @@
我在這里添加了一行
這是第四次修改
+hello world
+hello java
比較暫存區(qū)與已提交特定把把版本的差異
git diff --cached commit_id #不加commit_id表示與最新提交比較
例如:
$ git diff --cached
diff --git a/test.txt b/test.txt
index d0a5143..b704362 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
我在這里添加了一行
這是第四次修改
+hello world
遠(yuǎn)程倉(cāng)庫(kù)
push 推送
pull 拉取,同時(shí)會(huì)執(zhí)行合并merge,pull == fetch + merge
推送命令:
git remote add origin 倉(cāng)庫(kù)地址 #使用orgin與倉(cāng)庫(kù)地址關(guān)聯(lián)
git push -u origin master # 將master推送到遠(yuǎn)程,并與本地master分支關(guān)連,下次就不用再寫master
然后下次提交時(shí)使用git push就可以向該遠(yuǎn)程倉(cāng)庫(kù)提交了
使用git remote show命令可以列出與當(dāng)前倉(cāng)庫(kù)關(guān)聯(lián)的所有遠(yuǎn)程倉(cāng)庫(kù)
查看遠(yuǎn)程倉(cāng)庫(kù)詳細(xì)信息git remote show orgin
基于Git分支的開發(fā)模型:
- develop分支(頻繁變化的一個(gè)分支)
- test分支(供測(cè)試與產(chǎn)品等人員使用的一個(gè)分支,變化不是特別頻繁)
- master分支(生產(chǎn)發(fā)布分支,變化非常不頻繁的一個(gè)分支)
- bugfix(hotfix)分支(生產(chǎn)系統(tǒng)當(dāng)中出現(xiàn)了緊急Bug,用于緊急修復(fù)的分支)
git push origin +master #強(qiáng)制推送到遠(yuǎn)程
git fetch 和git pull 的差別
-
git fetch相當(dāng)于是從遠(yuǎn)程獲取最新到本地,不會(huì)自動(dòng)merge,如下指令:
git fetch orgin master //將遠(yuǎn)程倉(cāng)庫(kù)的master分支下載到本地當(dāng)前branch中
git log -p master ..origin/master //比較本地的master分支和origin/master分支的差別
git merge origin/master //進(jìn)行合并
也可以用以下指令:
git fetch origin master:tmp //從遠(yuǎn)程倉(cāng)庫(kù)master分支獲取最新,在本地建立tmp分支
git diff tmp //將當(dāng)前分支和tmp進(jìn)行對(duì)比
git merge tmp //合并tmp分支到當(dāng)前分支
-
git pull:相當(dāng)于是從遠(yuǎn)程獲取最新版本并merge到本地
git pull origin master
git pull 相當(dāng)于從遠(yuǎn)程獲取最新版本并merge到本地
在實(shí)際使用中,git fetch更安全一些