You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?
Cheat sheet
The commands you learned, in one place.
Terminal basics
Set up
git --versionIs Git here? Which version?git config --global user.name "Aiman"Tell Git your name, for all projects.git config --global user.email "aiman@example.com"Set your email.git config --global init.defaultBranch mainNew repos start on main.git config --listShow all your settings.git add -hShort help, right in the terminal.git help commitThe full manual page.
Save your work
git initGit starts watching this folder.git statusGreen = in the box. Red = not yet.git status -sA short view.git add index.htmlPut one file in the box.git add .Put all changes here in the box.git add -pPick pieces of a change, one by one.git commit -m "Add day 1 page"Save with a clear message.git commit -am "Fix typo"Add tracked changes and commit in one go.git commit --amend -m "Better message"Replace the last commit. Not after you push!git rm old.htmlDelete it and stage the delete.Risk: Carefulgit rm --cached secret.txtStop tracking it, but keep the file.Risk: Carefulgit mv home.html index.htmlRename it. Git knows it moved.
Look at history
git log --onelineOne line per commit.git log --oneline --graph --allDraw the whole history.git show HEAD~1Show the commit before the last one.git diffChanges not in the box yet.git diff --stagedChanges in the box.git diff HEAD~2 HEADBetween two commits.git branch* main means you are on main.
Undo things
git restore index.htmlThrow away changes. They are gone!Risk: Can lose workgit restore --staged index.htmlTake it out of the box. Safe: the change stays.Risk: Can lose workgit reset --soft HEAD~1Undo the last commit. Changes stay in the box.Risk: Carefulgit reset HEAD~1Changes go back to your folder.Risk: Carefulgit reset --hard HEAD~1Changes are wiped. Danger!Risk: Carefulgit revert HEADUndo the last commit, safely.git switch mainCome back to main.git switch --detach HEAD~2Visit an old commit (detached HEAD).git switch -Go back to where you were.git checkout a1b2c3dVisit commit a1b2c3d (detached HEAD).Risk: Carefulgit clean -nPreview only. Nothing is deleted.Risk: Can lose workgit clean -fDelete them. Git can't bring them back.Risk: Can lose workgit clean -fdUntracked folders too.Risk: Can lose workgit reflogFind the lost commit.git reset --hard HEAD@{1}Jump back to where HEAD was one move ago.git filter-repo --sensitive-data-removal --invert-paths --path .envWipe .env from all history. Every later commit id changes.Risk: Can lose workgit filter-repo --sensitive-data-removal --replace-text ../secrets.txtReplace secret text listed in a file, in every commit.Risk: Can lose work
Branches and merging
git branch feature/login-formMake a new branch here. You stay where you are.Risk: Carefulgit branch -d feature/login-formDelete a branch that is already merged. Safe.Risk: Carefulgit branch -D old-ideaDelete it even if it is not merged. Careful!Risk: Carefulgit branch -m new-nameRename the branch you are on.Risk: Carefulgit branch --mergedWhich branches are already merged here?Risk: Carefulgit switch -c hotfix/pay-buttonNew branch, and you are on it now.git checkout -b hotfix/pay-buttonThe older way to do the same thing.git switch -Jump back to the branch you were on.git merge feature/login-formBring the login form into this branch.git merge --no-ff feature/login-formAlways make a merge commit, even if Git could fast-forward.git merge --abortStuck in a conflict? Go back to before the merge.git stashPut my changes away for now.Risk: Carefulgit stash popTake the newest stash back out.Risk: Carefulgit stash listSee what is in the drawer.Risk: Carefulgit stash -uAlso put away new, untracked files.Risk: Carefulgit stash dropThrow one stash away. It is gone!Risk: Carefulgit rebase mainPut my branch's commits on top of the latest main.Risk: Carefulgit rebase --continueGo on after you fixed a conflict and added it.Risk: Carefulgit rebase --abortCancel and go back to how it was.Risk: Carefulgit rebase -i mainTidy your commits: squash, reword, drop, reorder.Risk: Carefulgit cherry-pick a1b2c3dCopy that one commit here.git cherry-pick --abortStop and go back to before.
Work with GitHub
git remote add origin https://github.com/you/diary.gitCall this GitHub address origin.git remote -vShow each remote and its address.git remote rename origin upstreamGive a remote a new name.git remote set-url origin <new-url>The repo moved? Update the address.git push -u origin mainFirst push: send main and remember where it goes.Risk: Carefulgit pushAfter the first time, this is enough.Risk: Carefulgit push --tagsSend your tags too.Risk: Carefulgit push origin --delete old-branchDelete a branch on the remote.Risk: Carefulgit push --force-with-leaseForce, but stop if someone else pushed since your last fetch.Risk: Can lose workgit push --forceForce with no safety check. Avoid it.Risk: Can lose workgit clone https://github.com/you/diary.gitMake a diary folder with the whole project.git fetchUpdate origin/main, then look with git status.git fetch --pruneAlso forget remote branches that were deleted.git pullGet the latest work from GitHub.Risk: Carefulgit pull --rebasePut your new commits on top of theirs.Risk: Carefulgit pull --no-rebaseJoin with a merge commit.Risk: Carefulgit config pull.rebase falseChoose merge as your usual way to pull.Risk: Carefulgit tag -a v1.0 -m "First release"Tag this commit as version 1.0, with a note.git tagList your tags.git push origin v1.0Send one tag to GitHub.git tag -d v1.0Delete a tag on your computer.
GitHub tools
gh repo clone aina-codes/diaryCopy the diary repo into a new diary folder.gh repo fork aina-codes/diary --cloneFork the repo, then copy your fork to your computer.gh issue create --title "Typo on home page" --body "It says Dairy."Report a typo. gh prints the new issue's link.gh issue create --webFinish writing the issue in your browser.gh issue list --label bugShow only open issues labelled bug.gh pr create --fillUse your commit message as the title and body.gh pr create --title "Fix typo" --body "Closes #5"Write your own title and body.gh pr checkout 6Switch to the branch of pull request 6.gh codespace create --repo aina-codes/diaryMake a Codespace for the diary repo. Uses your monthly allowance.gh codespace code --webOpen a Codespace in your browser.gh codespace stopStop a Codespace when you are done.
Deep Git
git cat-file -p HEADPrint the newest commit: its tree, parent, author and message.git cat-file -t HEAD^{tree}Print the type of an object. Here it says tree.git cat-file -s HEADPrint the size of an object in bytes.git hash-object index.htmlPrint the blob id of index.html. Nothing is saved.git hash-object -w notes.txtAlso save the content in Git as a blob.git ls-tree HEADList the top folder of the newest commit.git ls-tree -r --name-only HEAD~1List every file of an older commit.git rev-parse HEAD~2Print the full id of the commit two steps back.git rev-parse --short HEADPrint the short id instead.git rev-parse --abbrev-ref HEADPrint the name of your branch.git rev-parse --show-toplevelPrint the top folder of this repo.git log -S "TODO" --onelineShow commits that added or removed the word TODO.git log -G "phone|Call" -pShow commits whose changed lines match, with their changes.git log --author=Aisyah --onelineShow only the commits Aisyah made.git log --grep=footer -i --onelineShow commits whose message says footer, in any case.git grep -n footerShow every line with footer, with line numbers.git grep TODO HEAD~2Search the files as they were two commits ago.git blame index.htmlEach line gets a commit id, a name and a date.git blame -L 2,3 index.htmlLook at lines 2 to 3 only.git shortlog -snCount commits per person, the most first.git bisect start HEAD HEAD~9Bad is HEAD, good is nine commits back. Git checks out the middle.Risk: Carefulgit bisect goodThe commit you are testing works.Risk: Carefulgit bisect badThe commit you are testing is broken.Risk: Carefulgit bisect run sh test.shLet a test script judge every step for you.Risk: Carefulgit bisect resetFinish and go back to your branch.Risk: Carefulgit worktree add ../hotfix -b hotfixMake folder ../hotfix on a new branch called hotfix.Risk: Carefulgit worktree listShow every working folder and its branch.Risk: Carefulgit worktree remove ../hotfixDelete that folder. The branch and its commits stay.Risk: Carefulgit config --global alias.st statusNow git st does the same as git status.git config --global alias.lg "log --oneline --graph --all"git lg draws your whole history.git config --global --get-regexp aliasList all your aliases.git show-refEach line: an id, then a ref like refs/heads/main.git show-ref --headsOnly branches.git pack-refs --allPack branches too, not only tags.git gcPack and tidy now. Objects in the reflog stay safe.Risk: Carefulgit gc --prune=nowDrop unreachable objects at once, with no grace time.Risk: Carefulgit maintenance startRun the jobs on a schedule for this repo.git maintenance stopStop the scheduled jobs.git submodule add https://github.com/you/kuih-menu.git menuLink the kuih-menu repo into a folder called menu.Risk: Carefulgit submodule update --init --recursiveFill in empty submodule folders after a plain clone.Risk: Carefulgit clone --recurse-submodules https://github.com/you/shop.gitClone a repo and its submodules in one go.Risk: Carefulgit sparse-checkout set docsShow only the docs folder (and files at the top).git sparse-checkout disableShow every folder again.git config --global rerere.enabled trueTurn it on once. Git records fixes by itself after that.git rerere forget index.htmlDrop a recorded fix you no longer want.git range-diff main feature@{1} featureCompare the branch before and after your rebase.