Stage files (mark files to be included in the
next commit):
git add file1 file2 ... # if not yet tracked, tracking starts here
Commit changes:
git commit -m"Description of the changes"
Continue work
Back to step 1
To view what is staged / changed, use
git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: paper.tex
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: img/figure.svg
Untracked files:
(use "git add ..." to include in what will be committed)
img/new-figure.svg
To stage all tracked, changed files, use
git add -u# safer than git add .
Tipp: you also can stage only parts of changed files:
git add -p file1 file2 ...# -e for to even edit the diff
To untrack but not remove the file, use
git rm --cached<file>
Viewing changes
git status
git log
git log <commit-ref># e.g. <commit-ref>=11089693 (commit-ref is longer but# max. first 8 chars suffice per repo)
(omit command line options to see, if you actually like the
effect)
This enables:
git lg
git diff # Changes relative to the last commitgit diff <commit-ref># Changes relative to <commit-ref>git diff <branch-name># Changes relative to <branch-name>
git diff --staged# or --cached
git blame filename# Add -L 40,60 to only see changes between line 40 and 60# Add -L '/^double f(double x)/' to only see changes on lines# matching the regexp# Add --since=3.weeks to only show changes 3 weeks back
project├── README.md # <- versioned├── src│ └── main.cpp # <- versioned├── build # <- binaries => should go to .gitignore│ └── ...└── private # Uncommon / not used by most other devs ├── my_private_garbage # 🗑 └── ...
Use the “private” / untracked version of .gitignore:
.git/info/exclude
Git for collaboration
When:
work on individual features
try out alternative solutions
trace and fix bugs
… Then: git branches
Create a new branch out of the current git state:
git switch -c branch-name
Once you continued work and want to go back to the state of a
branch:
Clean up the history on “self-owned” branches before submitting for
review
Stick to git commit message conventions
Clean History
git commit --amend--no-edit# Without `--no-edit`, you also# can alter the commit message.
git commit --fixup=<commit-ref>
git rebase -i<ref> --autosquash# <ref>: branch / commit ref before commit to be rewritten
git rebase -i<ref># <ref>: branch / commit ref before commits to be rewritten
Rewriting the history needs a “force” push:
git push --force
others (e.g. reviewer) can update using a “force pull”:
git switch source-branch # if not already on the branchgit fetchgit reset --hard origin/source-branch
When you rewrite a commit, a force push is crucial. A normal push
will be rejected and you will be instructed to do a pull first (see
message below). This is not what we want to accomplish in this
situation. Ignore it and do the force push.
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to '...'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.