HOWTO · Git

在 Git 中新增檔案

本文演示了將檔案新增到 Git 上的倉庫的不同方法。

本文將討論將檔案新增到我們在 Git 上的倉庫的不同方法。

在 Git 中使用 git add 命令新增所有檔案

我們可以使用 git add 命令毫無例外地新增所有檔案,如下所示。

git add -A

-A 引數指示 Git 新增索引中存在的所有檔案。Git 將暫存未跟蹤、修改和刪除的檔案以進行提交。

或者,你可以執行 git add 命令,該命令指示 Git 暫存當前目錄中的檔案。

例子:

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
        deleted:    README.md
        modified:   Sample.txt
Untracked files:
    (use "git add <file>..." to include in what will be committed)
        Sample.txt.bak
        Tutorial.txt
no changes added to commit (use "git add" and/or "git commit -a")

在上面的示例中,我們有一個包含以下檔案的索引。

  1. 修改後的 Sample.tx 檔案。
  2. 已刪除的 README.md 檔案。
  3. 還有兩個未跟蹤的檔案。

我們執行 git add -A 命令來暫存所有檔案以進行提交,如下所示。

git add -A

讓我們執行 git status 命令來檢查 Git 是否暫存了我們的檔案。

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
        deleted:    README.md
        modified:   Sample.txt
        new file:   Sample.txt.bak
        new file:   Tutorial.txt

我們的檔案已準備好提交。

在 Git 中按副檔名新增檔案

有時,你可能需要特定於階段的檔案格式。例如 .js 檔案或 .txt 檔案。

我們使用 git add 命令輸入萬用字元和副檔名。下面是一個例子。

我們有一個 JavaScript 檔案,我們要在我們的目錄中暫存以進行提交。

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
        deleted:    README.md
        modified:   Sample.txt
        new file:   Sample.txt.bak
        new file:   Tutorial.txt
Untracked files:
    (use "git add <file>..." to include in what will be committed)
        New Text Document.txt
        New.js

我們可以在以下上下文中執行 git add 命令。

$ git add *.js

讓我們執行 git status 命令來檢查 Git 是否暫存檔案。

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
        new file:   New.js
        deleted:    README.md
        modified:   Sample.txt
        new file:   Sample.txt.bak
        new file:   Tutorial.txt
Untracked files:
    (use "git add <file>..." to include in what will be committed)
        New Text Document.txt

如你所見,我們的 New.js 檔案已準備好提交。

在 Git 中僅新增已刪除和修改的檔案

我們使用帶有 -u 引數的 git add 命令來指示 Git 僅新增已刪除和修改的檔案以進行提交。

例子:

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
        new file:   New.js
        deleted:    README.md
        modified:   Sample.txt
        new file:   Sample.txt.bak
        new file:   Tutorial.txt
Changes not staged for commit:
    (use "git add/rm <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
        modified:   Sample.txt
        deleted:    Sample.txt.bak

如果我們想暫存我們的兩個檔案,我們執行下面的命令。

$ Git add -u

我們將執行 git status 命令來檢查 Git 是否暫存了這兩個檔案。

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
        new file:   New.js
        deleted:    README.md
        modified:   Sample.txt
        new file:   Tutorial.txt
Untracked files:
    (use "git add <file>..." to include in what will be committed)
        New Text Document.txt

我們已經新增了已刪除和修改的檔案以進行提交。