git 基本指令
git init
建立一個git倉庫
git add
將工作區(qū)的內(nèi)容添加到緩存區(qū)中
git commit -m '上傳信息'
將緩存區(qū)的內(nèi)容發(fā)布(本地服務(wù)器冒晰、遠程服務(wù)器)
git status
查看當前git發(fā)布狀態(tài)。
發(fā)生沖突時竟块,也可以使用該命令查看哪個文件發(fā)生沖突
git branch
查看分支:git branch
創(chuàng)建分之:git branch 'name'
刪除分支:git branch -d dev
git checkout
git checkout --filename # 撤銷工作區(qū)的文件修改壶运,自動回到上一次的狀態(tài)
git checkout 'name' # 切換分支
git checkout -b 'new_name' # 創(chuàng)建并切換分支
git rm 'filename'
刪除文件,與git add類似操作浪秘,一個添加到緩存區(qū)蒋情,一個刪除
git log
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 20:59:18 2018 +0800
簡化log顯示:
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
#查看分支合并情況
git log --graph --pretty=oneline --abbrev-commit
# 查看分支合并圖
git log --graph
git reset
首先,Git必須知道當前版本是哪個版本耸携,在Git中棵癣,用HEAD表示當前版本,也就是最新的提交1094adb...(注意我的提交ID和你的肯定不一樣)夺衍,上一個版本就是HEAD^
狈谊,上上一個版本就是HEAD^^
,當然往上100個版本寫100個^
比較容易數(shù)不過來沟沙,所以寫成HEAD~100的畴。
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
$ git reset --hard 1094a # 使用commit id 返回到指定版本
HEAD is now at 83b0afe append GPL
git reflog
現(xiàn)在,你回退到了某個版本尝胆,關(guān)掉了電腦丧裁,第二天早上就后悔了,想恢復(fù)到新版本怎么辦含衔?找不到新版本的commit id怎么辦煎娇?
在Git中二庵,總是有后悔藥可以吃的。當你用$ git reset --hard HEAD^
回退到add distributed版本時缓呛,再想恢復(fù)到append GPL催享,就必須找到append GPL的commit id。Git提供了一個命令git reflog用來記錄你的每一次命令:
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
git diff
提交后哟绊,用git diff HEAD -- readme.txt命令可以查看工作區(qū)和版本庫里面最新版本的區(qū)別:
$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 76d770f..a9c5755 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
-Git tracks changes.
+Git tracks changes of files.
git merge
git merge branch_name 將branch_name分支的內(nèi)容合并到當前分支(master)因妙。
git 遠程倉庫
使用github作為中央服務(wù)器
將ssh-key 添加到github中,保證是本人連接的服務(wù)器票髓。
使用:
git remote add origin git@github.com:倉庫的git地址 # 連接到遠程倉庫
git push 推送文件
git pull 拉下文件
團隊開發(fā)示意圖:
通常攀涵,合并分支時,如果可能洽沟,Git會用Fast forward模式以故,但這種模式下,刪除分支后裆操,會丟掉分支信息怒详。
如果要強制禁用Fast forward模式,Git就會在merge時生成一個新的commit踪区,這樣昆烁,從分支歷史上就可以看出分支信息。
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
readme.txt | 1 +
1 file changed, 1 insertion(+)
git stash
可以把當前工作現(xiàn)場“儲藏”起來缎岗,等以后恢復(fù)現(xiàn)場后繼續(xù)工作
先使用git stash隱藏當前工作現(xiàn)場
$ git stash
Saved working directory and index state WIP on dev: f52c633 add merge
首先確定要在哪個分支上修復(fù)bug静尼,假定需要在master分支上修復(fù),就從master創(chuàng)建臨時分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
(use "git push" to publish your local commits)
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
修復(fù)完成后密强,切換到master分支茅郎,并完成合并蜗元,最后刪除issue-101分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
(use "git push" to publish your local commits)
$ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
readme.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
使用 git stash list 查看最開始的工作現(xiàn)場
$ git stash list
stash@{0}: WIP on dev: f52c633 add merge
使用git stash pop回到剛開始的工作現(xiàn)場或渤,恢復(fù)的同時把stash內(nèi)容也刪了
$ git stash pop
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
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
Dropped refs/stash@{0} (5d677e2ee266f39ea296182fb2354265b91b3b2a)
git remote (遠程倉庫的默認名稱是origin)
查看遠程庫的相關(guān)信息,git remote -v 查看更加詳細的奕扣。
git push origin master(分支名也可以修改為dev)
推送分支薪鹦,就是把該分支上的所有本地提交推送到遠程庫。推送時惯豆,要指定本地分支池磁,這樣,Git就會把該分支推送到遠程庫對應(yīng)的遠程分支上.
團隊開發(fā)步驟:
因此楷兽,多人協(xié)作的工作模式通常是這樣:
首先地熄,可以試圖用git push origin <branch-name>推送自己的修改;
如果推送失敗芯杀,則因為遠程分支比你的本地更新端考,需要先用git pull試圖合并雅潭;
如果合并有沖突,則解決沖突却特,并在本地提交扶供;
沒有沖突或者解決掉沖突后,再用git push origin <branch-name>推送就能成功裂明!
如果git pull提示no tracking information椿浓,則說明本地分支和遠程分支的鏈接關(guān)系沒有創(chuàng)建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>闽晦。
這就是多人協(xié)作的工作模式扳碍,一旦熟悉了,就非常簡單尼荆。
git rebase
rebase操作可以把本地未push的分叉提交歷史整理成直線左腔;
rebase的目的是使得我們在查看歷史提交的變化時更容易,因為分叉的提交需要三方對比捅儒。