GIT Cheat Sheet
· 2 min read
Setup
- Set user details
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
- Check config
git config --list
Creating 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 --oneline
Stashing
Save uncommitted changes
git stash
- Apply the latest stashed changes
git stash pop
Tags
- 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 --tags
Submodules
- Add a submodule
git submodule add <repository-url> <path>
- Initialize submodules
git submodule update --init --recursive
Advanced 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.