Git Cheat Sheet
🌐 Remote Operations
Manage remote repositories and interactions:
- Clone a Repository:
git clone [url]
- Pull Changes:
git pull [remote] [branch]
- Push Changes:
git push [remote] [branch]
- Show Remotes:
git remote -v
- Add a Remote Repository:
git remote add [remote-name] [url]
- Fetch Changes:
git fetch [remote-name]
- Push to All Remotes:
git push --all [remote-name]
- Delete a Remote Branch:
git push origin -d [branch-name]
- Rename a Remote:
git remote rename [old-name] [new-name]
- Remove a Remote:
git remote remove [remote-name]
- Fetch All Remotes:
git fetch --all
- Prune Remote-tracking Branches:
git fetch --prune
📦 Work with Index
Stage, commit, and manage changes:
- Add Files to Staging:
git add [file]
git add .
- Commit Changes:
git commit
- Amend Last Commit:
git commit --amend
🚀 Moving HEAD and Merging
Switch branches, check out commits, merge, and rebase changes:
- Switch Branches:
git checkout [branch-name]
- Create and Switch Branch:
git checkout -b [branch-name]
- Check Out a Commit:
git checkout [commit-hash]
- Merge Branches:
git merge [branch-name]
- Abort Merge:
git merge --abort
- Interactive Rebase:
git rebase -i [base-commit]
- Abort Rebase:
git rebase --abort
- Cherry-pick a Commit:
git cherry-pick [commit-hash]
📚 Info/Logging/Commit Tree Show
View logs, diffs, and commit history:
- Show Commit History:
git log
- Condensed Commit History:
git log --oneline
- Graphical Commit History:
git log --graph
- Filter by Author:
git log --author="Author Name"
- Decorated Log:
git log --decorate
- Log All Branches:
git log --all
- Mnemonic Log:
git log --all --decorate --oneline --graph # 'adog'
- Show Changes:
git diff [commit-hash1] [commit-hash2]
🏷️ Git Tagging
Manage and use tags for marking specific points in history:
- Create a Tag:
git tag [tag-name]
- List Tags:
git tag
- Push Tags:
git push [remote-name] --tags
- Annotated Tag:
git tag -a [tag-name] -m "Tag message"
- Show Tag Information:
git show [tag-name]
⚙️ Options/Configs
Set configurations:
- Set Global Configuration:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
- List Configurations:
git config --list
🛠️ Advanced and Miscellaneous
Handle advanced scenarios, large files, and troubleshoot:
- Track Large Files (Git LFS):
git lfs track "[file-pattern]"
- List Tracked Large Files (Git LFS):
git lfs ls-files
💡 Note: Git LFS is used for handling large files efficiently.
-
Git Hooks:
💡 Note: Customize Git hooks for automated scripts on certain Git actions. Located in
.git/hooks/
. -
Optimize the Local Repository:
git gc
🧠 Understanding Commit References
Navigate through commits using various references:
- HEAD: Refers to the current branch’s latest commit.
- HEAD~[n]: Refers to the commit n steps before the HEAD.
- HEAD^[n]: Refers to the n-th parent of the commit pointed to by HEAD (useful in merge commits).
🔍 Git Internals
Explore the underlying mechanics of Git:
- .git Directory: Where Git stores all repository metadata.
- Objects: The core building blocks in Git’s storage (blobs, trees, commits, tags).
- Refs: References to commit objects (branches, HEAD, tags).
- Index: The staging area where changes are recorded before committing.