在 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")
在上面的示例中,我们有一个包含以下文件的索引。
- 修改后的
Sample.tx
文件。 - 已删除的
README.md
文件。 - 还有两个未跟踪的文件。
我们运行 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
我们已经添加了已删除和修改的文件以进行提交。
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn