Git Cheat Sheet

 1. Introduction to Git

Definition: Git is a distributed version control system that tracks changes in files and allows multiple people to collaborate on a project.

  • Distributed: Each developer has a full copy of the repository, including its history.

  • Benefits: Keeps track of code history, enables collaboration, allows branching and merging, can revert changes safely.

  • Alternatives: SVN, Mercurial, CVS


 

2. Git Installation and Setup

Windows: Git for Windows
Mac: brew install git
Linux: sudo apt-get install git OR sudo yum install git

Configure Git:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Check Configuration:
git config --list


 

3. Basic Git Concepts

  • Repository (Repo): A project folder tracked by Git.

  • Working Directory: Your current project files.

  • Staging Area (Index): Temporary area to prepare files for commit.

  • Commit: Snapshot of changes.

  • Branch: Independent line of development.


 

4. Git Basic Commands (Beginner)

git init — Initialize repository
git clone <repo_url> — Clone repository
git status — Check status
git add <file_name> — Add file
git add . — Add all files
git commit -m "Commit message" — Commit changes
git log — View history
git log --oneline --graph --all — Compact history graph
git diff — Show unstaged changes
git diff --staged — Show staged changes


 

5. Branching and Merging (Intermediate)

git branch <branch_name> — Create branch
git checkout <branch_name> OR git switch <branch_name> — Switch branch
git checkout -b <branch_name> OR git switch -c <branch_name> — Create + Switch branch
git merge <branch_name> — Merge branch
git branch -d <branch_name> — Delete branch
git branch -D <branch_name> — Force delete branch


 

6. Remote Repositories

git remote add origin <repo_url> — Add remote
git push -u origin <branch_name> — Push branch
git pull — Pull changes
git fetch — Fetch changes
git remote -v — View remotes


 

7. Undoing Changes

git reset HEAD <file> — Unstage file
git checkout -- <file> — Discard changes in working directory
git revert <commit_id> — Revert commit
git reset --hard <commit_id> — Reset to previous commit


 

8. Git Tags

git tag <tag_name> — Create tag
git tag — List tags
git push origin <tag_name> — Push tag
git push origin --tags — Push all tags


 

9. Advanced Git Concepts

git stash                               — Save changes
git stash list — List stashes
git stash apply — Apply stash
git stash pop — Apply + remove stash
git cherry-pick <commit_id> — Apply specific commit
git rebase <branch_name> — Rebase commits
git rebase -i HEAD~<n> — Interactive rebase
Resolving Conflicts: Edit conflicting files → git add <file> → commit or continue rebase


 

10. Git Workflows

Centralized Workflow     : Single shared branch (like main).
Feature Branch Workflow : Develop features in separate branches.
Gitflow Workflow: main → production-ready, develop → development (feature, release, hotfix branches).
Forking Workflow: Fork repo, clone, make PRs for collaboration.

Leave a Comment