Git Notes: Comprehensive Guide with Examples
1. What is Git?
• Git is a version control system for tracking changes in code/projects.
• Helps multiple people work on the same project without overwriting each other’s work.
• Allows branching, merging, reverting changes, and keeping history.
2. Git Setup
git --version # Check if Git is installed
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
• user.name and user.email identify you in commits.
3. Initializing a Repository
git init # Start Git in current folder
git clone <url> # Copy an existing repo from remote
• git init → creates .git folder that tracks changes.
• git clone → gets a copy of a remote repository.
4. Checking Status
git status # Shows changes staged, unstaged, or untracked
git log # Shows commit history
git log --oneline # Compact one-line view
git log --graph --all # Visualizes branches and merges
5. Tracking Changes
git add <file> # Stage file for commit
git add . # Stage all changes
git commit -m "message" # Commit staged changes
• Example:
echo "Hello Git" > test.txt
git add test.txt
git commit -m "Add test.txt file"
6. Undoing Changes
• Undo unstaged changes:
git checkout -- <file>
• Unstage changes:
git reset <file>
• Revert a commit (creates a new commit that undoes the change):
git revert <commit_hash>
• Reset history (dangerous, rewrites history):
git reset --hard <commit_hash>
7. Branching
• Branches allow parallel versions of your project.
git branch # List branches
git branch <name> # Create a new branch
git checkout <name> # Switch to branch
git checkout -b <name> # Create + switch branch
Example:
git checkout -b feature_login
8. Merging
• Combine changes from one branch into another.
git checkout master
git merge feature_login
• If the same file is changed in both branches → conflict.
• Resolve conflict manually, then:
git add <file>
git commit -m "Resolved merge conflict"
9. Stashing Changes
• Save uncommitted changes temporarily.
git stash
git stash pop # Apply saved changes
git stash list # List stashed changes
Use case: You’re working on a file but need to switch branches quickly.
10. Working with Remotes
• Push/pull changes to/from GitHub, GitLab, or other remote repositories.
git remote add origin <url> # Link local repo to remote
git push -u origin master # Push master to remote
git pull origin master # Pull latest changes from remote
git fetch # Fetch changes without merging
11. Viewing Differences
git diff # Shows unstaged changes
git diff --staged # Shows staged changes
git show <commit_hash> # See changes in a specific commit
12. Tagging
• Mark versions/releases in your project.
git tag v1.0 # Tag current commit
git tag -a v1.1 -m "Version 1.1" # Annotated tag
git push origin --tags # Push tags to remote
13. Rebasing
• Move branch commits onto another base commit.
• Keeps history linear.
git checkout feature
git rebase master
• Conflicts may occur; resolve them manually and continue:
git rebase --continue
14. Cherry-pick
• Apply a specific commit from another branch:
git cherry-pick <commit_hash>
• Useful when you need one feature or bugfix without merging the entire branch.
15. Ignoring Files
• Use .gitignore to exclude files/folders:
*.pyc
__pycache__/
venv/
.env
16. Advanced Tips
• View commit history graphically:
git log --oneline --graph --all
• Undo last commit without deleting changes:
git reset --soft HEAD~1
• Delete branch:
git branch -d <branch>
git branch -D <branch> # Force delete
• Rename branch:
git branch -m old_name new_name