In this tutorial, we will learn how to commit changes to our Git repository. Committing changes is like saving a version of your files in a repository. This becomes a crucial part of version control when working on projects, especially when collaborating with others.
By the end of this tutorial, you will learn:
- How to stage changes
- How to commit changes
- How to view commit history
Prerequisites:
- Basic understanding of Git and GitHub
- Git installed on your machine
Staging changes is the first step before committing in Git. It's a way to prepare and organize your changes before committing them.
To stage changes, use the command: git add <filename>
After staging your changes, the next step is to commit those changes. Committing is the act of packaging your changes and providing a descriptive message of what has been changed.
To commit changes, use the command: git commit -m "your descriptive message"
It's important to keep track of what changes have been made and by whom. Git provides a command to view the history of commits.
To view commit history, use the command: git log
Let's say you have a file named index.html
and you've made some changes. To stage these changes:
git add index.html
After staging your changes, you can commit them with a message describing what you did:
git commit -m "Updated title in index.html"
To view your commit history:
git log
This will display a list of all commits made in that repository, along with details like the author, date, and commit message.
In this tutorial, we've learned how to stage changes, commit them, and view our commit history. These are fundamental skills for version control in Git. We encourage you to practice these commands until you're comfortable with them.
Next steps for learning could include learning how to work with branches, merge changes, and resolve conflicts.
Stage and commit changes in a file named practice.txt
. Write "This is a practice file" in your file and commit it with the message "First commit".
Make another change in the practice.txt
file. Modify the text to "This is an updated practice file", then stage and commit the changes with the message "Updated the practice file".
View your commit history. Can you understand the changes you've made?
Solutions
echo "This is a practice file" >> practice.txt
git add practice.txt
git commit -m "First commit"
echo "This is an updated practice file" > practice.txt
git add practice.txt
git commit -m "Updated the practice file"
git log
You should see two commits: "First commit" and "Updated the practice file".
Keep practicing these steps with different files and changes to get the hang of committing and tracking progress in Git.