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