前言:用Git做過一些項目,但是隻是會用一些常用功能。沒有真正的涉及複雜分支操作。花些時間系統學習一下Git的理論知識,結合實踐,提高水平。
本篇學習Git的基礎支持。基於官網提供的中文版入門書籍Pro Git的章節Git基礎。
獲取 Git 倉庫
使用clone命令克隆一個現有的遠程倉庫,默認clone命令會把遠程倉庫的所有版本的文件都克隆到本地。如下的示例是克隆一個Github的遠程倉庫到本地。成功後會在本地建立一個名為GitTestProject的文件夾。首次執行時會提示輸入Github的賬號密碼。
$ git clone https://github.com/Kutilion/GitTestProject.git
Cloning into 'GitTestProject'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
記錄每次更新到倉庫
在這個工程根目錄下添加一個文件
echo 'My project' > Information.txt
使用git status命令確認這個文件的狀態發現,這個文件沒有被git跟蹤
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)/<file>
Information.txt
nothing added to commit but untracked files present (use "git add" to track)
使用add命令,把文件添加到緩存,進行跟蹤
$ git add Information.txt
warning: LF will be replaced by CRLF in Information.txt.
The file will have its original line endings in your working directory
再次確認這個文件的狀態
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)/<file>
new file: Information.txt
修改既存的文件README.md後確認其狀態
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)/<file>
new file: Information.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)/<file>
(use "git checkout -- <file>..." to discard changes in working directory)/<file>
modified: README.md
使用add命令將修改後的README.md文件加入緩存後,確認其狀態
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)/<file>
new file: Information.txt
modified: README.md
再次修改README.md文件後確認其狀態
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)/<file>
new file: Information.txt
modified: README.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)/<file>
(use "git checkout -- <file>..." to discard changes in working directory)/<file>
modified: README.md
可以發現README.md出現了兩次。這是如若使用commit命令提交文件,只會提交Changed to be committed版本的文件。也就是說,第二次修改README.md文件的內容不會被提交。
查看狀態的命令可以加上-s參數,使輸出簡化:
$ git status -s
A Information.txt
MM README.md
忽略文件
在工程根目錄可以創建.gitignore文件,在其中列出不被git管理的文件。
$ cat .gitignore
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
可以看到,列出了很多類型的文件。這些文件都將被git忽略。可以使用正則表達式,更多的示例可以參照
https://github.com/github/gitignore
查看已暫存和未暫存的修改
diff命令是查看最後一次add命令添加到緩存的文件,和最後一次add命令之後修改的文件。
如下,README.md文件被修改了兩次, 第一次修改通過add命令緩存了,第二次修改沒有執行add命令。所以diff命令的結果輸出這兩個版本的不同。
$ git diff
diff --git a/README.md b/README.md
index 31fb62b..76de482 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
# Kutilion
-My one repository1
+My one repository12
diff --staged命令是查看已經被commit的文件和被緩存的文件。
如下,對於README.md文件來說,示例比較的是第一次修改後的README.md文件和剛剛克隆下來時的README.md文件
$ git diff --staged
diff --git a/Information.txt b/Information.txt
new file mode 100644
index 0000000..74e5e69
--- /dev/null
+++ b/Information.txt
@@ -0,0 +1 @@
+My project
diff --git a/README.md b/README.md
index 6ed3783..31fb62b 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
# Kutilion
-My one repository
+My one repository1
提交更新
commit命令可以把緩存後的文件提交。
下面先通過add將README.md第二次修改的內容也緩存,然後用commit提交。注意使用-m命令可以避免進入編輯器模式,用於添加提交註釋。筆者對這個編輯器很不感冒。
$ git add README.md
$ git status -s
A Information.txt
M README.md
$ git commit -m "First commit"
[master c4bea31] First commit
2 files changed, 2 insertions(+), 1 deletion(-)
create mode 100644 Information.txt
可以看到提交的分支是master,SHA-1校驗碼是c4bea31。以及一些文件信息。
跳過使用暫存區域
每次都使用add來把更改的文件加入緩存是很浪費時間的,所以可以使用commit -a命令來跳過緩存步驟。直接把更改後的文件提交。
刪除文件
直接使用rm命令刪除文件。本地文件夾中的文件也被刪除了。
$ git rm Information.txt
rm 'Information.txt'
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)/<file>
deleted: Information.txt
第三次修改README.md文件,然後執行rm命令
$ git rm README.md
error: the following file has local modifications:
README.md
(use --cached to keep the file, or -f to force removal)
出現錯誤,原因是第三次修改沒有通過add來添加到緩存。
使用-f可以無視緩存,強制刪除
$ git rm -f README.md
rm 'README.md'
rm --cached 命令可以將緩存的文件版本刪除, 而保留本地版本。有些情況會用到。
使用移動文件命令來更改文件名字
使用mv命令可以實現移動文件,也可以實現文件重命名。重命名看起來是一條命令,其實它隱含了三條命令
$ git mv ChangeName NameChanged
隱含的命令。當然隱含的命令不是我們需要關心的。
$ mv ChangeName NameChanged
$ git rm ChangeName
$ git add NameChanged
查看提交歷史
log命令查看提交歷史。-p參數顯示提交內容,-2參數顯示最近兩次提交,--stat參數顯示簡略報告。
$ git log
commit c4bea312bd0676dcdf00a40bb38fb2930bc82afe (HEAD -> master)
Author: kutilion <kutilion>
Date: Sun Mar 31 20:47:35 2019 +0800
First commit
commit 6e2fa0997c2ce0bd01c289000c70795d232676ca (origin/master, origin/HEAD)
Author: Kutilion <[email protected]>
Date: Thu Mar 14 15:54:53 2019 +0800
Initial commit
--pretty參數可以選擇報告的風格比如--pretty=oneline
$ git log --pretty=oneline
c4bea312bd0676dcdf00a40bb38fb2930bc82afe (HEAD -> master) First commit
6e2fa0997c2ce0bd01c289000c70795d232676ca (origin/master, origin/HEAD) Initial commit
關於log命令的參數可以參照原文。
閱讀更多 一個經驗用十年的碼農 的文章