痞子衡嵌入式:第一本Git命令教程(4)- 轉移(add/rm/mv)

痞子衡發表於2016-12-17

  今天是Git系列課程第四課,上一課我們在Git空間裡做了一些檔案改動並且知道了如何利用Git檢視這些變動,今天痞子衡要講的是將這些變動提交到Git本地倉庫前的準備工作。

  Git倉庫目錄下的檔案改動操作預設都發生在Git工作區內,Git並不會主動管理。如果希望Git能夠管理這些變動,你需要主動通知Git。共有3種通知Git的命令(git add/rm/mv),痞子衡為大家一一講解。

1.將工作區檔案改動新增到暫存區git add

  git add是第一種通知Git命令,這個命令用於告訴Git我們新增了檔案改動,被git add命令操作過的檔案(改動)便會處於Git暫存區。

1.1新增單檔案改動git add [file path]

  上一節課我們已經在工作區建立了3個檔案,讓我們開始用git add將它們一一新增到暫存區:

// 將main.c,test.c, dummy.c分別新增到暫存區
jay@pc MINGW64 /d/my_project/gittest (master)
$ git add main.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add app/test.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add app/dummy.c

// 檢視此時的檔案狀態,3個檔案都已在暫存區中了
jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app/dummy.c
        new file:   app/test.c
        new file:   main.c

1.2新增資料夾內全部改動git add -A [folder path]

  有沒有覺得git add [filepath]一次只能新增一個檔案不夠高效?別急,你還可以按資料夾來提交。這時我們再做一些改動,將dummy.c檔案刪除,將test.c檔案裡的內容全部刪除,再新增一個名叫trash.c的檔案。

// 檢視dummy.c,test.c, track.c狀態
jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app/dummy.c
        new file:   app/test.c
        new file:   main.c

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:    app/dummy.c
        modified:   app/test.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/trash.c

  讓我們試試git add -A [folderpath],執行完這個命令後,我們可以看到app/資料夾下的所有型別的檔案改動(新增、修改、刪除)被一次性地儲存到了暫存區,這下是不是效率高了很多。有興趣的朋友還可以繼續研究git add .(不包括刪除操作)和git add -u(不包括新增操作)兩個命令,實際上git add -A是這兩個命令的並集。

// 將app/資料夾下的所有型別的檔案改動全部新增到暫存區
jay@pc MINGW64 /d/my_project/gittest (master)
$ git add -A app/

// 檢視此時Git狀態,尤其是app/資料夾下的檔案狀態
jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app/trash.c
        new file:   app/test.c
        new file:   main.c

2.將暫存區檔案改動退回git rm

  git rm是第二種通知Git命令,這個命令用於告訴Git我們想把之前用git add新增的檔案改動從Git暫存區裡拿出去,不需要Git記錄了。拿出去有兩種拿法,一種是從暫存區退回到工作區,另一種是直接丟棄檔案改動。讓我們試著將test.c退回到工作區,trash.c直接丟棄。

2.1退回檔案改動到工作區git rm --cache [file path]

// 將test.c的改動從暫存區移回工作區
jay@pc MINGW64 /d/my_project/gittest (master)
$ git rm --cache app/test.c

rm 'app/test.c'

// 檢視test.c是否已經移回到工作區
jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app/trash.c
        new file:   main.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/test.c

2.2直接刪除檔案git rm -f [file path]

// 將track.c檔案直接從Git空間裡刪除,不留痕跡
jay@pc MINGW64 /d/my_project/gittest (master)
$ git rm -f app/trash.c

rm 'app/trash.c'

jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   main.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/

3.將暫存區檔案移動位置/重新命名git mv

  git mv是第三種通知Git命令,這個命令用於告訴Git我們想把之前用git add新增的檔案直接在暫存區裡重新命名或移動到新位置。讓我們試著將main.c重新命名為app.c,並移動到app/資料夾下。

3.1檔案重新命名git mv [src file] [dest file]

// 將main.c在暫存區裡直接改名為app.c(app.c也記錄在暫存區)
jay@pc MINGW64 /d/my_project/gittest (master)
$ git mv main.c app.c

jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/

3.2移動檔案位置git mv [src file] [dest dir]

// 將app.c從gittest主目錄移動到app/目錄下(移動操作記錄在暫存區)
jay@pc MINGW64 /d/my_project/gittest (master)
$ git mv app.c app/

jay@pc MINGW64 /d/my_project/gittest (master)
$ 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)

        new file:   app/app.c

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/test.c

相關文章