Git & GitHub/GitLab#

Git manages your code changes.

Github / Gitlab are hosting & collaborating platforms.

Git#

Read Git-Fu Basics: White Belt for a quick introduction into git.

Above article covered:

  • A git repository is a chain of commits.

  • A commit is recorded changes to a file, a message, a link to the parent commit and a hash

  • Branches (names pointing to certain commits)

    • Branch names such as main and day-2

    • The current branch HEAD

  • origin is the default name for the remote repository

  • File states

    1. modified: Changed but not committed

    2. staged: Marked to go into next commit

    3. commited: Stored in local Git repository

  • Two modes of interaction:

    • CLI (Commandline interface) through your terminal.

    • GUI (Graphical user interface) through your editor (Pycharm).

    ┌─────────┐     ┌───────┐     ┌──────────┐     ┌──────────┐
    │Working  │     │Staging│     │Local     │     │Remote    │
    │directory│     │area   │     │repository│     │repository│
    └────┬────┘     └────┬──┘     └─────┬────┘     └─────┬────┘
         │               │              │                │
     add │──────────────►│              │                │
         │               │              │                │
  commit │               ├─────────────►│                │
         │               │              │                │
    push │               │              ├───────────────►│
         │               │              │                │
    pull │               │              │◄───────────────┤
         │               │              │                │
 restore │◄──────────────┤              │                │
         │               │              │                │
  revert │◄─────────────────────────────┤                │

Basic configuration#

First time configuration of Git.

Name and email address#

git config --global user.name "Forename Surname"
git config --global user.email "forename.surname@mail.com"

Merge strategy on pull#

git config pull.rebase false  # choose merge, which is the default strategy

Preferred text editor#

Editor Configuration command
Notepad (Win) $ git config --global core.editor "c:/Windows/System32/notepad.exe"
Notepad++ (Win, 32-bit install) $ git config --global core.editor "'c:/program files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Notepad++ (Win, 64-bit install) $ git config --global core.editor "'c:/program files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Gedit (Linux) $ git config --global core.editor "gedit --wait --new-window"
Scratch (Linux) $ git config --global core.editor "scratch-text-editor"
VS Code $ git config --global core.editor "code --wait"
PyCharm $ git config --global core.editor "pycharm --wait"
Editor configuration (https://swcarpentry.github.io/git-novice/02-setup.html)

Cheatsheet (CLI)#

General workflow#

# Where am I?
git status

# Pull changes from remote (GitLab)
git pull

# Create new branch and switch to it
git switch --create branchname

# Add modified files from working tree to staging area
git add filename

# What changed? -> GUI might be more helpful
git diff

# Commit files from staging area
git commit

# Push commited changes to remote (GitLab)
git push

# Show history! -> GUI might be more helpful
git log

# Switch back to main
git switch main

Undo changes#

# Remove from staging area
git restore --staged filename

# Undo commit (creates a new commit wich undoes previous one)
git revert commithash

Merge#

Output produced by git log --all --oneline --graph

Two diverging branches: main and day-2

* 3acf970 (HEAD -> main, origin/main, origin/HEAD) reformulate additional notes
* ed8a33f add documentation about git
| * a7c27b9 (origin/day-2, day-2) docs: add link to pycharm integration of ruff
| * 2a3c318 add ruff as linter and formatter
|/
* a1195fa initial commit

Merge of the two branches (git merge day-2) creates a new commit. Merge commits are special commits pointing to multiple previous commits.

*   628f615 (HEAD -> main) Merge branch 'day-2'
|\
| * a7c27b9 (origin/day-2, day-2) docs: add link to pycharm integration of ruff
| * 2a3c318 add ruff as linter and formatter
* | 3acf970 (origin/main, origin/HEAD) reformulate additional notes
* | ed8a33f add documentation about git
|/
* a1195fa initial commit

Merge conflicts#

How to resolve merge conflicts?

Additional Notes#

  • Git stores all of its repository data in the .git directory.

  • You should never store passwords as plain text in code. How can we use environment variables in Python to use secretes like passwords? (os.getenv("MY_PASSWORD"))

  • Make use of .gitignore files for files which should not be part of the repository.