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 init | Start a repository in this folder. |
git clone https://codeberg.org/user/project.git | Copy an existing repository to your computer. |
Every day
git status | What 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 --staged | Changes not yet added / changes that will go into the commit. |
git log --oneline --graph --all | Short history of all branches as a graph. |
Branches
git branch | List branches; * is the current one. |
git switch -c new-feature | Make a new branch and switch to it. |
git switch main | Go back to main. |
git merge new-feature | Bring the changes from new-feature into the current branch. |
git branch -d new-feature | Delete a branch that is already merged. |
Remote repository
git remote -v | Where is the remote repository. |
git push -u origin main | Send main for the first time and remember where to. |
git push git pull | Send your commits / get and merge the new ones. |
git fetch | Only download what is new, without changing your files. |
Undoing things
git restore file.py | Throw away changes in a file since the last commit. |
git restore --staged file.py | Take a file out of the next commit, but keep the changes. |
git commit --amend | Fix the last commit message or add a forgotten file (only before push). |
git revert a1b2c3d | Make a new commit that undoes an old one – the safe way after push. |
git stash git stash pop | Put unfinished changes aside / bring them back. |
Good to know
.gitignore | A file with names and patterns Git should ignore – passwords, build files, node_modules/. |
git tag v1.0 git push --tags | Mark a version. |
git blame file.py | Who changed which line, and when. |