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-github1. 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 / GitLabsafety 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 agrees2. Basic#
init repository#
git initclone repository#
git clone git@github.com:example/order-api.gitcheck status#
git statusview diff#
git diffview staged diff#
git diff --cachedadd one file#
git add src/orders/service.tsadd all changes#
git add .add all repository changes from any directory#
git add -A :/use this when:
current shell is inside a subdirectory
you want to stage changes across the whole repository
note:
:/ means repository root pathspec
git add . only stages changes under the current directorycommit#
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 --allshow one commit#
git show a1b2c3d3. Remote#
list remotes#
git remote -vadd remote#
git remote add origin git@github.com:example/order-api.gitchange remote URL#
git remote set-url origin git@github.com:example/new-order-api.gitfetch remote#
git fetch originfetch all remotes#
git fetch --all --prunepull current branch#
git pullpull remote branch into current branch#
git pull origin mainpush current branch#
git pushpush and set upstream#
git push -u origin feature/order-api4. Branch#
list local branches#
git branchlist all branches#
git branch -acreate branch#
git branch feature/order-apiswitch branch#
git switch feature/order-apicreate and switch branch#
git switch -c feature/order-apicreate local branch from remote branch#
git switch -c feature/order-api origin/feature/order-apipull remote branch#
git fetch origin feature/order-api:feature/order-apirename current branch#
git branch -m feature/order-api-v2delete local branch#
git branch -d feature/order-apiforce delete local branch#
git branch -D feature/order-apidelete remote branch#
git push origin --delete feature/order-api5. Merge / Rebase#
merge branch into current branch#
git merge feature/order-apimerge main into current branch#
git merge origin/mainabort merge#
git merge --abortrebase current branch on main#
git rebase origin/maincontinue rebase after resolving conflicts#
git rebase --continueabort rebase#
git rebase --abortcherry-pick commit#
git cherry-pick a1b2c3d6. Undo / Rollback#
unstage file#
git restore --staged src/orders/service.tsdiscard file changes#
git restore src/orders/service.tsrestore file from main#
git restore --source origin/main src/orders/service.tsrevert one commit#
git revert a1b2c3drevert merge commit#
git revert -m 1 a1b2c3dcancel a revert commit#
If git revert already created a new commit, the safest way to cancel it is usually to revert the revert.
git log --oneline
git revert <revert_commit_hash>Example:
git revert abc1234This creates another commit that restores the content removed by the previous revert. Use this when the revert commit has already been pushed or shared.
If the revert commit is only local and not pushed yet, you can remove the last commit instead.
Keep the revert changes staged:
git reset --soft HEAD~1Discard the revert commit and its working tree changes:
git reset --hard HEAD~1decision:
already pushed:
git revert <revert_commit_hash>
not pushed, want to keep changes staged:
git reset --soft HEAD~1
not pushed, want to delete the revert completely:
git reset --hard HEAD~1
danger:
reset --hard discards local working tree changes
check git status before using itreset last commit but keep changes staged#
git reset --soft HEAD~1reset last commit and keep changes unstaged#
git reset HEAD~1reset working tree to remote branch#
git reset --hard origin/maindanger:
reset --hard discards local working tree changes
use only after checking git statusfind old commit with reflog#
git reflogrestore branch to reflog point#
git reset --hard HEAD@{3}7. Stash#
stash current changes#
git stash push -m "wip order api"list stashes#
git stash listapply latest stash#
git stash applyapply and drop latest stash#
git stash popdrop one stash#
git stash drop stash@{0}clear all stashes#
git stash clear8. Tag#
list tags#
git tagcreate lightweight tag#
git tag v1.2.0create 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#
git push origin v1.2.0push all tags#
git push origin --tagsdelete local tag#
git tag -d v1.2.0delete remote tag#
git push origin --delete v1.2.09. GitHub PR#
login GitHub CLI#
gh auth logincreate 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#
gh pr listview PR#
gh pr view 123 --webcheckout PR locally#
gh pr checkout 123merge PR#
gh pr merge 123 --squash --delete-branch10. 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-notesupload release asset#
gh release upload v1.2.0 dist/order-api.tar.gzlist releases#
gh release listdelete release#
gh release delete v1.2.011. Conflict#
show conflicted files#
git diff --name-only --diff-filter=Umark resolved file#
git add src/orders/service.tsfinish merge after conflict#
git commitfinish rebase after conflict#
git rebase --continue12. Worktree#
create worktree for another branch#
git worktree add ../order-api-hotfix hotfix/payment-timeoutlist worktrees#
git worktree listremove worktree#
git worktree remove ../order-api-hotfix13. Ignore / Cached Files#
staged / index meaning#
staged / index:
files already added to the next commit
git commit reads from the index, not directly from the working tree
.gitignore:
prevents new untracked files from being added accidentally
does not automatically remove files already tracked or stagedremove ignored build output from index#
Use this when files are already staged/tracked, but should stay on disk and be ignored by Git.
git rm --cached -r envs/uat/ap-northeast-1/sns/buildsThen verify:
git statusresult:
files are removed from Git index
local files remain on disk
future git add . will not add them if .gitignore matches
common case:
.gitignore contains **/builds/**
zip/json build artifacts were staged before the ignore rule
git rm --cached removes them from the next commitDo not use plain rm unless you also want to delete local files.
14. Clean#
preview untracked files to delete#
git clean -nddelete untracked files#
git clean -fddanger:
git clean -fd deletes untracked files
run git clean -nd first15. 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