如何在 git 中为单个提交添加多个文件?

How to add multiple files in git for a single commit?

我对不同目录中的两个文件进行了更改,如何为单个提交添加这两个文件。我可以这样做吗,添加第一个,然后将目录更改为第二个文件并添加第二个文件最后执行提交。这行得通吗?

如果这两个目录是同一个 git 项目的一部分,只需添加带有 git 的两个文件,然后进行提交:

git add folder1/file1 folder2/file2 
git commit 

通过这样做,您可以看到对于这个特定的提交,您有两个文件的内容已更改。

可以使用add命令interactively:

git add -i

然后你会看到:

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help
What now>

打4(添加未追踪),然后你看到

What now> 4
  1: file1
  2: file2
Add untracked>>

点击 1 和 2 添加 file1file2

然后提交这些文件:git commit

另一种选择。您可以使用

添加 "ALL" 修改后的文件到下一次提交
git add -A

相当于

git add -all

那你就可以使用

git commit -m "some info about this commit"

当您有两个以上的文件或者您不想写其中的 names/paths 个文件时很有用。

发件人:

git-add Documentation


此问题还可以为您提供有关 git 的更多信息添加: Difference between "git add -A" and "git add ."

1."git status" 列出已暂存、未暂存和未跟踪的文件。 在 "Changes not staged for commit" 下,您可以看到所有已编辑的文件。

 git status 

2."git add" 暂存您的提交。

 git add <file1> <file2> <file3>        

(或) "git add -A" 暂存所有要提交的修改文件。

 git add -A

3."git commit -m " 提交您的更改,

 git commit -m "I am committing multiple files"

您可以使用

添加所有文件
git add file1.txt folder/file2.txt file3.txt file4.txt file5.txt

然后提交您要提交的文件。

git commit file1.txt folder/file2.txt file3.txt -m"I committed file1.txt folder/file2.txt"

您将添加

  • file1.txt
  • folder/file2.txt
  • file3.txt
  • file4.txt

到暂存区,然后提交相同的消息。

  • file1.txt
  • folder/file2.txt
  • file3.txt

请注意,文件以相同的格式添加或提交