Overview of Git workflow
Git is a distributed version control system widely used for code management and collaborative development. Understanding Git workflows helps manage code changes efficiently, reduce conflicts, and ensure smooth team collaboration. Below is an explanation ranging from common workflow patterns and branch strategies to specific operations.
Basic Concepts and Core Workflow
The core workflow of Git revolves around repositories, branches, and commits. Each developer has a complete copy of the repository and synchronizes changes through pulling and pushing. Typical operations include:
- Initialize a Repository
git init
- Clone a Remote Repository
git clone <repository_url>
Centralized Workflow
Suitable for small teams or simple projects, where all developers commit changes directly to the main branch (e.g., main
).
Steps
- Pull the latest code:
git pull origin main
- Commit changes and push:
git add . git commit -m "fix: update login validation" git push origin main
Conflict Resolution
If multiple people modify the same file simultaneously, manually resolve conflicts and re-submit:
git pull origin main
# After resolving conflicts
git add .
git commit -m "resolve merge conflict"
git push origin main
Feature Branch Workflow
Each new feature or fix is developed in an independent branch and integrated into the main branch via a Merge Request or Pull Request after completion.
Example Workflow
- Create a feature branch:
git checkout -b feature/user-auth
- Push to remote after development:
git push origin feature/user-auth
- Create a Pull Request on GitHub/GitLab for team review and merging.
Git Flow Workflow
Defines a strict branch model suitable for complex projects with long-term maintenance. Core branches include:
main
: Stable production codedevelop
: Latest integrated development contentfeature/*
: Feature development branchesrelease/*
: Pre-release brancheshotfix/*
: Emergency fix branches
Example Operations
- Create a feature branch based on
develop
:git checkout -b feature/payment develop
- Create a
release
branch for a new version:git checkout -b release/1.0.0 develop
- Emergency fixes are created directly from
main
:git checkout -b hotfix/login-bug main
Forking Workflow
Common in open-source projects, where contributors first fork the repository, make changes locally, and submit a Pull Request.
Typical Steps
- Fork the remote repository to your personal account.
- Clone the forked repository:
git clone https://github.com/yourname/project.git
- Add the upstream repository (original project):
git remote add upstream https://github.com/original/project.git
- Synchronize upstream changes:
git fetch upstream git merge upstream/main
Rebase vs. Merge
Two ways to integrate branch changes:
Rebase Example
Move the current branch's commits to the latest commit of the target branch:
git checkout feature/search
git rebase main
# After resolving conflicts
git rebase --continue
Merge Example
Preserve branch history and generate a merge commit:
git checkout main
git merge feature/search
Hooks and Automation
Git supports custom scripts triggered by specific events, such as running tests before committing:
# .git/hooks/pre-commit
#!/bin/sh
npm test
Common Issues and Solutions
Undo Local Commits
# Undo the last commit, keeping changes
git reset --soft HEAD~1
# Discard uncommitted changes completely
git reset --hard
Recover a Deleted Branch
git reflog
git checkout -b recovered-branch <commit_hash>
Advanced Techniques
Interactive Rebase
Modify or combine commit history:
git rebase -i HEAD~3
Stash Changes
Temporarily save unfinished changes:
git stash
git stash pop
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:分布式版本控制的特点
下一篇:Git的基本术语解释