While dealing with large projects, developers create multiple files for various purposes. So, they create files, add them to the Git index, and commit them. However, sometimes, it becomes so hard to create, stage, and commit files one by one. In this situation, Git permits users to stage and commit files or changes simultaneously with one command.
This article will discuss:
How to Git Stage and Commit New Files With One Command?
To stage and commit the new file, first, navigate to the local repository and view the newly created untracked files by checking the Git status. Then, execute the “git add -A && git commit -m “<commit message>”” command and verify changes.
Step 1: View Git Status
First, check the current status of the working repository:
It can be seen that the current repository contains a new untracked file:
Step 2: Stage and Commit File
To track and commit file at once, utilize the below-stated command:
Here, the “-A” option is used to add the file to the Git staging area, and “-m” is utilized for the commit message:
Step 3: Verify Changes
Lastly, view the commit history to verify changes:
The below output indicates that the desired file has been staged and committed:
How to Git Stage and Commit Already Tracked Files With One Command?
To stage and commit the already tracked files, utilize the “git commit -am “<commit message>”” command. Try out the following instructions to do so.
Step 1: Check Current Status
First, utilize the following command and view the current status of the repository:
It can be observed that the current repository already contains a tracked modified file that needs to be staged:
Step 2: Stage and Commit File
Now, stage and commit the modified file using the provided command:
Here, the “-am” option is used to automatically add and commit the already tracked file:
Step 3: Ensure Changes
Finally, check the Git log to verify the changes:
As you can see, the particular file has been staged and committed:
That was all about Git staging and committing with one command.
Conclusion
To stage and commit new untracked files with one command, utilize the “git add -A && git commit -m “<commit message>”” command, and for the already tracked modified files, run the “git commit -am “<commit message>”” command. This article explained the methods to stage and commit files or changes with one command.