GIT Cheat Sheet
Last updated by Nabeel Hassan
Setup
- Set user details
git config --global user.name "Your Name"git config --global user.email "your.email@example.com"- Check config
git config --listCreating and Cloning Repositories
- Initialize a new Git repository
git init- Clone an existing repository
git clone <repository-url>Basic Commands
- Check the status of the working directory
git status- Add files to staging area
- Add specific file
git add <file>- Add all files
git add .- Commit changes
git commit -m "Commit message"Branching and Merging
- List branches
git branch- Create a new branch
git branch <branch-name>- Switch to a branch
git checkout <branch-name>- Create and switch to a new branch
git checkout -b <branch-name>- Merge a branch into the current branch
git merge <branch-name>Remote Repositories
- Add a remote repository
git remote add origin <repository-url>- Push changes to remote
git push origin <branch-name>- Pull latest changes from remote
git pull origin <branch-name>Undoing Changes
- Unstage a file
git reset HEAD <file>- Discard local changes
git checkout -- <file>- Revert a commit
git revert <commit-hash>Log and History
- Show commit history
git log- Show commit history in one line
git log --onelineStashing
Save uncommitted changes
git stash- Apply the latest stashed changes
git stash popTags
- List all tags
git tag- Create a new tag
git tag -a v1.0 -m "Version 1.0"- Push tags to remote
git push origin --tagsSubmodules
- Add a submodule
git submodule add <repository-url> <path>- Initialize submodules
git submodule update --init --recursiveAdvanced Commands
- Squash commits during interactive rebase
git rebase -i HEAD~<number-of-commits>- Amend the last commit message
git commit --amend -m "New commit message"- Hard reset to a specific commit
git reset --hard <commit-hash>This cheat sheet provides a quick reference to essential Git commands for everyday development.