Skip to content

Instantly share code, notes, and snippets.

@lquenti
Last active September 23, 2025 11:23
Show Gist options
  • Select an option

  • Save lquenti/8a5b63323ecf38e85702ede54d3a05d9 to your computer and use it in GitHub Desktop.

Select an option

Save lquenti/8a5b63323ecf38e85702ede54d3a05d9 to your computer and use it in GitHub Desktop.
Very Practical Introduction to Git

Very Practical Introduction to Git

  • Hands on, thus no slides
  • This should bring you through your first 1-2 years definitely

Structure

  • The absolute minimum of theory
  • Get started FAST (alone):
    1. New Project
    2. Existing Project
  • Work with others
    1. You are part of the project
    2. Public project
  • Fancy Pants Features
  • Where to find more

The absolute minimum of theory

  • What is it

    • Distributed Version Control System
    • Tracks named changes to your code over time
    • Used to collaboratively work together on code
  • Why is it needed?

    • Keep history of all changes (with explainations)
    • Allows collaboration using "branches"
    • Allows for easy reverts and comparisons
  • How does it work

    • A directed tree of named updates
    • Each git can be thought of a bunch of diffs with a name
    • The named updates form a usually linear history
    • The history can break up (branch), and then be put together again (merge)
  • Needed Lingo:

    • Repository: Central place for a project.
      • Internally, a magic .git folder within the projects root
    • Commit: A named update, representing a snapshot at a specific point in time
      • Each commit has one or more parents, which point to the previous commits
      • This snapshot can be viewed again, called a checkout
    • Branch: Lightweight, movable pointer showing the newest commit of a given graph branch
      • Represent one independent line of development

Image of branches

  • Git != GitHub != GitLab
    • Git: The Open Source Software itself
    • GitHub/GitLab/Codeberg/...: Web-based platforms to host git repositories

Get started FAST: New Project

1. Prepare on cluster

  1. Load git
git --version
module av
module load git
git --version
  1. Create SSH key if you don't have already
ssh-keygen -t ed25519 -C "[email protected]"
cat .ssh/id_ed25519.pub
  1. Upload SSH Key to GWDG GitLab

2. Initialize Repository

3. First Commit

git init # Only creates .git

git config --global user.name "John Doe"
git config --global user.email "[email protected]"
git config --global core.editor "vim"
# TODO: Show where its stored (global/local)


git remote add origin ...  # Origin is only convention => Decentralized
# edit stuff
git status # untracked/staged/committed
git add
git commit
git status
git log

git push --set-upstream origin master
# show in web

4. Second Commit

git status
# edit stuff

git status
git diff
git add

git diff
git diff --staged

git commit
git status

git diff
git diff --staged
git log

git push # no --set-upstream origin master
git remote -v
cat .git/config

5. Watching what you have done

git log
git log --oneline
git --no-pager log --oneline

6. Fixing whoopsies (when working alone)

# edit stuff
git config --local user.name "Aztec Pwn3r"
git config --local user.email "[email protected]"
git commit -am "I catn spel"
git push

OH NO!!

git config --unset --local user.email
git config --list

git commit --amend --no-edit
git log -1

Half way there: Commit still wrong!

git commit --amend

Update remote:

git push
# The following is a really really bad idea...

git push --force
# Settings -> Repository -> Protected Branches

Get started FAST: Existing Project

cd ..
rm -rf 

module list
git clone # Use SSH, not HTTP

git status
git log --oneline

# ...another commit

Work with others: You are part of the project

1. Get started

git clone
cd
git status
git log --oneline -10
# ..add another commit

2. Branches and Merge Conflicts

Image of branches Guitar Hero

2.1 Update two branches

git status # We should be on master

echo "\nwritten on master" >> README.md
git add .
git commit

git log --oneline --graph --all

git switch -c notmaster
git status

echo "\nwritten on notmaster" >> README.md
git add .
git commit

git log --oneline --graph --all

2.2 Create and Fix Merge conflict

git status
git switch master
git merge notmaster
# ...fix

If you screwed up: git merge --abort and try again

3. Typical feature branch workflow

  1. Pull master/main to be up2date
  2. Branch out for a new feature (maybe encode issue digit)
  3. Work on feature
  4. Open Merge Request onto master/main
  5. (Manual) Review Process
  6. Feature on master/main :)

Fancy pants features

Goal: Know that they exist

1. Gitignore / Gitkeep

  • Can be anywhere in git repo (not only root)
  • Example:
exact_match.txt
node_modules/  # i.e. a directory
/just_in_this_folder.txt
*.secret

2. Git Blame

  • Usually never used in terminal
  • See web interface

3. Git Bisect

bisect steps on history

git bisect start
git bisect bad
git bisect good <good-commitish> # for example, tags or branch (as their heads) are also okay

Then iteratively

git bisect good # or
git bisect bad

At the end

git bisect reset

3.1 Automated Testing

git bisect start
git bisect bad
git bisect good <good-commitish>
git bisect run ./test_script.sh

4. Git Stash

  • Problem: You want to pull but have stuff uncommitted (but tracked)
  • Solution: git stash (a stack for temporary data)
git stash
git pull
git stash pop

5. "Fixing" More Stuff (rm/reset/revert)

5.1 Untrack single file

git rm --cached <path>

5.2 Remove file, remove from git as well

git rm -f <path>

5.3 Undo (local) commit, keep files staged

git reset --soft HEAD~1

5.4 Undo (local) commit, remove update destructively

git reset --hard HEAD~1

5.5 Do a commit that does the inverse

git revert <commitish>

Where to find more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment