مدیریت پروژه با Git و GitHub

راهنمای کامل برای کنترل نسخه و همکاری تیمی

مقدمه

Git و GitHub ابزارهای ضروری برای هر برنامه‌نویس هستند. علی سلمانیان در این مقاله، راهنمای کاملی از Git ارائه می‌دهد که به شما کمک می‌کند تا کد خود را به صورت حرفه‌ای مدیریت کنید. از اصول اولیه تا تکنیک‌های پیشرفته، همه چیز را پوشش می‌دهیم.

Git چیست؟

Git یک سیستم کنترل نسخه توزیع‌شده است که:

  • تاریخچه تغییرات کد را نگهداری می‌کند
  • امکان همکاری تیمی را فراهم می‌کند
  • از Branch ها برای کار موازی استفاده می‌کند
  • امکان بازگشت به نسخه‌های قبلی را می‌دهد
  • کد را در سرورهای مختلف ذخیره می‌کند

🔄 مزایای Git:

  • Version Control: کنترل کامل بر تاریخچه کد
  • Branching: کار موازی روی ویژگی‌های مختلف
  • Collaboration: همکاری آسان با تیم
  • Backup: پشتیبان‌گیری خودکار از کد
  • Merge: ادغام آسان تغییرات

نصب و راه‌اندازی Git

1. نصب Git

برای شروع، باید Git را نصب کنید:

# Windows
# دانلود از https://git-scm.com/download/win

# macOS
brew install git

# Ubuntu/Debian
sudo apt update
sudo apt install git

# CentOS/RHEL
sudo yum install git

# بررسی نصب
git --version

2. تنظیمات اولیه

# تنظیم نام کاربری
git config --global user.name "علی سلمانیان"

# تنظیم ایمیل
git config --global user.email "alisalmanian1395@gmail.com"

# تنظیم ویرایشگر پیش‌فرض
git config --global core.editor "code --wait"

# تنظیم نام branch پیش‌فرض
git config --global init.defaultBranch main

# تنظیم خط پایان خط (برای Windows)
git config --global core.autocrlf true

# تنظیم خط پایان خط (برای macOS/Linux)
git config --global core.autocrlf input

# مشاهده تنظیمات
git config --list

# مشاهده تنظیمات خاص
git config user.name
git config user.email

مفاهیم اصلی Git

1. Repository

Repository (repo) مکانی است که کد و تاریخچه آن ذخیره می‌شود:

# ایجاد Repository جدید
git init

# کلون کردن Repository موجود
git clone https://github.com/username/repository.git

# کلون کردن با نام خاص
git clone https://github.com/username/repository.git my-project

# کلون کردن branch خاص
git clone -b develop https://github.com/username/repository.git

# کلون کردن فقط آخرین commit
git clone --depth 1 https://github.com/username/repository.git

2. Working Directory, Staging Area, Repository

Git سه ناحیه اصلی دارد:

# وضعیت فایل‌ها
git status

# اضافه کردن فایل به Staging Area
git add filename.txt
git add .                    # همه فایل‌ها
git add *.js                 # فایل‌های .js
git add src/                 # پوشه src

# حذف فایل از Staging Area
git reset filename.txt
git reset                    # همه فایل‌ها

# Commit تغییرات
git commit -m "پیام commit"

# Commit با پیام چندخطی
git commit -m "عنوان commit

توضیحات کامل تغییرات:
- اضافه کردن ویژگی جدید
- رفع باگ در authentication
- بهبود performance"

# اضافه کردن و commit همزمان
git commit -am "پیام commit"

3. Commit History

# مشاهده تاریخچه commits
git log

# تاریخچه کوتاه
git log --oneline

# تاریخچه با نمودار
git log --graph --oneline --all

# تاریخچه فایل خاص
git log filename.txt

# تاریخچه با جزئیات
git log --stat

# تاریخچه با تغییرات
git log -p

# تاریخچه محدود
git log -10                 # 10 commit آخر
git log --since="2023-01-01"
git log --until="2023-12-31"
git log --author="علی سلمانیان"

# مشاهده commit خاص
git show commit-hash
git show HEAD               # آخرین commit
git show HEAD~1             # commit قبل از آخرین

Branch Management

1. ایجاد و مدیریت Branch ها

# مشاهده branch ها
git branch
git branch -a               # همه branch ها (محلی و remote)

# ایجاد branch جدید
git branch feature-login
git branch -b feature-login  # ایجاد و تغییر به branch

# تغییر branch
git checkout feature-login
git switch feature-login     # روش جدید

# تغییر به branch قبلی
git checkout -
git switch -

# حذف branch
git branch -d feature-login  # حذف امن
git branch -D feature-login  # حذف اجباری

# حذف branch از remote
git push origin --delete feature-login

# تغییر نام branch
git branch -m old-name new-name

# ردیابی branch از remote
git branch --set-upstream-to=origin/main main

2. Merge و Rebase

# Merge branch
git checkout main
git merge feature-login

# Merge با پیام
git merge feature-login -m "ادغام ویژگی login"

# Merge بدون fast-forward
git merge --no-ff feature-login

# Rebase
git checkout feature-login
git rebase main

# Interactive rebase
git rebase -i HEAD~3

# ادامه rebase بعد از حل conflict
git rebase --continue

# لغو rebase
git rebase --abort

# Squash commits
git rebase -i HEAD~3
# در ویرایشگر، 'pick' را به 'squash' تغییر دهید

Remote Repository

1. کار با GitHub

# اضافه کردن remote
git remote add origin https://github.com/username/repository.git

# مشاهده remote ها
git remote -v

# تغییر URL remote
git remote set-url origin https://github.com/username/new-repository.git

# حذف remote
git remote remove origin

# Push به remote
git push origin main
git push -u origin main     # تنظیم upstream

# Push همه branch ها
git push --all origin

# Push tags
git push --tags

# Pull از remote
git pull origin main

# Fetch (بدون merge)
git fetch origin

# Merge بعد از fetch
git merge origin/main

2. Clone و Fork

# Clone repository
git clone https://github.com/username/repository.git

# Clone با SSH
git clone git@github.com:username/repository.git

# Clone فقط آخرین commit
git clone --depth 1 https://github.com/username/repository.git

# Clone branch خاص
git clone -b develop https://github.com/username/repository.git

# Fork workflow
# 1. Fork repository در GitHub
# 2. Clone fork شده
git clone https://github.com/your-username/repository.git
# 3. اضافه کردن upstream
git remote add upstream https://github.com/original-username/repository.git
# 4. Sync با upstream
git fetch upstream
git checkout main
git merge upstream/main

Pull Request و Code Review

1. ایجاد Pull Request

# Workflow کامل Pull Request
# 1. ایجاد branch جدید
git checkout -b feature-new-feature

# 2. انجام تغییرات
# ... کد نویسی ...

# 3. اضافه کردن تغییرات
git add .
git commit -m "اضافه کردن ویژگی جدید"

# 4. Push branch
git push origin feature-new-feature

# 5. ایجاد Pull Request در GitHub
# 6. Code Review
# 7. Merge یا Close

# 8. حذف branch بعد از merge
git checkout main
git pull origin main
git branch -d feature-new-feature
git push origin --delete feature-new-feature

2. Code Review Best Practices

# Commit های کوچک و معنادار
git commit -m "اضافه کردن validation برای email"
git commit -m "رفع باگ در calculation"
git commit -m "بهبود performance در database query"

# استفاده از conventional commits
git commit -m "feat: اضافه کردن سیستم authentication"
git commit -m "fix: رفع مشکل در login"
git commit -m "docs: به‌روزرسانی README"
git commit -m "style: فرمت کردن کد"
git commit -m "refactor: بازسازی UserService"
git commit -m "test: اضافه کردن unit test برای UserController"

# Squash commits قبل از merge
git rebase -i HEAD~3
# در ویرایشگر، همه commits را به 'squash' تغییر دهید

Advanced Git Techniques

1. Stash

# ذخیره تغییرات موقت
git stash
git stash push -m "ذخیره تغییرات ناتمام"

# مشاهده stash ها
git stash list

# اعمال stash
git stash apply
git stash pop              # اعمال و حذف

# اعمال stash خاص
git stash apply stash@{0}

# حذف stash
git stash drop stash@{0}
git stash clear            # حذف همه

# ایجاد branch از stash
git stash branch new-branch stash@{0}

2. Cherry-pick

# انتخاب commit خاص
git cherry-pick commit-hash

# انتخاب چند commit
git cherry-pick commit1 commit2 commit3

# انتخاب range
git cherry-pick commit1..commit3

# انتخاب بدون commit
git cherry-pick --no-commit commit-hash

# انتخاب با پیام جدید
git cherry-pick -m 1 commit-hash

3. Reset و Revert

# Soft reset (حفظ تغییرات در staging)
git reset --soft HEAD~1

# Mixed reset (حفظ تغییرات در working directory)
git reset --mixed HEAD~1
git reset HEAD~1           # همان mixed

# Hard reset (حذف همه تغییرات)
git reset --hard HEAD~1

# Reset به commit خاص
git reset --hard commit-hash

# Revert (ایجاد commit جدید که تغییرات را لغو می‌کند)
git revert commit-hash
git revert HEAD            # لغو آخرین commit

# Revert merge commit
git revert -m 1 merge-commit-hash

Git Hooks

1. Pre-commit Hook

#!/bin/sh
# .git/hooks/pre-commit

# اجرای linter
npm run lint
if [ $? -ne 0 ]; then
    echo "Lint errors found. Commit aborted."
    exit 1
fi

# اجرای tests
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

# بررسی commit message
commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}'
if ! grep -qE "$commit_regex" "$1"; then
    echo "Invalid commit message format!"
    echo "Format: type(scope): description"
    exit 1
fi

2. Commit-msg Hook

#!/bin/sh
# .git/hooks/commit-msg

# بررسی طول commit message
if [ $(wc -c < "$1") -lt 10 ]; then
    echo "Commit message too short!"
    exit 1
fi

# بررسی وجود کلمه کلیدی
if ! grep -qE "(feat|fix|docs|style|refactor|test|chore)" "$1"; then
    echo "Commit message must start with type!"
    exit 1
fi

Git Workflow Strategies

1. Git Flow

# Git Flow Workflow
# main: کد production
# develop: کد development
# feature/*: ویژگی‌های جدید
# release/*: آماده‌سازی release
# hotfix/*: رفع باگ‌های فوری

# ایجاد feature branch
git checkout develop
git checkout -b feature/user-authentication

# کار روی feature
# ... کد نویسی ...

# Merge به develop
git checkout develop
git merge --no-ff feature/user-authentication

# ایجاد release branch
git checkout -b release/1.0.0

# Merge به main
git checkout main
git merge --no-ff release/1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"

# Merge به develop
git checkout develop
git merge --no-ff release/1.0.0

2. GitHub Flow

# GitHub Flow (ساده‌تر)
# main: کد production
# feature/*: ویژگی‌های جدید

# ایجاد feature branch
git checkout main
git pull origin main
git checkout -b feature/new-feature

# کار روی feature
# ... کد نویسی ...

# Push و ایجاد Pull Request
git push origin feature/new-feature

# بعد از merge، حذف branch
git checkout main
git pull origin main
git branch -d feature/new-feature

GitHub Actions

1. CI/CD Pipeline

# .github/workflows/ci.yml
name: CI/CD Pipeline

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Run linter
      run: npm run lint
    
    - name: Run tests
      run: npm test
    
    - name: Build
      run: npm run build
    
    - name: Deploy
      if: github.ref == 'refs/heads/main'
      run: npm run deploy

Best Practices

✅ نکات مهم Git:

  • Commit های کوچک: هر commit یک تغییر منطقی
  • پیام‌های معنادار: توضیح واضح تغییرات
  • Branch های کوتاه: کار سریع روی feature ها
  • Code Review: بررسی کد قبل از merge
  • Backup منظم: Push منظم به remote

⚠️ اشتباهات رایج:

  • Commit کردن فایل‌های حساس (passwords, keys)
  • Commit های خیلی بزرگ
  • Merge بدون بررسی
  • حذف branch های مهم
  • فراموش کردن .gitignore

مقالات مرتبط

برای یادگیری بیشتر، مقالات زیر را مطالعه کنید:

نتیجه‌گیری

Git و GitHub ابزارهای ضروری برای هر برنامه‌نویس هستند. با یادگیری اصول و تکنیک‌های پیشرفته، می‌توانید کد خود را به صورت حرفه‌ای مدیریت کنید. علی سلمانیان آماده کمک به شما در پیاده‌سازی Git workflow در پروژه‌هایتان است.

درباره نویسنده

علی سلمانیان - برنامه نویس وب و اندروید با تخصص در Git و Version Control. متخصص DevOps، CI/CD و مدیریت پروژه.

📧 alisalmanian1395@gmail.com | 📱 +98 938 822 2808