Git Workflow
In this chapter, we will introduce Git's workflow.
The following figure shows Git's workflow:
We can imagine Git's workflow as the process ofdoing homework and handing it in to the teacher. Combining the figure above, we can divide this process into four main parts:
1. Working Directory — your desk
This is where youactually modify files. Here you write code, delete and make changes.
- Action: You write new features or fix bugs here.
2. Staging Area — your basket ready to mail
When you feel the homework is almost done, you put it in a basket, ready to be packaged.
- Key command:
git add - Significance: Tell Git that you confirm these changes are to be committed, and to note them down first.
3. Local Repository — your personal safe
You package the things in the basket, label them (commit message), and lock them in your own safe.
- Key command:
git commit - Significance: The changes officially become part of the project history. Even if you mess things up later, you can always recover them from here.
4. Remote — the teacher's inbox (such as GitHub/GitLab)
Finally, you send the code in the safe to the remote server over the network, so others can view or collaborate.
- Key command:
git push - Significance: Back up the code and share progress with the team.
Knowledge point explanation
- git stash (stash area): You're halfway through your homework, suddenly need to work on another urgent task, but don't want to commit the unfinished homework. At this point you can first use
stashto hide the code in a drawer, and take it out to continue later when you're free (pop)。 - git pull (pull): See if the teacher (remote repository) has new homework submitted by others, and sync it directly to your desk.
- git fetch & merge: First see what updates are on the remote (
fetch), then after confirming there are no issues, merge (merge) into your own code.
Simple summary:
Modify code → add (put in the basket) → commit (store in the box) → push (send to the distance).
Command description
1. Clone repository
If you want to participate in an existing project, you first need to clone the remote repository locally:
git clone https://github.com/username/repo.git
cd repo
2. Create a new branch
To avoid developing directly on the main or master branch, a new branch is usually created:
git checkout -b new-feature
3. Working directory
Edit code, add new files, or delete unneeded files in the working directory.
4. Stage files
Add modified files to the staging area for the next commit operation:
git add filename
# Or add all modified files
git add .
5. Commit changes
Commit the changes in the staging area to the local repository, and add a commit message:
git commit -m "Add new feature"
6. Pull latest changes
Before pushing local changes, it's best to pull the latest changes from the remote repository to avoid conflicts:
git pull origin main
# Or if working on a new branch
git pull origin new-feature
7. Push changes
Push local commits to the remote repository:
git push origin new-feature
8. Create a Pull Request (PR)
Create a Pull Request on GitHub or another hosting platform, inviting team members to review the code. After the PR is merged, your changes will be merged into the main branch.
9. Merge changes
After the PR is approved and merged, you can merge the remote repository's main branch into your local branch:
git checkout main
git pull origin main
git merge new-feature
10. Delete branch
If the new feature branch is no longer needed, you can delete it:
git branch -d new-feature
Or delete the branch from the remote repository:
git push origin --delete new-feature