What is a Git Workflow?¶
In collaborative development, Git is not just a version control tool but also the “traffic rules” for team collaboration. A Workflow is a set of agreed-upon rules for submitting code, merging branches, and managing versions. It ensures ordered code submissions, simplifies issue tracking, and prevents chaos from multiple people modifying the same file simultaneously.
Common Branch Strategies (Simplified Version)¶
For beginners, the Simplified Git Flow is most recommended. Its core ideas are:
- Main Branch (usually main or master): Always holds deployable, stable code. Only accepts merges of completed features.
- Feature Branches: Created from the main branch for developing new features or fixing bugs, with each feature in its own branch.
- Merge Rules: After a feature is complete, merge it into the main branch for release (testing required before merging).
Must-Know Basic Commands (with Scenarios)¶
| Command | Purpose | Scenario Example |
|---|---|---|
git clone <repository-url> |
Clone a remote repository locally | First time getting project code |
git checkout -b <branch-name> |
Create and switch to a new branch | Create a feature branch: git checkout -b feature/login |
git add . |
Stage modified files | After coding, stage all changes first |
git commit -m "Commit message" |
Commit staged changes | Write a clear description: git commit -m "Complete login form UI" |
git status |
Check workspace status | Regularly verify uncommitted changes |
git pull origin <main-branch> |
Pull latest code from the main branch | Ensure your feature branch stays in sync |
git merge <branch-name> |
Merge branches | Merge a feature branch into the main branch |
git push origin <branch-name> |
Push branch to remote repository | Push local branch to GitHub/GitLab |
Complete Workflow Steps (Example: “Develop Login Function”)¶
1. Preparation: Ensure the main branch is up-to-date¶
Assume the main branch is named main. First, confirm your local main branch is latest to avoid missing updates when creating new branches:
# Switch to the main branch
git checkout main
# Pull latest code from the remote repository
git pull origin main
2. Create a Feature Branch¶
Create a feature branch from main (e.g., for developing the login function). Naming convention: feature/function-name:
git checkout -b feature/login
This command is equivalent to:
git branch feature/login(create branch) +git checkout feature/login(switch branch)
3. Develop and Commit Code¶
Write code on the feature/login branch and commit when done:
# Check which files were modified
git status
# Stage all changes (or use `git add <filename>` for specific files)
git add .
# Commit with a clear message
git commit -m "Implement login form input fields and buttons"
Commit Message Tip: Clearly state “what was done” (e.g., “Fix login button unresponsive”, “Add captcha logic”) to simplify rollbacks or history checks.
4. Sync with Main Branch Updates (Avoid Conflicts)¶
If others have pushed new code to the main branch (e.g., fixed a bug), update your feature branch to prevent merge conflicts:
# Switch back to main and pull latest code
git checkout main
git pull origin main
# Switch back to your feature branch and merge main updates
git checkout feature/login
git merge main
If no conflicts, Git merges automatically. If conflicts occur, you’ll see “Automatic merge failed” (see “Conflict Resolution” section).
5. Push Feature Branch to Remote Repository¶
After developing the feature, push it to the remote repository for team review:
git push origin feature/login
After pushing, you can view the branch on GitHub/GitLab and create a “Merge Request (PR)” to the main branch.
6. Merge to Main Branch (Release)¶
After code review and approval, merge the feature branch into main:
- Local Merge (if working directly in the terminal):
# Switch to main branch
git checkout main
# Merge the feature branch
git merge feature/login
# Push merged main branch to remote
git push origin main
- Merge via PR (more common for team collaboration): On GitHub/GitLab, create a PR from
feature/logintomain, then delete the feature branch after merging.
7. Clean Up Completed Branches¶
Delete local and remote feature branches after merging to keep the repository tidy:
# Delete local feature branch
git branch -d feature/login
# Delete remote feature branch
git push origin --delete feature/login
Conflict Resolution (Must-Know for Beginners)¶
When Conflicts Occur: When multiple people modify the same line of a file, Git can’t automatically decide which version to keep.
Resolution Steps:
1. Git will flag conflicted files (e.g., login.js). Open the file and find conflict markers:
<<<<<<< HEAD (Current branch's code)
const username = "admin";
======= (Separator)
const username = "user";
>>>>>>> feature/login (Code from the branch being merged)
- Manually edit the file to decide which code to keep (or adjust logic), then remove the conflict markers (
<<<<<<<,=======,>>>>>>>). - Mark as resolved and commit:
git add login.js
git commit -m "Resolve login function branch conflicts with main"
Summary and Best Practices¶
- Branch Naming:
mainfor stable code,feature/xxxfor features,bugfix/xxxfor fixes. - Pre-Commit Check: Use
git statusto verify changes and avoid committing unintended files (e.g., temporary files). - Small, Frequent Commits: Commit “one small feature” at a time (e.g., “Implement login form layout”, “Fix password field styling”) for easier tracking.
- Keep Main Branch Clean: Only merge tested code. Never write or commit directly to
main.
Once you master this workflow, you’ll efficiently manage code versions and streamline team collaboration. When stuck, consult git --help or the official Git documentation to familiarize yourself with commands!