1. 程式人生 > >痞子衡嵌入式:第一本Git命令教程(3)- 編輯(status/add/rm/mv)

痞子衡嵌入式:第一本Git命令教程(3)- 編輯(status/add/rm/mv)

this 通知 一次 ranch card use div 添加文件 app


  今天是Git系列課程第三課,前兩課我們都是在做Git倉庫準備工作,今天痞子衡要講的是Git本地提交前的準備工作。

  本地有了倉庫,我們便可以在倉庫所在目錄下做文件增刪改操作,這些操作默認都發生在Git工作區內,Git並不會主動管理。如果希望Git能夠管理這些變動,你需要主動通知Git。共有3種通知Git的命令,痞子衡為大家一一講解。

1.查看Git空間文件改動狀態git status

  前面講過Git空間內文件改動有4種狀態,除了Unmodified狀態的文件之外,其他文件改動狀態都可以通過git status來查看。讓我們開始在工作區創建3個文件:main.c、test.c、dummy.c

  dummy.c(路徑:/gittest/app/dummy.c)為空白文件。
  test.c(路徑:/gittest/app/test.c)文件內容如下:

#include <stdio.h>
#include <stdlib.h>
void test(void)
{
    printf("this is test\n");
}

  main.c(路徑:/gittest/main.c)文件內容如下:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    printf("hello world
\n"); return 0; }

  新建了3個文件,讓我們來看看git記錄的狀態,從下面結果可知,新增的3個文件在Git空間裏都屬於Untracked文件,存放在工作區內。

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

        app/
        main.c

2.將工作區文件改動添加到暫存區git add

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

2.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文件裏的內容全部刪除,再新增一個名叫duty.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/duty.c

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

jay@pc MINGW64 /d/my_project/gittest (master)
$ git add -A 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/duty.c
        new file:   app/test.c
        new file:   main.c

3.將暫存區文件改動退回git rm

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

3.1退回文件改動到工作區git rm --cache [file path]

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

rm 'app/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/duty.c
        new file:   main.c

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

        app/test.c
3.2丟棄文件改動git rm -f [file path]

jay@pc MINGW64 /d/my_project/gittest (master)
$ git rm -f app/duty.c

rm 'app/duty.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/

4.將暫存區文件移動位置/重命名git mv

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

4.1文件重命名git mv [src file] [dest file]

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/
4.2移動文件位置git mv [src file] [dest dir]

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

痞子衡嵌入式:第一本Git命令教程(3)- 編輯(status/add/rm/mv)