This article will demonstrate the method to add a new file to the most recent commit in Git.
How to Add a New File to the Last Commit in Git?
To add a new file to the most recent commit in Git, first, navigate to the Git repository. Then, create a file and commit all added changes to the repository. Similarly, create another file and add it to the last commit using the “$ git commit –amend –no-edit” command.
For practical demonstration, follow the provided steps!
Step 1: Open Git Bash Terminal
Search for the “Git Bash” terminal on the Start menu and launch it:
Step 2: Move to Git Repository
Utilize the “cd” command and move to the required Git repository:
Step 3: Initialize Particular Git Repository
Now, initialize the current repository with the help of the below-listed command:
Step 4: Create a New File
To generate a new file, run the “$ touch” command:
Step 5: Add File to the Git Staging Area
Now, move the created untracked file to the Git staging area by running the “$ git add” command:
Step 6: Verify Git Status
To ensure that the file is tracked or not, check the Git repository status:
Step 7: Commit Created File
Run the “git commit” command with the “-m” option for saving all of the added changes:
Step 8: Check Git Log
To check the Git log history, use the following command:
Step 9: Create Another New File
To generate another file to add it to the last commit, execute the “touch” command:
Step 10: Add File to Git Staging Index
Then, use the below-listed command to move the newly created file to the Git staging area:
Step 11: Verify Git Status
Now, check the Git repository status to see whether the new file is created or not through the below-listed command:
Step 12: Add Newly Created File to Last Commit
Add the new file to the last commit by running the given command:
Here, the “–amend” option is used to change the commit, and the “–no-edit” option can be utilized to make a change to the commit without changing its message:
Step 13: Check Git Log
To check the Git repository log to view the latest commit, execute the “git log” command:
The below output shows that the new file is added to the last commit. Here, the “–raw” option shows the entire commit the same as it is stored in the commit object:
That’s it! We have demonstrated the method to add a file to the most recent commit in Git.
Conclusion
To add a file to the last commit, first, go to the Git local repository. Then, generate a file and commit it. After that, create another file and add it to the last commit with the help of the “git commit –amend –no-edit” command. This article illustrated the procedure of adding a file to the last commit in Git.