Git簡介
集中式vs分布式
svn屬于集中式管理,git屬于分布式管理巩梢。
安裝Git
MacOS 安裝Xcode后默認安裝Git。
創(chuàng)建版本庫
git init
git add
$ cd ~/Desktop/
$ mkdir GitResponse
$ cd GitResponse/
$ ls -a
輸出: . ..
$ git init
$ ls -a
輸出 . .. .git
此時可以看到文件夾中有.git
的文件,說明git倉庫構(gòu)建成功疗涉。Git只能跟蹤文本文件的改動易稠,比如.txt
文件缸废,網(wǎng)頁,程序代碼等。
$ touch readme.txt
新建一個文件readme.txt
使用$ git add readme.txt
將文件加入倉庫企量。執(zhí)行完畢以后如果沒有任何提示测萎,則表明添加成功!UNIX思想沒有消息就是好消息
git commit -m ""
$ git commit -m "新建readme.txt文件"
輸出: [master (root-commit) f242d2d] 新建readme.txt文件
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
git status
我們嘗試修改readme.txt
后 使用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")
說明修改的文件未提交届巩。 readme.txt
文件未提交硅瞧。
git diff
git diff
命令用來查看文件修改的地方
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 25fb339..344dba9 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
這是Git倉庫。
+開始學習Git恕汇。
\ No newline at end of file
其中 +開始學習Git
就是修改的地方腕唧。
$ git add readme.txt
$ git commit -m "readme.txt 新增一句話:開始使用Git"
[master 5db8876] readme.txt 新增一句話:開始使用Git
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean
版本回退
git log
查看提交日志
git log --pretty=oneline
git reset --hard 版本號
HEAD^
上個版本
HEAD^^
上上個版本
HEAD~100
git reset --hard HEAD~1
git reflog
查看命令行記錄
刪除文件
git rm <file>
用于刪除一個文件。如果一個文件已經(jīng)被提交到版本庫瘾英,那么你永遠不用擔心誤刪枣接,但是要小心,你只能恢復(fù)文件到最新版本缺谴,你會丟失最近一次提交后你修改的內(nèi)容月腋。
遠程倉庫
origin
Git默認是遠程倉庫
分支管理
創(chuàng)建與合并分支
Git 鼓勵大量使用分支
git branch
查看分支
git branch <name>
創(chuàng)建分支
git checkout <name>
切換分支
git checkout -b <name>
創(chuàng)建+切換分支
git merge <name>
合并某分支到當前分支
git branch -d <name>
刪除分支
解決沖突
同時在兩個分支中修改統(tǒng)一文件的位置后。合并代碼便會發(fā)生沖突瓣赂。
$ git merge dev
error: merge is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
此時提示合并有沖突.
git merge --no-ff -m "merge with no-ff" <branch name>
合并分支時候建議使用榆骚。可以查看其他分支的提交信息煌集。
Bug分支
git stash
工作現(xiàn)場保存妓肢。
git stash apply
回復(fù)工作現(xiàn)場 但是不會刪除stash內(nèi)容
git stash drop
刪除保存stash
git stash pop
恢復(fù)并刪除
多人協(xié)作
git remote -v
查看詳細的遠程倉庫信息
git push origin <branchName>
所有本地提交推送到遠程倉庫
git pull
從遠程倉庫拉去最新代碼
git checkout -b <branchName> origin/<branchName>
本地創(chuàng)建和遠程分支對應(yīng)的分支
git branch --set-upstream-to <branchName> origin/<branchName>
建立本地分支和遠程分支的關(guān)聯(lián)