Git is a distributed version control system for tracking changes in source code during software development. It enables collaboration, branching, merging, and history tracking.
| Feature | Git | GitHub/GitLab |
|---|---|---|
| Type | Version Control | Hosting Platform |
| Scope | Local & Remote | Remote (Cloud) |
| Features | Branch, Merge, Track | PRs, Issues, CI/CD |
# Windows: Download installer from website
# macOS (Homebrew):
brew install git
# Ubuntu/Debian:
sudo apt update && sudo apt install git
git config)git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --list
git init).git folder to track version historygit init
git clone)git clone https://github.com/user/repo.git
git status)git status
git add)git add file.txt
# Add all changes
git add .
git commit)git commit -m "Describe your changes"
git log)git log
# One line per commit
git log --oneline
git diff, git diff --staged)--staged to see staged changesgit diff
# Show staged changes
git diff --staged
git branch
git branch branchnamegit checkout branchname or git switch branchnamegit branch feature-x
git checkout feature-x
# Or (Git 2.23+)
git switch feature-x
git merge)git checkout main
git merge feature-x
# After conflict, edit files, then:
git add conflicted-file.txt
git commit
git checkout feature-x
git rebase main
git branch -d)-d (safe delete), -D (force delete)git branch -d feature-x
# Force delete
git branch -D feature-x
git stash)git stash
# List stashes
git stash list
# Apply latest stash
git stash apply
git restore, git checkout)git restore (modern), git checkout (legacy)git restore file.txt
# Or (older)
git checkout -- file.txt
git reset, git revert)git reset: Move HEAD and optionally modify index/working directorygit revert: Create a new commit that undoes changes# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (safe for shared repos)
git revert <commit-hash>
git remote add, git remote -v)git remote add origin https://github.com/user/repo.git
git remote -v
git push)git push origin main
git pull)git pull origin main
git fetch)git fetch origin
git tag)git tag v1.0.0
git tag
# Push tags to remote
git push origin v1.0.0
.gitignore)# Example .gitignore
node_modules/
.env
*.log
/dist
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"
--oneline, --graph, --decorategit log --oneline --graph --decorate --all
.git/hooks/ directorymain, develop, feature, release, and hotfix branches for structured releases.
(Above: Feature Branch & Git Flow simplified. Forking workflow: fork → clone → branch → PR.)