阿里云主机折上折
  • 微信号
Current Site:Index > Overview of Git workflow

Overview of Git workflow

Author:Chuan Chen 阅读数:28063人阅读 分类: 开发工具

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:

  1. Initialize a Repository
    git init
    
  2. 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

  1. Pull the latest code:
    git pull origin main
    
  2. 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

  1. Create a feature branch:
    git checkout -b feature/user-auth
    
  2. Push to remote after development:
    git push origin feature/user-auth
    
  3. 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 code
  • develop: Latest integrated development content
  • feature/*: Feature development branches
  • release/*: Pre-release branches
  • hotfix/*: Emergency fix branches

Example Operations

  1. Create a feature branch based on develop:
    git checkout -b feature/payment develop
    
  2. Create a release branch for a new version:
    git checkout -b release/1.0.0 develop
    
  3. 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

  1. Fork the remote repository to your personal account.
  2. Clone the forked repository:
    git clone https://github.com/yourname/project.git
    
  3. Add the upstream repository (original project):
    git remote add upstream https://github.com/original/project.git
    
  4. 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

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.