Git workflow
In this chapter, we will introduce Git's workflow.
The following diagram shows Git's workflow:
We can imagine Git's workflow as aDo the homework and hand it in to the teacherprocess. Combined with the diagram above, we can divide this workflow into four main parts:
1. Working Directory —— your desk
this is youActual modified filesplace where you write code, make deletions and modifications.
- action: You write new features or fix bugs here.
2. Staging Area —— your basket for outgoing mail
When you feel your work is mostly done, you put it into a basket, ready to be packaged.
- Key commands:
git add - Meaning: Tell Git that you confirm these changes are to be committed, and ask it to remember them for now.
3. Local Repository —— your personal safe
You pack up the things in the basket, attach a label (commit message), and lock it in your own safe.
- Key commands:
git commit - Meaning: The changes officially become part of the project history. Even if you mess things up later, you can always restore them from here.
4. Remote —— your teacher's inbox (e.g., GitHub/GitLab)
Finally, you send the code in the safe to a remote server over the network, so others can view or collaborate.
- Key commands:
git push - Meaning: Back up the code and share progress with the team.
Knowledge point explanation
- git stash (stash area): You're halfway through your work, and suddenly you need to work on another urgent task, but you don't want to commit the unfinished work. At this point, you can first use
stashto stash the code in a drawer, then take it out and continue working on it after you're done (pop)。 - git pull (pull): Check whether others have submitted new work on the teacher's side (remote repository), and directly sync it to your desk.
- git fetch & merge: First check what updates the remote has (
fetch), after confirming there are no issues, merge (merge) into your own code.
Brief summary:
Modify code →add(put into basket) →commit(store in box) →push(send to a distant place).
Command description
1. Clone the repository
If you want to participate in an existing project, you first need to clone the remote repository to your local machine:
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, you usually create a new branch:
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 the modified files to the staging area, so that you can proceed with the next commit operation:
git add filename # 或者添加所有修改的文件 git add .
5. Commit changes
Commit the staged changes to the local repository and add a commit message:
git commit -m "Add new feature"
6. Pull latest changes
Before pushing local changes, it is best to pull the latest changes from the remote repository to avoid conflicts:
git pull origin main # 或者如果在新的分支上工作 git pull origin new-feature
7. Push changes
Push local commits to the remote repository:
git push origin new-feature
8. Create Pull Request (PR)
Create a Pull Request on GitHub or other hosting platforms to invite 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 reviewed and merged, you can merge the main branch of the remote repository into the local branch:
git checkout main git pull origin main git merge new-feature
10. Delete branch
If the feature branch is no longer needed, you can delete it:
git branch -d new-feature
Or delete a branch from the remote repository:
git push origin --delete new-featureother extensions