Git
Website: https://git-scm.com CLI Tool: git Authentication: git config user.name/user.email
Description
Git is a distributed version control system for tracking changes in source code during software development. Enables collaboration, branching, merging, and version history. Essential for modern software development workflows.
Commands
Repository Setup
Initialize Repository
git init
git init --bare
Create new Git repository in current directory. Use --bare for bare repository (no working directory).
Clone Repository
git clone <url>
git clone <url> <directory>
git clone --depth 1 <url>
git clone --branch <branch> <url>
Clone remote repository. Use --depth 1 for shallow clone, --branch for specific branch.
Remote Management
git remote add <name> <url>
git remote -v
git remote remove <name>
git remote rename <old> <new>
git remote set-url <name> <url>
Manage remote repositories.
Basic Operations
Status
git status
git status -s
git status --short
Show working tree status. Use -s for short format.
Add Files
git add <file>
git add .
git add -A
git add -p
git add *.js
Stage changes for commit. Use . for current directory, -A for all changes, -p for interactive.
Commit Changes
git commit -m "message"
git commit -am "message"
git commit --amend
git commit --amend --no-edit
Record changes to repository. Use -a to stage all tracked files, --amend to modify last commit.
Push Changes
git push
git push origin <branch>
git push -u origin <branch>
git push --force
git push --force-with-lease
git push --tags
Upload commits to remote. Use -u to set upstream, --force-with-lease for safer force push.
Pull Changes
git pull
git pull origin <branch>
git pull --rebase
git pull --ff-only
Fetch and merge remote changes. Use --rebase to rebase instead of merge.
Fetch Changes
git fetch
git fetch origin
git fetch --all
git fetch --prune
Download objects and refs from remote. Use --prune to remove deleted remote branches.
Branching
Create Branch
git branch <name>
git branch <name> <start-point>
git checkout -b <name>
git switch -c <name>
Create new branch. Use checkout -b or switch -c to create and switch.
List Branches
git branch
git branch -a
git branch -r
git branch -v
List branches. Use -a for all, -r for remote, -v for verbose.
Switch Branch
git checkout <branch>
git switch <branch>
Switch to branch.
Delete Branch
git branch -d <branch>
git branch -D <branch>
git push origin --delete <branch>
Delete branch. Use -D for force delete, push with --delete to remove remote branch.
Rename Branch
git branch -m <old> <new>
git branch -m <new>
Rename branch. Omit old name to rename current branch.
Merging
Merge Branch
git merge <branch>
git merge <branch> --no-ff
git merge <branch> --squash
git merge --abort
Merge branch into current branch. Use --no-ff to create merge commit, --squash to squash commits.
Rebase
git rebase <branch>
git rebase -i <commit>
git rebase --continue
git rebase --abort
git rebase --skip
Reapply commits on top of another branch. Use -i for interactive rebase.
History
View Log
git log
git log --oneline
git log --graph
git log --all --graph --decorate --oneline
git log -n 10
git log --since="2 weeks ago"
git log --author="name"
git log --grep="pattern"
git log <file>
View commit history with various filters and formats.
Show Commit
git show <commit>
git show <commit>:<file>
git show HEAD
git show HEAD~1
Show commit details and changes.
Diff Changes
git diff
git diff <file>
git diff --staged
git diff <commit1> <commit2>
git diff <branch1>..<branch2>
git diff HEAD~1 HEAD
Show differences between commits, branches, or working tree.
Blame
git blame <file>
git blame -L 10,20 <file>
Show what revision and author last modified each line.
Undoing Changes
Discard Changes
git checkout -- <file>
git restore <file>
git checkout .
git restore .
Discard changes in working directory.
Unstage Files
git reset HEAD <file>
git restore --staged <file>
git reset HEAD
Unstage files.
Reset Commits
git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>
git reset HEAD~1
Reset to previous commit. --soft keeps changes staged, --mixed unstages, --hard discards.
Revert Commit
git revert <commit>
git revert HEAD
git revert <commit> --no-commit
Create new commit that undoes changes from specified commit.
Stashing
Stash Changes
git stash
git stash save "message"
git stash push -m "message"
git stash -u
Save changes temporarily. Use -u to include untracked files.
List Stashes
git stash list
Show all stashes.
Apply Stash
git stash apply
git stash apply stash@{0}
git stash pop
git stash pop stash@{0}
Restore stashed changes. pop also removes from stash list.
Drop Stash
git stash drop stash@{0}
git stash clear
Remove stash. Use clear to remove all stashes.
Tags
Create Tag
git tag <name>
git tag <name> <commit>
git tag -a <name> -m "message"
git tag -s <name> -m "message"
Create tag. Use -a for annotated tag, -s for signed tag.
List Tags
git tag
git tag -l "v1.*"
List tags with optional pattern.
Push Tags
git push origin <tag>
git push --tags
Push tags to remote.
Delete Tag
git tag -d <tag>
git push origin --delete <tag>
Delete local and remote tags.
Configuration
User Config
git config --global user.name "Name"
git config --global user.email "email@example.com"
git config user.name
git config --list
Configure user identity and view settings.
Editor Config
git config --global core.editor "vim"
git config --global core.editor "code --wait"
Set default text editor.
Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Create command shortcuts.
Advanced
Cherry-Pick
git cherry-pick <commit>
git cherry-pick <commit1> <commit2>
git cherry-pick --continue
git cherry-pick --abort
Apply specific commits to current branch.
Bisect
git bisect start
git bisect bad
git bisect good <commit>
git bisect reset
Binary search to find bug-introducing commit.
Reflog
git reflog
git reflog show <branch>
Show reference logs (history of HEAD and branch tips).
Clean
git clean -n
git clean -f
git clean -fd
git clean -fX
Remove untracked files. Use -n for dry run, -d for directories, -X for ignored files.
Submodules
git submodule add <url> <path>
git submodule init
git submodule update
git submodule update --remote
Manage repository within repository.
Examples
Basic Workflow
# Initialize and commit
git init
git add .
git commit -m "Initial commit"
# Connect to remote
git remote add origin https://github.com/user/repo.git
git push -u origin main
Feature Branch Workflow
# Create feature branch
git checkout -b feature/new-feature
# Make changes
git add .
git commit -m "feat: add new feature"
# Push to remote
git push -u origin feature/new-feature
# Update from main
git checkout main
git pull
git checkout feature/new-feature
git rebase main
Fix Mistakes
# Amend last commit
git commit --amend -m "corrected message"
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Discard last commit
git reset --hard HEAD~1
# Undo pushed commit
git revert HEAD
git push
Sync Fork
# Add upstream remote
git remote add upstream https://github.com/original/repo.git
# Fetch and merge
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
Interactive Rebase
# Clean up last 3 commits
git rebase -i HEAD~3
# In editor: pick, squash, reword, etc.
# Save and close
# Force push if already pushed
git push --force-with-lease
Notes
- Working Directory: Your local file system where you edit files
- Staging Area (Index): Where changes are prepared for commit
- Repository: Where commits are stored
- HEAD: Pointer to current branch tip
- Origin: Default name for remote repository
- Main/Master: Default branch name
- Fast-Forward: Merge without creating merge commit
- Merge Commit: Commit with multiple parents
- Rebase: Rewrite history by replaying commits
- Detached HEAD: Checked out commit instead of branch
- .gitignore: Specify files to ignore
- .gitattributes: Define attributes for paths
- Commit Messages: Use imperative mood ("add feature" not "added feature")
- Force Push: Dangerous! Use
--force-with-leaseinstead - Shallow Clone: Use
--depth 1to save space and time - Signed Commits: Use GPG keys for commit verification
- SSH vs HTTPS: SSH recommended for frequent access
- LFS (Large File Storage): Handle large files separately
- Hooks: Scripts that run on git events (pre-commit, post-commit, etc.)
Comments (0)
Add a Comment
No comments yet. Be the first to comment!