Cheat Sheets

JPNAS.NET / Cheat Sheets

🔀 Git

The Git commands for everyday work – from the first commit to branches, undoing mistakes and working with a remote repository on Forgejo, Codeberg or GitHub.

Open in the interactive Cheat Sheets

First steps

git config --global user.name "Jernej Pleteršek"Your name for all commits (once per computer).
git config --global user.email "me@example.com"Your e-mail for all commits.
git initStart a repository in this folder.
git clone https://codeberg.org/user/project.gitCopy an existing repository to your computer.

Every day

git statusWhat changed, and what is ready to commit.
git add file.py git add .Mark one file / all changes for the next commit.
git commit -m "Fix the date format"Save the marked changes with a short description.
git diff git diff --stagedChanges not yet added / changes that will go into the commit.
git log --oneline --graph --allShort history of all branches as a graph.

Branches

git branchList branches; * is the current one.
git switch -c new-featureMake a new branch and switch to it.
git switch mainGo back to main.
git merge new-featureBring the changes from new-feature into the current branch.
git branch -d new-featureDelete a branch that is already merged.

Remote repository

git remote -vWhere is the remote repository.
git push -u origin mainSend main for the first time and remember where to.
git push git pullSend your commits / get and merge the new ones.
git fetchOnly download what is new, without changing your files.

Undoing things

git restore file.pyThrow away changes in a file since the last commit.
git restore --staged file.pyTake a file out of the next commit, but keep the changes.
git commit --amendFix the last commit message or add a forgotten file (only before push).
git revert a1b2c3dMake a new commit that undoes an old one – the safe way after push.
git stash git stash popPut unfinished changes aside / bring them back.

Good to know

.gitignoreA file with names and patterns Git should ignore – passwords, build files, node_modules/.
git tag v1.0 git push --tagsMark a version.
git blame file.pyWho changed which line, and when.