Git

How to Add a File to the Last Commit in Git?

Git is a famous open-source software for coding collaboration. It helps the users to manage their development source code files and keep track of changes. Developers can save project changes by utilizing the “$ git commit” command. Additionally, they can update the committed changes.

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:

$ cd "C:\Git"

Step 3: Initialize Particular Git Repository

Now, initialize the current repository with the help of the below-listed command:

$ git init

Step 4: Create a New File

To generate a new file, run the “$ touch” command:

$ touch demoFile.txt

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:

$ git add demoFile.txt

Step 6: Verify Git Status

To ensure that the file is tracked or not, check the Git repository status:

$ git status

Step 7: Commit Created File

Run the “git commit” command with the “-m” option for saving all of the added changes:

$ git commit -m "DemoFile is committed"

Step 8: Check Git Log

To check the Git log history, use the following command:

$ git log

Text Description automatically generated

Step 9: Create Another New File

To generate another file to add it to the last commit, execute the “touch” command:

$ touch NewFile.txt

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:

$ git add NewFile.txt

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:

$ git status

Step 12: Add Newly Created File to Last Commit

Add the new file to the last commit by running the given command:

$ git commit --amend --no-edit

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:

$ git log --raw

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.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.