Git & GitHub

Professional Reference Guide for Version Control and Collaboration

📚 Overview

Git is a distributed version control system that allows you to track changes in your project history and work with different versions efficiently.

GitHub is a cloud-based platform that hosts Git repositories and facilitates collaboration, code sharing, and project management.

🚀 Initial Setup & First Commit

Quick Start Workflow

  1. Initialize a new Git repository
  2. Stage your files
  3. Create your first commit
  4. Connect to remote repository
  5. Push to GitHub
git init
Initialize a new Git repository in the current directory
git add .
Stage all files for commit
git commit -m "Initial commit"
Create your first commit with a descriptive message
git branch -M main
Rename the default branch to 'main'
git remote add origin https://github.com/USERNAME/REPO_NAME.git
Connect your local repository to a remote GitHub repository
git push -u origin main
Push your code to GitHub and set upstream tracking

🔄 Remote Repository Management

⚠️ Important Note

If you created a repository on GitHub with README, license, or .gitignore files, you'll need to pull those changes first before pushing your local code.

git pull origin main --allow-unrelated-histories
Pull remote changes when repositories have unrelated histories
git remote -v
View all configured remote repositories
git remote remove origin
Remove the remote repository connection

🌿 Branch Management

git branch
List all local branches
git branch -a
List all branches (local + remote)
git checkout -b feature-branch
Create and switch to new branch
git switch -c branch-name
Modern way to create and switch

🔀 Merging & Collaboration

git merge feature-branch
Merge branch into current branch
git push -u origin branch-name
Push new branch to remote
git branch -d branch-name
Delete merged branch
git branch -D branch-name
Force delete unmerged branch

📥 Repository Operations

git clone https://github.com/username/repository-name.git
Clone a remote repository to your local machine

📊 Status & History

git status
Check working directory status
git log
View detailed commit history

🎯 Typical Development Workflow

  1. git pull origin main - Get latest changes
  2. git checkout -b feature/new-feature - Create feature branch
  3. Make your changes and save files
  4. git add . - Stage changes
  5. git commit -m "Add new feature" - Commit changes
  6. git push -u origin feature/new-feature - Push to remote
  7. Create Pull Request on GitHub
  8. Merge after review and delete feature branch