Essential Git Workflow and Command Reference
Core Data Flow
Working Directory → Staging Area → Local Repository → Remote Repository
Daily Commands
Push with Safety Net
git pull --rebase # fetch + rebase to avoid merge bubbles
git push # send current branch upstream
git push -f origin feature-x # only after history rewrite
git push --all origin # publish every local branch
Rewriting History
# Rebase feature onto latest main
# Before:
# main: A---B---C
# \
# feature: D---E---F
git checkout feature
git rebase main
# After:
# main: A---B---C
# \
# feature: D'--E'--F'
Merging Branches
git checkout main
git merge hotfix
# Result:
# main: A---B---M
# \ /
# hotfix: C---D
Branch Manipulation
git branch -f master HEAD~3 # move master three commits back
Undoing Changes
git reset --hard HEAD~1 # discard last commit (dangerous)
git revert HEAD~1 # create new commit that undoes previous one
Clone with Submodules
git clone --recurse-submodules https://repo.url/project.git
# or
git clone https://repo.url/project.git
git submodule update --init --recursive
Publishing a Local Project
Remote Already Has Content
git init
git remote add origin https://repo.url/project.git
git fetch origin master
git branch --set-upstream-to=origin/master master
git add .
git commit -m "Initial import"
git push
Empty Remote
git init
git remote add origin https://repo.url/project.git
git add .
git commit -m "First commit"
git push -u origin master
Global Configuration
~/.gitconfig
[user]
name = Your Name
email = you@example.com
[color]
ui = auto
[credential]
helper = store
[commit]
template = ~/.gitmessage
[core]
editor = code --wait
Common Pit Snags
- Empty directories: Git tracks files only; add a
.gitkeepplaceholder. - Ignoring files: Create
.gitignoreand list patterns:node_modules/ *.log .env