Links#
https://git-scm.com/docs
https://git-scm.com/book/en/v2
https://cli.github.com/manual/gh_pr_create
https://cli.github.com/manual/gh_release_create
https://docs.github.com/en/pull-requests
https://docs.github.com/en/repositories/releasing-projects-on-github
1. Important Points#
Git command mental model:
working tree:
local files
index / stage:
files prepared for next commit
local repository:
local commits
remote repository:
origin / upstream on GitHub / GitLab
safety rules:
git status before destructive commands
prefer git revert for commits already pushed
prefer --force-with-lease over --force
do not reset shared branch unless team agrees
2. Basic#
init repository#
clone repository#
git clone git@github.com:example/order-api.git
check status#
view diff#
view staged diff#
add one file#
git add src/orders/service.ts
add all changes#
commit#
git commit -m "feat: add order create api"
amend last commit#
git commit --amend -m "feat: add order create endpoint"
show commit history#
git log --oneline --decorate --graph --all
show one commit#
3. Remote#
list remotes#
add remote#
git remote add origin git@github.com:example/order-api.git
change remote URL#
git remote set-url origin git@github.com:example/new-order-api.git
fetch remote#
fetch all remotes#
pull current branch#
pull remote branch into current branch#
push current branch#
push and set upstream#
git push -u origin feature/order-api
4. Branch#
list local branches#
list all branches#
create branch#
git branch feature/order-api
switch branch#
git switch feature/order-api
create and switch branch#
git switch -c feature/order-api
create local branch from remote branch#
git switch -c feature/order-api origin/feature/order-api
pull remote branch#
git fetch origin feature/order-api:feature/order-api
rename current branch#
git branch -m feature/order-api-v2
delete local branch#
git branch -d feature/order-api
force delete local branch#
git branch -D feature/order-api
delete remote branch#
git push origin --delete feature/order-api
5. Merge / Rebase#
merge branch into current branch#
git merge feature/order-api
merge main into current branch#
abort merge#
rebase current branch on main#
continue rebase after resolving conflicts#
abort rebase#
cherry-pick commit#
6. Undo / Rollback#
unstage file#
git restore --staged src/orders/service.ts
discard file changes#
git restore src/orders/service.ts
restore file from main#
git restore --source origin/main src/orders/service.ts
revert one commit#
revert merge commit#
reset last commit but keep changes staged#
reset last commit and keep changes unstaged#
reset working tree to remote branch#
git reset --hard origin/main
danger:
reset --hard discards local working tree changes
use only after checking git status
find old commit with reflog#
restore branch to reflog point#
git reset --hard HEAD@{3}
7. Stash#
stash current changes#
git stash push -m "wip order api"
list stashes#
apply latest stash#
apply and drop latest stash#
drop one stash#
clear all stashes#
8. Tag#
create lightweight tag#
create annotated tag#
git tag -a v1.2.0 -m "release v1.2.0"
create tag on specific commit#
git tag -a v1.2.0 a1b2c3d -m "release v1.2.0"
push one tag#
delete local tag#
delete remote tag#
git push origin --delete v1.2.0
9. GitHub PR#
login GitHub CLI#
create PR#
gh pr create --base main --head feature/order-api --title "Add order API" --body "Implement order create and query endpoints."
create draft PR#
gh pr create --base main --head feature/order-api --draft --title "WIP order API" --body "Draft for early review."
list PRs#
view PR#
checkout PR locally#
merge PR#
gh pr merge 123 --squash --delete-branch
10. GitHub Release#
create release from tag#
gh release create v1.2.0 --title "v1.2.0" --notes "Release v1.2.0"
create release and generate notes#
gh release create v1.2.0 --generate-notes
upload release asset#
gh release upload v1.2.0 dist/order-api.tar.gz
list releases#
delete release#
11. Conflict#
show conflicted files#
git diff --name-only --diff-filter=U
mark resolved file#
git add src/orders/service.ts
finish merge after conflict#
finish rebase after conflict#
12. Worktree#
create worktree for another branch#
git worktree add ../order-api-hotfix hotfix/payment-timeout
list worktrees#
remove worktree#
git worktree remove ../order-api-hotfix
13. Clean#
preview untracked files to delete#
delete untracked files#
danger:
git clean -fd deletes untracked files
run git clean -nd first
14. Production Checklist#
before merge:
git status clean
tests passed
branch is up to date
PR reviewed
before rollback:
prefer git revert for pushed commits
record commit hash
know whether commit is merge commit
before release:
tag name checked
release notes generated/reviewed
artifacts uploaded if needed
rollback plan exists