changed 5 years ago
Linked with GitHub

7.2 0424 Git 小劇場

part1: commit前

現有專案如下:

tingdeAir:git-examples tingtinghsu$ cd git-branch1
tingdeAir:git-branch1 tingtinghsu$ ls
config		hello.html	index.html	welcome.html

tingdeAir:git-examples tingtinghsu$ cd git-branch1/
tingdeAir:git-branch1 tingtinghsu$ git log --oneline
e12d8ef add database.yml in config folder
85e7e30 add hello
657fce7 add container
abb4f43 update index page
cef6e40 create index page
cc797cd init commit

狀況1 : 不小心刪掉檔案

tingdeAir:git-branch1 tingtinghsu$ rm *.html
tingdeAir:git-branch1 tingtinghsu$ ls
config

Step1. 先用 git status查發生什麼事
三個檔案被刪掉

tingdeAir:git-branch1 tingtinghsu$ 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:    hello.html
	deleted:    index.html
	deleted:    welcome.html

Step2. 用git checkout把被刪掉的檔案登出staging area

如果是 git checkout .:在staging area把所有變更恢復成最後一次存檔的狀態

tingdeAir:git-branch1 tingtinghsu$ git checkout index.html
tingdeAir:git-branch1 tingtinghsu$ 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:    hello.html
	deleted:    welcome.html

被刪除的檔案再次出現在該資料夾

no changes added to commit (use "git add" and/or "git commit -a")
tingdeAir:git-branch1 tingtinghsu$ ls -al
total 8
drwxr-xr-x@  5 tingtinghsu  staff  160  4 24 22:12 .
drwxr-xr-x@  9 tingtinghsu  staff  288  4 24 22:06 ..
drwxr-xr-x@ 18 tingtinghsu  staff  576  4 24 22:12 .git
drwxr-xr-x@  3 tingtinghsu  staff   96  8 20  2017 config
-rw-r--r--   1 tingtinghsu  staff  161  4 24 22:12 index.html

狀況2. 看某行程式碼是誰寫的: 使用git blame

tingdeAir:git-branch1 tingtinghsu$ git blame index.html
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  1) <!DOCTYPE html>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  2) <html>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  3)   <head>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  4)     <meta charset="utf-8">
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  5)     <title>首頁</title>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  6)   </head>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800  7)   <body>
657fce78 (Eddie Kao 2017-08-02 16:53:43 +0800  8)     <div class="container">
657fce78 (Eddie Kao 2017-08-02 16:53:43 +0800  9)     </div>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800 10)   </body>
abb4f438 (Eddie Kao 2017-08-02 16:49:49 +0800 11) </html>

狀況3. 新增目錄: 空目錄無法進版控

如果僅新增目錄後輸入git status, 出現nothing to commit
因為更新與否,是使用檔案內容做計算。

tingdeAir:git-branch1 tingtinghsu$ mkdir hello-world
tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
nothing to commit, working directory clean

慣例: 可新增一個.keep檔案到資料夾

tingdeAir:git-branch1 tingtinghsu$ touch hello-world/.keep
tingdeAir:git-branch1 tingtinghsu$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	hello-world/
Select a repo