π’ Beginner
- Install Git
- git config --global user.name "Your Name"
- git config --global user.email "[email protected]"
- Set default branch:
init.defaultBranch main - Set default editor: VS Code, Vim, etc.
- Enable color UI
- View current config:
git config --list - View current config:
git config --list --global
- git init - Initialize a new Git repository.
- git status - Check the status of files β modified, staged, untracked.
- git add / git add . - Stage file(s) to be committed.
- git commit -m "message" - Commit staged changes with a message.
- git log / git log --oneline - View commit history (detailed or short).
- git diff - See whatβs changed before staging.
- git show - Show the details of a specific commit.
- Use .gitignore - Ignore files you donβt want Git to track (e.g., node_modules/, env, dist).
- Branching
-
git branchβ List all local branches -
git branch <name>β Create a new branch -
git switch <branch>β Switch to a branch (newer) -
git checkout <branch>β Switch to a branch (older method) -
git checkout -b <name>β Create and switch to a new branch -
git branch -d <name>β Delete a local branch (safe) -
git branch -D <name>β Delete a local branch (force) -
git branch -m <new>β Rename current branch -
git branch -vvβ Show branches with last commit info -
git push origin <branch>β Push branch to remote -
git push --set-upstream origin <branch>β Link local branch to remote -
git fetch --allβ Fetches all branches from all remotes (usually just origin) but does NOT merge or checkout anything. -
git fetch -pβ It tells Git to remove (prune) any remote-tracking branches that no longer exist on the remote.
- Merging Git Merge is used to combine changes from one branch into another. Usually, you merge a feature branch into a main/stable branch.
π¦ Merge Types in Git
| Merge Type | Description |
|---|---|
| π’ Fast-forward Merge | Simple move of the pointer if thereβs no diverging history. |
| π‘ Recursive Merge | The default merge strategy when branches have diverged. |
π΅ No Fast-forward Merge (--no-ff) |
Always creates a merge commit to preserve history. |
| π΄ Manual Merge | Required when thereβs a conflict. |
β³οΈ Common Merge Commands
-
git merge <branch>β Merge the given branch into the current branch -
git merge --no-ff <branch>β Force a merge commit even on fast-forward -
git merge --squash <branch>β Merge and squash into a single commit -
git merge --abortβ Abort a merge in progress -
git merge --continueβ Continue merge after resolving conflicts
- git remote add origin
- git push -u origin main
- git push / git pull
- git clone
- git remote -v
- git push --set-upstream origin
- git branch -vv
- git branch --set-upstream-to
- git reset --soft / --mixed / --hard
- git restore
- git restore .
- git restore --staged
- git commit --amend
- git reflog
- git clean -fd
- git reset --merge (undo merge)
π‘ Intermediate
-
git cherry-pick <commit>β Apply specific commit from another branch -
git cherry-pick commit1 commit2 commit3β You can also cherry-pick multiple commits -
git cherry-pick A^..Bβ even a range of commits
git rebase moves or reapplies commits from one branch on top of another. It rewrites history to create a cleaner, linear commit history.
π― When to Use Rebase β Clean up history β Avoid extra merge commits β Keep project history linear β Re-apply your local feature branch on top of main
-
git rebase <branch>β Reapply current branch commits on top of<branch> -
git rebase -i <branch>β Interactive rebase: squash, reword, drop commits -
git rebase -i HEAD~nβ Interactively edit the lastncommits -
git rebase --autosquashβ Auto-squash commits marked withfixup!orsquash! -
git rebase --continueβ Continue rebase after resolving conflicts -
git rebase --abortβ Abort the rebase and return to the previous state -
git rebase --skipβ Skip the current conflicting commit during rebase -
git pull --rebaseβ Pull latest changes using rebase instead of merge -
git statusβ Check status during rebase (conflicts, progress) -
git logβ Review commit history before/after rebase - Resolve conflicts manually β Fix file conflicts and mark as resolved
| Command | git rebase -i <branch> Meaning |
|---|---|
pick |
Keep commit as-is |
reword |
Change commit message |
edit |
Pause to change content |
squash |
Combine with previous commit (keep both messages) |
fixup |
Combine with previous, discard this message |
drop |
Delete this commit |
- git tag
- git tag -a -m "message"
- git show
- git push origin
- git push --tags
-
git stashβ Stash tracked modified files -
git stash -uβ Stash tracked + untracked files (not ignored) -
git stash -aβ Stash all (tracked + untracked + ignored) -
git stash listβ View list of stashes -
git stash showβ Show summary of latest stash -
git stash show -pβ Show patch/diff of latest stash -
git stash popβ Apply stash and delete it -
git stash applyβ Apply stash but keep it -
git stash dropβ Delete latest stash -
git stash clearβ Delete all stashes -
git stash push -m "message"β Stash with custom message
- git log with formatting
- git diff --cached
- git blame
- git describe
- git shortlog
- git shortlog -sn
- Use Pull Requests / Merge Requests
- Feature branch workflow
- Gitflow workflow
- Code review & approvals
- Protected branches
- Squash commits before merge
- Setup Husky for hooks
- Use pre-commit, commit-msg, post-merge
- Use lint-staged for auto formatting
- Create .husky/ scripts
- Use
git commit --no-verify(with caution)
- Create git aliases
- Use Git GUI tools
- Use .gitattributes
- Try https://learngitbranching.js.org
- Use GitLens in VS Code
- Use
diff-so-fancyfor better diffs
- Understand .git/ structure
- Git Object Types: blob, tree, commit, tag
- Learn about HEAD, refs, index
- Learn SHA-1 and commit IDs
- Conflict resolution
- Detached HEAD fixes
- Use reflog to recover lost commits
- git bisect start / bad / good (binary search debugging)
- Setup GitHub Actions or GitLab CI
- Configure .github/workflows/
- Use CI for test/lint/build
- Auto deployment with git tags
- Test GitHub Actions locally with
act
- Use Conventional Commits (feat:, fix:, chore:, etc.)
- Install commitlint
- Use Commitizen (cz) for commits
- Validate commits with hooks
- git submodule add
- git submodule update --init
- Learn subtrees (optional)
- Use Nx or Lerna for monorepo
π΄ Advanced
- Use .env files and gitignore
- Install git-secrets
- GPG sign commits
- Enable 2FA on GitHub
- Remove secrets from history (BFG or git filter-branch)
- Simulate team workflows
- Use pair programming tools (git-duet)
- Create/merge PRs
- Trunk-based development
- Fork vs shared branch model
- GitHub
- GitLab
- Bitbucket
- Azure Repos
- git worktree (multiple working dirs)
- git archive (create zip/tar)
- git format-patch / git apply / git am (email-based flow)
- git fame (contribution analytics)
- git LFS for large files
- https://git-scm.com/book
- https://learngitbranching.js.org
- https://ohmygit.org
- Git cheatsheets from GitHub
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"- git commit --fixup and git rebase --autosquash
- git notes β add metadata/comments to commits
- git revert -n (stage multiple reverts)
- Understand orphan branches: git checkout --orphan newbranch
- git gc (garbage collection to optimize repo)
- git repack (compress objects)
- git verify-pack / git count-objects -v (inspect storage)
- Use BFG Repo-Cleaner for large repos
- Audit history for secrets (truffleHog, gitleaks)
- Enforce signed commits via branch protection
- Enforce linear history in main branch
- Use branch naming conventions and enforce them
- Use git sparse-checkout (large monorepos)
- Partial clone (--filter=blob:none) for huge projects
- Split large repos with git subtree split
- Setup custom merge drivers
- Setup .mailmap to unify commit authors
- Use .git/info/exclude for local-only ignores
- Custom diff drivers (e.g., for .psd files)
- Generate contributor graphs (git shortlog, git fame)
- Generate churn stats (git log --stat)
- Track contribution activity with git log --since or GitHub Insights
- git replace to swap objects in history
- git filter-branch for complex rewriting
- Use with caution β avoid on shared history
- Use Git with GitHub CLI (gh)
- Use Git in CI runners and scripts
- Use Git in Docker images for automation
- Access GitHub/GitLab via REST API