📘 Git & GitHub Notes
🔹 What is Git & GitHub?
Git: A distributed version control system that tracks changes in source code during
software development.
GitHub: A cloud-based hosting service for Git repositories. It enables collaboration,
code sharing, and pull requests.
🔹 Important Git Commands
# Basic Configuration
git config --global user.name "Your Name"
git config --list
# Repository Setup
git init # Initialize new repo
git clone <url> # Clone repo
git status # Check status
git log --oneline # View commit history
# Staging & Committing
git add <file> # Stage file
git add . # Stage all files
git commit -m "message" # Commit changes
git commit -am "message" # Add + commit tracked files
# Undoing
git checkout -- <file> # Discard file changes
git reset <file> # Unstage file
git reset --hard HEAD # Reset repo
git revert <commit-id> # Undo commit safely
# Remote Operations
git remote -v
git remote add origin <url>
git push -u origin main
git pull origin main
git fetch
🔹 Steps to Push a Project on GitHub
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/username/repo-name.git
git push -u origin main
👉 Later updates:
git add .
git commit -m "update message"
git push
🔹 Branching in Git
✦ What is Branching?
Branching means creating a separate line of development. It helps developers:
Work on new features without breaking main code.
Support parallel development.
Experiment safely.
Merge changes back to main after testing.
✦ Branching Commands
# Create branch
git branch feature-1
# Create + switch (shortcut)
git checkout -b feature-1
# or
git switch -c feature-1
# List branches
git branch
# Switch branch
git checkout main
# or
git switch main
# Delete branch
git branch -d feature-1
# Push branch to GitHub
git push -u origin feature-1
# Merge branch into main
git checkout main
git merge feature-1
. What is the difference between local and remote repository?
👉 A local repo exists on your machine, while a remote repo (like on GitHub) is stored on a
server for collaboration.
Q4. Difference between git clone and git fork?
👉 git clone copies a repository to your system. Fork creates a copy of someone else’s repo
in your GitHub account.
🔸 Commands
Q5. What is the difference between git fetch and git pull?
👉 git fetch downloads changes but doesn’t merge them. git pull downloads and merges
changes into your branch.
Q6. What is the difference between git add and git commit?
👉 git add stages changes, git commit saves them permanently in the repository.
Q7. How do you discard changes in a file?
👉 Use git checkout -- <file> to discard uncommitted changes.
🔸 Branching & Merging
Q8. What is a branch in Git? Why is it useful?
👉 A branch is a separate line of development. It’s useful for working on new features or bug
fixes without affecting the main code.
Q9. Difference between git merge and git rebase?
👉 git merge combines branches and keeps history. git rebase rewrites commits on top of
another branch, giving a cleaner history.
Q10. How do you resolve merge conflicts?
👉 Edit the conflicting files manually, then run git add <file> and git commit.
Q11. What is a detached HEAD state in Git?
👉 It happens when you checkout a specific commit instead of a branch, so changes don’t
belong to any branch.