添加文件到暫存區(qū)
git add file
git add 可以多次調(diào)用實(shí)現(xiàn)多個(gè)文件添加到版本庫。
anyang@anyang:~/anyang/learngit$ touch README.md
anyang@anyang:~/anyang/learngit$ ls
README.md
anyang@anyang:~/anyang/learngit$ **git add README.md **
把暫存區(qū)的內(nèi)容提交到本地倉庫
git commit -m “提交文件時(shí)的說明信息”
在創(chuàng)建 Git 倉庫時(shí)董栽,Git 自動(dòng)創(chuàng)建了一個(gè) master 分支汤踏,默認(rèn)情況下git commit
就是往 master 分支上提交更改的玉工,git commit
一次可以提交多個(gè)文件更改到當(dāng)前分支霹疫。
anyang@anyang:~/anyang/learngit$ git commit -m "Add README.md"
[master (root-commit) 72f8209] Add README.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
查看版本庫狀態(tài)
git status
若文件尚未存入暫存區(qū)俐巴,則效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
若文件已經(jīng)存入暫存區(qū)压汪,但尚未提交到本地倉庫,則效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
若所有文件修改已經(jīng)提交到本地倉庫斗塘,則效果如下:
anyang@anyang:~/anyang/learngit$ git status
On branch master
nothing to commit, working tree clean
查看文件修改內(nèi)容
git diff file
當(dāng)我們?cè)赗EADME.md文件中加入Hello, world!
后赢织,輸入以上命令效果如下:
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git diff README.md
diff --git a/README.md b/README.md
index e69de29..af5626b 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+Hello, world!
顯示從最近到最遠(yuǎn)的提交日志
該命令通常用于查看需要回退到哪個(gè)版本時(shí)使用。
git log
如果覺得輸出信息太多馍盟,不方便查看于置,可以加上--pretty=oneline
參數(shù),效果圖如下所示:
anyang@anyang:~/anyang/learngit$ git log
commit 513dec66f4313d10909e8ba0336939138957da27
Author: anyang xautanyang@163.com
Date: Fri Dec 2 20:12:52 2016 +0800
Add Hello, world!
commit 72f8209cd1f9bfa3cc279eee5492336b97217178
Author: anyang xautanyang@163.com
Date: Fri Dec 2 19:58:34 2016 +0800
Add README.md
anyang@anyang:~/anyang/learngit$ git log --pretty=oneline
513dec66f4313d10909e8ba0336939138957da27 Add Hello, world!
72f8209cd1f9bfa3cc279eee5492336b97217178 Add README.md
版本回退
Git 中HEAD
表示當(dāng)前版本贞岭,HEAD^
表示上一版本八毯,HEAD^^
表示上上版本,對(duì)于多版本回退瞄桨,可以使用HEAD~n
话速,例如:HEAD~50
表示回退50個(gè)版本。
git reset --hard HEAD^ 或 git reset --hard commit_id
假設(shè)我們?cè)?README.md 文件中錯(cuò)誤的加入了一行文字 "Wrong code" 芯侥,并提交到了版本庫泊交,則可以通過 git reset --hard HEAD^
回退到版本庫上一狀態(tài),效果圖如下:
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
Wrong code
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ git commit -m "Add Wrong code"
[master 89fdd19] Add Wrong code
1 file changed, 1 insertion(+)
anyang@anyang:~/anyang/learngit$ git reset --hard HEAD^
HEAD is now at 513dec6 Add Hello, world!
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
查詢歷史命令
該命令通常用于查看需要回到未來的哪個(gè)版本時(shí)使用柱查。
git reflog
效果圖如下所示:
anyang@anyang:~/anyang/learngit$ git reflog
513dec6 HEAD@{0}: reset: moving to HEAD^
89fdd19 HEAD@{1}: commit: Add Wrong code
513dec6 HEAD@{2}: commit: Add Hello, world!
72f8209 HEAD@{3}: commit (initial): Add README.md
撤銷更改
git checkout -- file
把文件在工作區(qū)的修改全部撤銷廓俭,讓文件回到最近一次 git commit 或 git add 時(shí)的狀態(tài),其分別為:
- 若文件自修改后還沒有被放到暫存區(qū)唉工,撤銷修改就回到和版本庫一模一樣的狀態(tài), 效果圖如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
haha
anyang@anyang:~/anyang/learngit$ git checkout -- README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
- 若文件已經(jīng)添加到暫存區(qū)研乒,并進(jìn)行了修改,撤銷修改就會(huì)回到添加到暫存區(qū)后的狀態(tài)淋硝,效果圖如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
hehe
anyang@anyang:~/anyang/learngit$ git checkout -- README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
heihei
把暫存區(qū)的修改回退到工作區(qū)雹熬,即撤銷 git add 命令
git reset HEAD file
效果圖如下所示:
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
anyang@anyang:~/anyang/learngit$ vim README.md
anyang@anyang:~/anyang/learngit$ git add README.md
anyang@anyang:~/anyang/learngit$ cat README.md
Hello, world!
haha
anyang@anyang:~/anyang/learngit$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
anyang@anyang:~/anyang/learngit$ git reset HEAD README.md
Unstaged changes after reset:
M README.md
anyang@anyang:~/anyang/learngit$ 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.md
no changes added to commit (use "git add" and/or "git commit -a")
從版本庫中刪除文件
git rm file
git commit -m "remove file"
效果圖如下所示:
anyang@anyang:~/anyang/learngit$ ls
README.md
anyang@anyang:~/anyang/learngit$ touch file.txt
anyang@anyang:~/anyang/learngit$ ls
file.txt README.md
anyang@anyang:~/anyang/learngit$ git add file.txt
anyang@anyang:~/anyang/learngit$ git commit -m "Add file.txt"
[master 13f3101] Add file.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file.txt
anyang@anyang:~/anyang/learngit$ rm file.txt
anyang@anyang:~/anyang/learngit$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
anyang@anyang:~/anyang/learngit$ **git rm file.txt **
rm 'file.txt'
anyang@anyang:~/anyang/learngit$ git commit -m "remove file.txt"
[master 2d88197] remove file.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file.txt
anyang@anyang:~/anyang/learngit$ git status
On branch master
nothing to commit, working tree clean
相關(guān)資料:
- Git 官網(wǎng)
- Git 官方文檔
- [廖雪峰的 Git 教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8
067c8c017b000) - Git 常用命令查詢文檔
- Git 在線學(xué)習(xí)網(wǎng)址