This page is focused on providing and explaining the commands I most commonly use to deal with git via the terminal. I can only recommend everyone knowing at least the basic commands even if you use external tooling.
Even though external tools add an unnecessary amount of abstraction to your workplace and understanding, I can recommend them to use for visualization.
Basics
normal workflow: implement your changes → save file → review changes via diff view → stage files → commit files → publish the changes also to your remote
initialize your project
git init
preparing a commit is called staging: it means you add files to the index that will be used for the next commit
TODO: undo operation of git add → git reset (???)
# adding a single filegit add path/to/file# adding the files of a whole foldergit add path/to/folder/# adding everythinggit add --all# or: add everything from the current directorygit add .
Info
Sometimes, git doesn’t track a new file yet … This also that git commit -am "..." doesn’t add it to the index.
You have to explicitly add the folder or the file to the index.
making a commit
# normal commit with messagegit commit -m "feat: your first commit"# commit while also adding all files# -> no need for an intermediate "add" stepgit commit -am "feat: adding all files automatically"
push your changes: often you not only work alone but, want to synchronize your local repository with a remote repository (e.g., Github) to enable collaboration
# publish your changes to the default origingit push# don't do this unless you want to make your colleges hate yougit push --force
Helpers
output the current state of your git repository
this command gives you information about which files you have not versioned yet, changed or staged
git status
git history
git log
visualizing the current changes
# for unstaged filesgit diff# for staged filesgit diff --cached
Merging
merge one branch into your current one
# merge branch "abc" into your current onegit merge abc
Stashing
Branching
# creates a new branch called "abc"git checkout -b abc# switch to the branch "xyz"git checkout xyz
Working with remote
TODO: git clone
TODO: git upstream einrichten
pull changes: synchronize your local repository with incoming changes from the remote repository
# just update the information you have about the remote repository # without actually updating your files (nothing happens)git fetch
be careful: merge conflicts can already happen here
no local changes and you want to pull the newest changes
# update to your local files with changes from remotegit pull
you previously had the newest changes, but made some local progress after that; that could be file changes and even some commits;
important: you didn’t change the files that had been updated by the remote repository (otherwise you will get merge conflicts)
# goated commandgit pull --rebase --autostash
you changed files that were also changed by the remote repository → merge conflicts!
# no worriesgit pull —-rebase
After that, you can resolve the merge via your text editor and select the changes you want to keep. Then you can commit the merge.