Seeds
(Updated )
Git Commands
Git Commands
Start
Create your repository in GitHub, name it “your-repo-name”.
cd into the folder.
To establish who is working on these files
git config --global user.name "your-name"
posted with your commits
git config --global user.email "your-email"
To set the color of git in CLI (optional)
git config --global color.ui auto
To establish a local repository on your pc
git init
Establish the remote repository
git remote add origin [https://github.com/your-username/your-repo-name.git]
Add and commit
git add .
git commit -m "Your first meaningful commit message"
Push to GitHub (use -u only on the first push)
git push -u origin main
# Git Commands Cheat Sheet
## Setup & Config
```bash
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --list
Initialize & Clone
git init # Initialize a new repo
git clone <url> # Clone a remote repo
git clone <url> <folder> # Clone into a specific folder
Staging & Committing
git status # Show working tree status
git add <file> # Stage a file
git add . # Stage all changes
git commit -m "message" # Commit with message
git commit --amend # Amend the last commit
Branching
git branch # List branches
git branch <name> # Create a branch
git checkout <name> # Switch to a branch
git checkout -b <name> # Create and switch
git switch <name> # Switch (modern)
git switch -c <name> # Create and switch (modern)
git branch -d <name> # Delete a branch
git branch -D <name> # Force delete a branch
Merging & Rebasing
git merge <branch> # Merge branch into current
git rebase <branch> # Rebase onto branch
git rebase -i HEAD~<n> # Interactive rebase last n commits
git cherry-pick <commit> # Apply a specific commit
Remote
git remote -v # List remotes
git remote add origin <url> # Add a remote
git remote remove <name> # Remove a remote
git fetch # Fetch all remotes
git pull # Fetch and merge
git pull --rebase # Fetch and rebase
git push origin <branch> # Push branch to remote
git push -u origin <branch> # Push and set upstream
git push --force-with-lease # Safer force push
Stash
git stash # Stash changes
git stash pop # Apply and drop last stash
git stash apply # Apply without dropping
git stash list # List all stashes
git stash drop stash@{n} # Drop a specific stash
git stash clear # Clear all stashes
Viewing History
git log # Full log
git log --oneline # Compact log
git log --oneline --graph # Visual branch graph
git log --author="name" # Filter by author
git diff # Unstaged changes
git diff --staged # Staged changes
git show <commit> # Show a commit
Undoing Changes
git restore <file> # Discard working dir changes
git restore --staged <file> # Unstage a file
git reset HEAD~1 # Undo last commit (keep changes)
git reset --hard HEAD~1 # Undo last commit (discard changes)
git revert <commit> # Create a revert commit
git clean -fd # Remove untracked files/dirs
Tags
git tag # List tags
git tag <name> # Create lightweight tag
git tag -a <name> -m "msg" # Create annotated tag
git push origin <tag> # Push a tag
git push origin --tags # Push all tags
Global Config & Variables
git config --global --list # List all global settings
git config --list --show-origin # List all settings with their source file
git config --global user.name # Get a specific global value
git config --global user.email # Get global email
git config --global core.editor # Get default editor
git config --global alias.<name> # Get a specific alias
git config --global --unset <key> # Remove a global setting
Config is stored in
~/.gitconfig. You can also edit it directly with:git config --global --edit
Useful Shortcuts
git shortlog -sn # Commit count per author
git blame <file> # Line-by-line authorship
git bisect start # Start binary search for bug
git reflog # Show all HEAD movements
git archive --format=zip HEAD > out.zip # Export repo as zip