Basic Git Workflow
After you’ve created your first Git repository, it’s the perfect time to dive into the Git workflow. The basic Git workflow is the process by which you track changes to your files, stage them for committing, and then commit those changes to your local repository.
In this lesson we will guide you through the first steps of this process and we will give you the tools which will help you to start managing your project with Git effectively and get the most benefit of it.
git status
After creating your first Git repository, the first thing you’ll want to do is tracking the status of your project files and monitoring changes. The git status command shows you which files have been modified, which are untracked, and which are staged for commit. It’s a very helpful way to get a quick summary of your project changes.
Using git status
- 1Open your terminal and navigate to your Git repository file.
- 2Run the following command:
git status
This will show the current status of your repository. For example, if you’ve just created a new file, it will appear as untracked:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
In this example, README.md is an untracked file. Git doesn’t know about it yet, so we need to stage it before we can commit it.
Staging Files with git add
Before starting to commit changes to your repository, you need to stage the files. Staging a file tells Git which changes you want to include in your next commit (To understand in future).
How to Stage Files
To stage a file, run this command:
git add <filename>
For example, to stage your README.md
file:
git add README.md
If you decide to stage all modified or new files at only one step, you can use the following command:
git add .
It makes sense because there isn't a specific file to stage, so you don't need to provide the command with a filename.
So this command stages all changes in the current directory and its all subdirectories. After staging files, run git status
again to confirm that they are now ready to be committed:
git status
You’ll get output similar to this one:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Committing Changes with git commit
After you’ve staged your changes, it’s time to commit them. A commit in Git represents a snapshot of your project at a specific point in time. It records the changes you've made and adds them to the version history ( Like recording a video to detect changes ).
How to Commit Changes
To start committing your staged files, you can use the following command:
git commit -m "Your commit message"
Like that:
git commit -m "Add README file"
The -m
option in this command refers to (message)
The -m
option allows you to add a commit message directly in this command. The message should briefly describe the important changes you made. Here are some good practices for writing commit messages to better understand:
- Be concise: A short and clear description is best. for example, using “Fix homepage bug” provide a clearer understanding than “Fix bug”
- Use present tense: For example, using “Fix bug” is better than “Fixed bug”.
- Be descriptive: For example, "Add feature X to homepage" is better than "Updated homepage".
Viewing Commit History with git log
After committing your project changes, you can see the history of your commits using the git log
command. This allows you to track the progress of your project over time.
How to View Commit History
Run the following command:
git log
This command displays a list of commits, starting with the most recent. Each commit has an ID (SHA hash), author, date, and commit message (describe). Here’s an example of what you might see:
commit 7ac9c41b0ad9b4be1f00f42a9f1e5c88a3feacb7 (HEAD -> main)
Author: Your Name <youremail@example.com>
Date: Fri Nov 28 16:42:19 2024 -0500
Add README file
You can scroll through the commit history using the arrow keys to see the rest commits or click q to exit.
A Basic Example
Here’s how the basic workflow looks in real world:
1. Creating a new file:
echo "Hello, Git!" > README.md
2. Checking the project status:
git status
3. Staging your file:
git add README.md
4. Commit the changes:
git commit -m "Add README file"
5. View the commit history:
git log
This simple example of tracking process, staging, and committing changes is the foundation of working with Git. Once you’re comfortable with these steps, you can start exploring more advanced features like branching and merging.
Conclusion
In this lesson, we’ve learned about the basics of Git workflow: tracking changes, staging files, and committing those changes to your local repository. With these core skills, you’re now ready to manage the version history of your projects. In the next lesson, we’ll explain working with remote repositories and the GitHub platform for teamwork and collaboration.