git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git init
git clone git@host:org/repo.git
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git status
git status -s
git add file.txt
git add .
git add -p
git commit -m "message"
git commit -am "message"
git diff
git diff --staged
git show HEAD
git branch feature-x
git switch feature-x
git switch -c feature-x
git branch -a
git branch -d feature-x
git branch -D feature-x
git switch main
git merge feature-x
git merge --no-ff feature-x
git switch feature-x
git rebase main
git rebase --continue
git cherry-pick a1b2c3d
git status
# edit conflicted files
git add resolved-file.txt
git commit
git remote -v
git remote add origin git@host:org/repo.git
git fetch origin
git pull origin main
git pull --rebase origin main
git push origin feature-x
git push -u origin feature-x
git push --force-with-lease
git tag v1.0.0
git tag -a v1.0.0 -m "release"
git push origin v1.0.0
git branch --set-upstream-to=origin/main
git branch -vv
git fetch --prune
git remote prune origin
git restore --staged file.txt
git restore file.txt
git commit --amend -m "new message"
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
git revert a1b2c3d
git stash
git stash pop
git stash list
git reflog
git checkout a1b2c3d
git log --oneline --graph --all
git log -p file.txt
git blame file.txt
git clean -n
git clean -fd
| Symptom | Fix |
|---|---|
| Detached HEAD | git switch -c new-branch |
| Wrong branch commit | git cherry-pick, then git reset --hard on wrong branch |
| Committed to main by mistake | git branch feature-x; git reset --hard origin/main |
| Merge conflict markers left in file | resolve manually, git add, git commit |
| Push rejected (non-fast-forward) | git pull --rebase, then push |
| Accidentally deleted branch | git reflog → git checkout <sha> → git switch -c |
| Command | Risk |
|---|---|
| git reset --hard | destructive loses uncommitted work |
| git push --force | destructive can overwrite others' commits |
| git push --force-with-lease | caution safer, checks remote hasn't moved |
| git clean -fd | destructive deletes untracked files |
| git rebase (shared branch) | destructive rewrites history others rely on |
| git revert | safe adds a new commit, no history rewrite |
node_modules/
*.log
.env
git help commit
git commit -h
git config --global alias.st status
git config --global alias.co checkout