1.Git簡介
Git是目前世界上最先進的分佈式版本控制系統,在處理各種項目時都十分高效
Git是分佈式版本控制系統,它就沒有中央服務器的,每個人的電腦就是一個完整的版本庫,這樣,工作的時候就不需要聯網了,因爲版本都是在自己的電腦上。
2.Git安裝(僅列出在Windows系統下的安裝過程)
打開Git官網下載安裝程序,然後按照默認選項安裝即可。安裝完成後,打開Git bash軟件,彈出一個類似cmd的命令行窗口,證明安裝成功。
安裝完成後,需要進行設置,在命令行輸入以下代碼:
git config --global user.name "name"
git config --global user.email "[email protected]"
顧名思義,這是設置你的名字和Email地址。
我們可以查看一下用戶名和密碼:
git config user.name
git config user.email
假如我們這時候報錯,證明Git的用戶名和密碼沒有配置成功,我們還可以這樣做:
在用戶主目錄下找到 .git 文件夾: 然後打開 config 文件,這是專門用來配置和讀取相應的工作環境變量的,在裏面加上如圖所示內容:
這樣也就完成了對Git用戶名和郵箱的配置。
3.創建版本庫
版本庫(repository)也叫倉庫,可以看做一個目錄,這個目錄裏的所以文件都由Git進行管理,每個文件的修改、刪除,Git都能跟蹤。
1.選擇一個合適的地方,創建一個空目錄:
- $ mkdir 20200214ZTE //創建一個名叫20202014ZTE的空目錄
- $ cd 20200214ZTE 進入20200214ZTE目錄
- $ pwd //查看當前目錄
如果使用Windows系統,要保證目錄名不包含中文。
2.通過如下命令把這個目錄變成Git可以管理的倉庫:
- $ git init
這樣Git就把倉庫建好啦,我們可以看到在當前目錄下多了一個 .git 的目錄,這個目錄是Git來跟蹤管理版本庫的。
3.把文件添加到版本庫
我們在 20200214ZTE 目錄下編寫一個 helloworld.txt 文件,內容如下:
this is demo for git command
helloworld is popular world!!
(1) 用 git add 命令,把文件添加到倉庫:
$ git add helloworld.txt
(2) 用 git commit 命令,把文件提交到倉庫:
$ git commit -m " xxxxxxx " // -m 後面輸入的是本次提交的說明, 可以輸入任何內容
4.修改文件
我們已經成功添加並提交了一個 helloworld.txt 文件,繼續修改 helloworld.txt 文件,改成如下內容:
運行 git status 命令:
$ git status // 查看倉庫當前的狀態
- $ git status
- On branch master
- Changes not staged for commit: // 沒有文件要被提交
- (use "git add/rm <file>..." to update what will be committed)
- (use "git restore <file>..." to discard changes in working directory)
- modified: helloworld.txt
- deleted: kevin.txt
- no changes added to commit (use "git add" and/or "git commit -a")
上面的命令告訴我們,helloworld.txt 文件被修改過了,但還沒有準備提交的修改。
如果我們想知道上次是怎麼修改readme.txt 文件的,需要用 git diff 命令:
$ git diff helloworld.txt
接下來還是那兩步:
(1) git add
git add helloworld.txt
這時候可以用 git status 查看一下當前倉庫狀態:
(2) git commit
再用 git status 查看一下當前倉庫狀態:
nothing to commit, working tree clean //當前沒有需要提交的修改,而且,工作目錄是乾淨的。
5.版本回退
如果我們繼續對 helloworld.txt 文件進行修改,改成如下內容:
- add modify for heloworld is a sample for git demo
- this line is fixed mark
- for reback check
然後添加並提交:
git add helloworld.txt
git commit -m "for reback check"
到目前爲止,readme.txt 文件一共有4個版本被提交到了 Git 倉庫裏,我們可以用 git log 命令進行查看:
git log // 查看歷史記錄
我們還可以加上 --pretty=oneline 參數:
git log --pretty=oneline
-
NKG-KEVIN-LUENG+Kevin-Lueng@NKG-Kevin-Lueng MINGW64 /d/==Work/Gitlab/20200214ZTE (master) $ git log --pretty=oneline c9926b1cdcad69a92f476b5ebcb55d05322d6467 (HEAD -> master) for reback check 4c36becccc142edde808841d22e331da1c719204 2nd modify 204f3768157f9a61a903afee49be718c72a10a4f this is a helloworld file d4ee04bbea261dcd240e6c5d7d8c3dfe6c75b116 wrote a kevin temp file 好了,現在如果我們想把 readme.txt 文件退回到上一個版本,就可以使用 git reset 命令:
好了,現在如果我們想把helloworld.txt 文件退回到上一個版本,就可以使用 git reset 命令:
HEAD表示當前版本,HEAD^表示上一個版本,那麼上上版本就是HEAD^^
這時候用 cat 命令查看一下 helloworld.txt 的內容:
果然 helloworld.txt 文件返回到了上一個版本。
我們現在想要回到最新的版本,還是使用 git reset 命令:
git reset --hard c9926 //這裏不能用HEAD,而必須用commit ID, 因爲最新版本在之前返回時已經被刪除了,而commit ID可以在之前的代碼中查到。
這時再查看一下 readme.txt 文件, 確認回到最新修改後的內容
6.工作區和暫存區
工作區(Working Directory)
20200214ZTE 文件夾就是一個工作區。
版本庫(Repository),工作區有個隱藏目錄 .git ,這個不算工作區,而是 Git 的版本庫。
版本庫裏面的 index(stage) 文件叫暫存區,還有Git爲我們自動創建的第一個分支 master ,以及指向 master 的一個指針叫做 HEAD。
前面我們提到過,如果我們想把文件添加到Git裏面時,需要分兩步:
- 第一步是用 git add 把文件添加進去,實際上就是把文件修改添加到暫存區(stage)。
- 第二步是用 git commit 提交更改,實際上就是把暫存區(stage)的所有內容提交到當前分支。(我們現在只有唯一一個分支 master,所以現在就是往 master 分支上提交更改)
練習:修改helloworld.txt裏面的內容,並且新建一個文件(如 goodbye.txt, 添加任何內容)
使用兩次 git add 命令分別把 helloworld.txt 和 goodbye.txt 都添加後,可以用 git status 命令查看一下:
現在,暫存區的狀態就變成這樣了:
再使用 git commit 命令把暫存區的所有修改提交到分支:
這時候的工作區就是乾淨的:
這時候版本庫就變成了這樣:
7.管理修改
Git 如此的優秀是因爲,Git 跟蹤並管理的不是文件,而是修改。
我們對 helloworld.txt 文件進行修改:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
然後,添加:
$ git add helloworld.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: helloworld.txt
然後再修改 readme.txt 文件:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
###
2nd modify for GIT manage
提交:
$ git commit -m "1st and 2nd modify, check which be added"
[master f8ab4c9] 1st and 2nd modify, check which be added
1 file changed, 1 insertion(+)
這時候我們查看一下狀態:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: helloworld.txt
no changes added to commit (use "git add" and/or "git commit -a")
我們可以發現,第二次修改並沒有被提交。因爲在工作區的第一次修改被放入暫存區,準備提交;而在工作區的第二次修改並沒有被放入暫存區,所以, git commit 命令只負責把暫存區的修改提交了。
提交後,我們可以用 git diff HEAD -- readme.txt 命令去查看工作區和版本庫裏面最新版本的區別:
$ git diff HEAD -- helloworld.txt
diff --git a/helloworld.txt b/helloworld.txt
index b8f31fa..b98cfcb 100644
--- a/helloworld.txt
+++ b/helloworld.txt
@@ -2,4 +2,6 @@ add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
-1st modify for GIT manage
\ No newline at end of file
+1st modify for GIT manage
+###
+2nd modify for GIT manage
\ No newline at end of file
8.撤銷修改
假如說你在 readme.txt 文件中添加了一行內容如下:
$ cat helloworld.txt
add modify for heloworld is a sample for git demo
this line is fixed mark
for reback check
for add and commit check
1st modify for GIT manage
###
2nd modify for GIT manage
for check reback
最後一行是萬萬不能讓BOSS看到的,應該怎麼撤銷呢?
(1) 沒有 git add 之前
可以手動刪除最後一行,手動把文件恢復到上一個版本的狀態。然後再用 git checkout -- file 命令丟棄工作區的修改:
(2) git add了,但沒有git commit
這時候的修改添加到了暫存區,但沒有提交到分支,用 git status 查看一下:
這時候我們可以使用 git reset HEAD file 命令把把暫存區的修改撤銷掉,重新放回工作區:
$ git reset HEAD helloworld.txt // git reset 既可以回退版本, 也可以把暫存區的修改回退到工作區,HEAD表示最新版本
Unstaged changes after reset:
M helloworld.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 restore <file>..." to discard changes in working directory)
modified: helloworld.txt //暫存區是乾淨的,工作區有修改
no changes added to commit (use "git add" and/or "git commit -a")
這時候再丟棄工作區的修改就OK了:
$ git checkout -- helloworld.txt //丟棄工作區的修改
$ git status
On branch master
nothing to commit, working tree clean
終於大功告成了。
(3) 既 git add 了,也 git commit 了
可以回退到上一個版本,見回退版本內容。
9.刪除文件
在工作區即 learngit 文件夾下新建一個 test.txt 文件,並添加和提交到Git:
$ vim new0217.txt
$ git add new0217.txt
warning: LF will be replaced by CRLF in new0217.txt.
The file will have its original line endings in your working directory
$ git commit -m "new file txt"
[master 27420c0] new file txt
1 file changed, 1 insertion(+)
create mode 100644 new0217.txt
這時候可用 rm 命令刪除:
$ rm new0217.txt
這時工作區和版本庫就不一樣了。
現在又分兩種情況:
(1) 確實要從版本庫中刪除該文件,那就用 git rm 命令刪除,並且 git commit:
$ git rm new0217.txt
rm 'new0217.txt'
$ git commit -m "remove new file 0217"
[master 0d736f4] remove new file 0217
1 file changed, 1 deletion(-)
delete mode 100644 new0217.txt
這時候文件就從版本庫被刪除了。
(2) 文件被刪錯了。因爲版本庫裏有,所以很好恢復:
$ rm new0217.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: new0217.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- new0217.txt // 用版本庫裏的版本替換工作區的版本
$ git status
On branch master
nothing to commit, working tree clean
10.遠程倉庫準備工作
在開始這部分之前,我們需要自行註冊Gitlab/github賬號。而且,因爲你的本地Git倉庫和Gitlab/hub倉庫之間的傳輸是通過SSH加密的,所以需要設置:
(1) 創建SSH Key。在用戶主目錄下C:\Users\Kevin-Lueng\,看看有沒有.ssh 目錄,如果有的話,看此目錄下有沒有 id_rsa 和 id_rsa.pub 這兩個文件,如果有,直接跳到下一步。如果沒有,打開Git Bash,創建SSH
(2) 登陸GitHub/Lab,關於如何創建賬戶及設置, 網上專業的教程太對了, 兄弟們可以自己搜索
下一篇再分享如何遠程提交和克隆git
總結 git 基本命令
git init // 對文件進行 git 操作
git add README.md // 將 工作區 的文件移出到 暫存區
git checkout README.md // 將 暫存區 的文件移出到 工作區
git reset HEAD~ README.md // 將 Git 倉庫 上一版本的文件移出到 暫存區
git commit -m "add README.md" // 將 工作區 的文件移出到 Git 倉庫
git status // 查看文件狀態
git log // 查看歷史提交
git reset HEAD~ // 將 暫存區 變成 Git 倉庫 的上一個版本,並刪除這個版本的 Git 倉庫
git reset --sort HEAD~ // 將 Git 倉庫 變成上一個版本,並刪除這個版本的 Git 倉庫
git reset --hard HEAD~ // 將 工作區 變成 Git 倉庫 的上一個版本,並刪除這個版本的 Git 倉庫(危險)
git diff // 比較 暫存區 和 工作區 的不同(顯示的 - 代表 暫存區, + 代表 工作區)
git diff Git 倉庫版本ID Git 倉庫版本ID // 比較 Git 倉庫兩個版本間的不同,注意中間是一個空格,ID 是前 6 位
git diff Git 倉庫版本ID // 比較 Git 倉庫 和 工作區 的不同
git diff --cached Git 倉庫版本ID // 比較 Git 倉庫 和 暫存區 的不同,注意中間也是一個空格
git rm README.md // 刪除 暫存區 和 工作區 的文件
git rm -f README.md // 當 暫存區 與 工作區 同名文件不一致,強制刪除兩者此文件
git mv README.md readme.md // 重命名,左邊舊名,右邊新名
git branch dev // 創建一個 dev 分支( 對立的是 master 分支 )
git branch // 查看所有分支
git checkout master // 切回 master 分支
git merge dev // 將 dev 分支與當前所在分支合併
git branch -d dev // 只能刪除已經 merge 的分支,對於未 merge 的分支是無法刪除的
git branch -D dev // 強制刪除分支
————————————————