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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32074850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:28:48  来源:igfitidea点击:

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

gitgit-commit

提问by Tumuluri V Datta Vamshi

I made changes to two files in different directory, How can I add the two files for a single commit. can I do like, add the first, then change the directory to second file and add the second file finally do the commit. Is this going to work?

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

采纳答案by iamafasha

You can add all the files using

您可以使用添加所有文件

git add file1.txt folder/file2.txt file3.txt file4.txt file5.txt

And the commit your the files you wish to commit.

并提交您希望提交的文件。

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

You will have added

你会添加

  • file1.txt
  • folder/file2.txt
  • file3.txt
  • file4.txt
  • 文件1.txt
  • 文件夹/file2.txt
  • 文件3.txt
  • 文件4.txt

to staging area and then commited with the same message.

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

  • file1.txt
  • folder/file2.txt
  • file3.txt
  • 文件1.txt
  • 文件夹/file2.txt
  • 文件3.txt


Note that files are added or committed in the same format

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

回答by alifirat

If the two directories are part of the same git project, just add the two files with gitand then make your commit :

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

git add folder1/file1 folder2/file2 
git commit 

With doing this, you can see that for this specific commit, you have two files which the contents are changed.

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

回答by neshkeev

You can use the addcommand interactively:

您可以交互使用该add命令:

git add -i

then you see:

然后你会看到:

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

hit 4 (add untracked), then you see

点击 4(添加未跟踪),然后您会看到

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

hit 1 and 2 to add file1and file2

按 1 和 2 添加file1file2

then you commit these files: git commit

然后你提交这些文件: git commit

回答by BEPP

1."git status" list which files are staged, unstaged, and untracked. Under "Changes not staged for commit" you can see all your edited files.

1.“git status”列出哪些文件是暂存的、未暂存的和未跟踪的。在“未暂存的更改提交”下,您可以看到所有已编辑的文件。

 git status 

2."git add" to stage your commit.

2.“git add”来暂存你的提交。

 git add <file1> <file2> <file3>        

(or) "git add -A" to stage all the modified files to commit.

(或)“git add -A”将所有修改过的文件暂存为提交。

 git add -A

3."git commit -m " to commit your changes,

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

 git commit -m "I am committing multiple files"

回答by mayo

Another option. You can add "ALL" the modified files to the next commit with

另外一个选项。您可以将“所有”修改后的文件添加到下一次提交中

git add -A

which is equivalent to

这相当于

git add -all

Then you can use

然后你可以使用

git commit -m "some info about this commit"

Useful when you have more than just two files or if you don't want to write the names/paths of them.

当您有两个以上的文件或者您不想写下它们的名称/路径时很有用。

From:

从:

git-add Documentation

git-add 文档



这个问题也可以给你更多关于 git add 的信息: Difference between "git add -A" and "git add ."“git add -A”和“git add”之间的区别。