# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Clone the repository
git clone https://github.com/team/project.git
cd project
# Check remote connections
git remote -v| Command | Description | Example |
|---|---|---|
git status |
Check working directory status | git status |
git add |
Stage changes for commit | git add . or git add filename.js |
git commit |
Commit staged changes | git commit -m "Add new feature" |
git pull |
Fetch and merge remote changes | git pull origin main |
git push |
Push commits to remote | git push origin feature-branch |
git fetch |
Download remote changes without merging | git fetch origin |
git log |
View commit history | git log --oneline --graph |
git diff |
Show changes between commits | git diff HEAD~1 |
List, create, or delete branches
# List all branches
git branch -a
# Create new branch
git branch feature-name
# Delete local branch
git branch -d feature-name
# Delete remote branch
git push origin --delete feature-name
# Rename branch
git branch -m old-name new-nameSwitch branches or restore files
# Switch to existing branch
git checkout feature-branch
# or (newer syntax)
git switch feature-branch
# Create and switch to new branch
git checkout -b new-feature
# or
git switch -c new-feature
# Checkout remote branch
git checkout -b feature-branch origin/feature-branchMerge branches together
# Merge feature branch into current branch
git merge feature-branch
# Merge with no fast-forward (creates merge commit)
git merge --no-ff feature-branch
# Abort merge in case of conflicts
git merge --abortgit checkout -b feature/user-authenticationgit add .
git commit -m "Add login functionality"git fetch origin
git merge origin/main
# or rebase
git rebase origin/maingit push -u origin feature/user-authenticationgit checkout main
git pull origin main
git branch -d feature/user-authenticationManage remote repositories
# View remotes
git remote -v
# Add remote
git remote add upstream https://github.com/original/repo.git
# Remove remote
git remote remove upstream
# Rename remote
git remote rename origin upstream
# Update remote URL
git remote set-url origin https://github.com/new/repo.gitTemporarily save uncommitted changes
# Save current changes
git stash
# or with message
git stash save "Work in progress on feature X"
# List stashes
git stash list
# Apply most recent stash
git stash apply
# or specific stash
git stash apply stash@{2}
# Apply and remove stash
git stash pop
# Delete stash
git stash drop stash@{1}
# Clear all stashes
git stash clearReapply commits on top of another base
# Rebase current branch onto main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
# Pull with rebase
git pull --rebase origin main# When conflict occurs during merge
git status # See conflicted files
# Edit files to resolve conflicts
# Look for conflict markers:
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> feature-branch
# After resolving, add files
git add resolved-file.js
# Complete the merge
git commit -m "Resolve merge conflicts"
# Or abort if needed
git merge --abortView commit history
# One line per commit
git log --oneline
# With graph
git log --graph --oneline --all
# Show commits by author
git log --author="John Doe"
# Show commits since date
git log --since="2 weeks ago"
# Show commits affecting specific file
git log -- path/to/file.js
# Pretty format
git log --pretty=format:"%h - %an, %ar : %s"Show details of commits, tags, etc.
# Show latest commit
git show
# Show specific commit
git show abc123
# Show file at specific commit
git show abc123:path/to/file.jsShow who modified each line
# Show line-by-line authorship
git blame file.js
# Show blame for specific lines
git blame -L 10,20 file.js
# Ignore whitespace changes
git blame -w file.jsReset current HEAD to specified state
# Unstage files (keep changes)
git reset HEAD file.js
# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (keep changes unstaged) - default
git reset HEAD~1
# Hard reset (discard changes) - DANGER!
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard abc123Create new commit that undoes previous commit
# Revert last commit
git revert HEAD
# Revert specific commit
git revert abc123
# Revert without creating commit immediately
git revert -n abc123Restore working tree files (Git 2.23+)
# Discard changes in working directory
git restore file.js
# Restore file from specific commit
git restore --source=HEAD~2 file.js
# Unstage file
git restore --staged file.jsCreate, list, and manage tags
# List tags
git tag
# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
# Tag specific commit
git tag v1.0.0 abc123
# Push tag to remote
git push origin v1.0.0
# Push all tags
git push origin --tags
# Delete local tag
git tag -d v1.0.0
# Delete remote tag
git push origin --delete v1.0.0# Format
type(scope): subject
body
footer
# Examples
feat(auth): add login functionality
fix(api): resolve timeout issue in user endpoint
docs(readme): update installation instructions
style(components): format code according to prettier
refactor(utils): simplify date formatting function
test(auth): add unit tests for login
chore(deps): update dependenciesTypes:
feat: New featurefix: Bug fixdocs: Documentation changesstyle: Code style changes (formatting, etc.)refactor: Code refactoringtest: Adding or updating testschore: Maintenance tasks# Add to ~/.gitconfig or use git config --global alias.name command
[alias]
# Shortcuts
st = status
co = checkout
br = branch
ci = commit
# Pretty log
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# Show last commit
last = log -1 HEAD
# Undo last commit (keep changes)
undo = reset HEAD~1 --mixed
# List contributors
contributors = shortlog --summary --numbered
# List branches by date
recent = for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'# Force push (rewrites remote history)
git push --force origin branch-name
# Safer alternative
git push --force-with-lease origin branch-name
# Hard reset (loses uncommitted changes)
git reset --hard HEAD
# Clean untracked files (permanent deletion)
git clean -fd
# Checkout file from another branch (overwrites current)
git checkout other-branch -- file.js
# Amend pushed commit (requires force push)
git commit --amend
git push --force