Git檔案操作(二)

Young_618發表於2018-07-28

有關git版本管理的介紹,參考https://blog.csdn.net/cymy001/article/details/81262819

git add <file>:告訴Git,把“檔案”新增到“倉庫”

git commit -m <file>:告訴Git,把“檔案”提交到“倉庫”

git status:檢視倉庫當前的狀態【修改資訊】

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ touch readme.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add readme.txt   #將檔案從工作區加到暫存區 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "+ one flie"   #將暫存區檔案加到分支
[master (root-commit) 59dcbd5] + one flie
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git status   #檢視版本庫/倉庫當前狀態
On branch master
nothing to commit, working tree clean

Git比其他版本控制系統設計得優秀,因為Git跟蹤並管理的是修改,而非檔案。

如何提交多次修改?

(1)git add -> 第一次修改,然後git commitgit add -> 第二次修改,然後git commit

(2)git add -> 第一次修改,再git add -> 第二次修改最後一起git commit,就相當於把兩次修改合併後一塊提交了。

git diff <file>:檢視檔案修改前後差異,顯示的格式是Unix通用的diff格式

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management

YYwecashdeMacBook-Pro:GITWORKSPACE yywecash$ git add readme.txt 
YYwecashdeMacBook-Pro:GITWORKSPACE yywecash$ git commit -m "modify readme.txt"
[master c8c2c16] modify readme.txt
 1 file changed, 2 insertions(+)
YYwecashdeMacBook-Pro:GITWORKSPACE yywecash$ git status
On branch master
nothing to commit, working tree clean

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff readme.txt   #檢視工作區檔案修改前後差異
diff --git a/readme.txt b/readme.txt
index e69de29..85487de 100644
--- a/readme.txt
+++ b/readme.txt
@@ -0,0 +1,2 @@
+git maybe better than svn
+I just want to try another version management

git log:顯示從最近到最遠的提交日誌

git log –pretty=oneline:顯示簡版提交日誌

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git log   #顯示全部提交日誌記錄
commit c8c2c16de9f8065a4f2945db947bef39d23a024e (HEAD -> master)
Author: Yu Yg <898486@qq.com>
Date:   Sat Jul 28 23:07:16 2018 +0800

    modify readme.txt

commit 59dcbd5dd97eb47518942b3b97bdb4f3ebd08447
Author: Yu Yg <898486@qq.com>
Date:   Sat Jul 28 22:02:35 2018 +0800

    + one flie

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git log --pretty=oneline   #顯示簡版提交記錄
c8c2c16de9f8065a4f2945db947bef39d23a024e (HEAD -> master) modify readme.txt
59dcbd5dd97eb47518942b3b97bdb4f3ebd08447 + one flie

git reset –hard HEAD^:版本回退

git reset –hard [+commitid]:找回剛被刪掉的提交id是commitid的版本

上一個版本是HEAD^,上上一個版本是HEAD^^,當然往上100個版本是HEAD~100。Git在回退版本時,其內部有個指向當前版本的HEAD指標。

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git reset --hard HEAD^   #回退版本
HEAD is now at 59dcbd5 + one flie
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git log --pretty=oneline
59dcbd5dd97eb47518942b3b97bdb4f3ebd08447 (HEAD -> master) + one flie

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git reset --hard c8c2c16   #找回回退版本
HEAD is now at c8c2c16 modify readme.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git log --pretty=oneline
c8c2c16de9f8065a4f2945db947bef39d23a024e (HEAD -> master) modify readme.txt
59dcbd5dd97eb47518942b3b97bdb4f3ebd08447 + one flie

git reflog:記錄每一次命令

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git reflog   #
c8c2c16 (HEAD -> master) HEAD@{0}: reset: moving to c8c2c16
59dcbd5 HEAD@{1}: reset: moving to HEAD^
c8c2c16 (HEAD -> master) HEAD@{2}: commit: modify readme.txt
59dcbd5 HEAD@{3}: commit (initial): + one flie

git diff HEAD -- readme.txt:檢視工作區和版本庫裡面最新版本的區別

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt 
git maybe better than svn
I just want to try another version management

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim readme.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat readme.txt
git maybe better than svn
I just want to try another version management
I just want to tey the diff function of git!!!

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff HEAD -- readme.txt   #檢視工作區和版本庫的檔案差異
diff --git a/readme.txt b/readme.txt
index 85487de..f95a964 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 git maybe better than svn
 I just want to try another version management
+I just want to tey the diff function of git!!!

git checkout -- <file>:檔案沒新增暫存區,在工作區修改錯了,撤回修改

git reset HEAD <flie>+git checkout -- <file>:工作區修改錯,然後將錯的新增到暫存區,撤回修改

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ touch testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat testwrong.txt 
git may be stupid,haha...
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "+wrong example"   #將檔案提交到版本庫
[master 981b076] +wrong example
 1 file changed, 1 insertion(+)
 create mode 100644 testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim testwrong.txt   #工作區修改檔案
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat testwrong.txt 
git may be stupid,haha...
Is git really better than svn?
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff HEAD -- testwrong.txt 
diff --git a/testwrong.txt b/testwrong.txt
index 29c3afa..4798cf4 100644
--- a/testwrong.txt
+++ b/testwrong.txt
@@ -1 +1,2 @@
 git may be stupid,haha...
+Is git really better than svn?
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout -- testwrong.txt   #撤銷工作區修改 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff HEAD -- testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ touch testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat testwrong.txt 
git may be stupid,haha...
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git commit -m "+wrong example"
[master 981b076] +wrong example
 1 file changed, 1 insertion(+)
 create mode 100644 testwrong.txt

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ vim testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ cat testwrong.txt 
git may be stupid,haha...
a wrong change add to temporary area
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git add testwrong.txt   #將修改提交到暫存區
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff HEAD -- testwrong.txt
diff --git a/testwrong.txt b/testwrong.txt
index 29c3afa..29b546a 100644
--- a/testwrong.txt
+++ b/testwrong.txt
@@ -1 +1,2 @@
 git may be stupid,haha...
+a wrong change add to temporary area

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git reset HEAD testwrong.txt   #丟棄提交到暫存區的修改,退回到工作區 
Unstaged changes after reset:
M	readme.txt
M	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout -- testwrong.txt   #將工作區修改丟棄 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git diff HEAD -- testwrong.txt

git checkout <file>:用“版本庫裡的版本”替換“工作區的版本”,無論工作區是修改還是刪除,都可以“一鍵還原”

git rm <file>:將檔案從版本庫中刪除

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ rm testwrong.txt 
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git checkout testwrong.txt   #用版本庫恢復本地刪除的檔案
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt	testwrong.txt

YYwhdeMacBook-Pro:GITWORKSPACE yywh$ git rm testwrong.txt   #刪除版本庫裡的檔案 
rm 'testwrong.txt'
YYwhdeMacBook-Pro:GITWORKSPACE yywh$ ls
readme.txt

 

相關文章