How to add multiple files in git for a single commit?
Solution 1
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
to staging area and then commited with the same message.
- file1.txt
- folder/file2.txt
- file3.txt
Note that files are added or committed in the same format
Solution 2
You can use the add
command interactively:
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
What now> 4
1: file1
2: file2
Add untracked>>
hit 1 and 2 to add file1
and file2
then you commit these files: git commit
Solution 3
If the two directories are part of the same git project, just add the two files with git
and then make your commit :
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.
Solution 4
1."git status" list which files are staged, unstaged, and untracked. Under "Changes not staged for commit" you can see all your edited files.
git status
2."git add" to stage your commit.
git add <file1> <file2> <file3>
(or) "git add -A" to stage all the modified files to commit.
git add -A
3."git commit -m " to commit your changes,
git commit -m "I am committing multiple files"
Solution 5
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:
Also this question can give to you more info about git add: Difference between "git add -A" and "git add ."
Tumuluri V Datta Vamshi
Updated on December 16, 2020Comments
-
Tumuluri V Datta Vamshi over 3 years
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?
-
Holloway over 8 yearsYou can also add them in the same command
git add folder1/file1 folder2/file2