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. そして 2つの追跡されていないファイル。

以下に示すように、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

2つのファイルをステージングする場合は、以下のコマンドを実行します。

$ Git add -u

git status コマンドを実行して、Git が 2つのファイルをステージングしたかどうかを確認します。

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

コミット用に削除および変更されたファイルを追加しました。