Control

Git and Github

Image generated with ChatGPT
slides by Lukas Steinwender

Git

  • local
  • version control
    • editing code without affecting latest working version
  • crucial for collaborating on code
  • useful for personal projects
  • learn/try interactively

GitHub

  • remote (cloud)
  • allows to share code with others
slides by Lukas Steinwender

Useful Commands

#getting the repo
git clone <url/to/repo>
git fork <url/to/repo>
#version update
git status
git pull                        #remote
git diff
git add <yourfiles>
git commit -m "<your message>"
git push                        #remote
git remote [-rm <file>]         #remote interaction                                             
#branching
git checkout [-b] <branchname>
git branch [-a]
git merge <branch to be merged into current branch>
git rebase <branch to be rebased onto current branch>
git log --graph --oneline --decorate --all --color                                      
slides by Lukas Steinwender

Repository Layout

  • .git/ directory
    • GitHubs way of tracking changes, branches, etc.
  • .gitignore
    • powerful file specifying what shall and shall not be tracked
  • README.md (optional - but basically required)
    • essentially welcome page
    • project outline and summary
    • quick reference
  • LICENSE (optional)
    • license file for publishing
slides by Lukas Steinwender

Rebase vs Merge

Merge

  • in principle automatic (requires user action for merge conflicts)
gitGraph
    commit id: "A"
    commit id: "B"
    branch feature
    checkout feature
        commit id: "C"
    checkout main
    commit id: "D"
    commit id: "E"
    merge feature id: "C "
    commit id: "F"

Rebase

gitGraph
    commit id: "A"
    commit id: "B"
    branch feature
    checkout feature
        commit id: "C"
        commit id: "D"
        commit id: "E"
    checkout main
    cherry-pick id: "C"
    commit id: "D "
    commit id: "E "
slides by Lukas Steinwender

Clone vs Fork

Clone (Copy Linked to Original)

  • sets original repository as origin
  • you can sync changes to your local copy if the devs updated the code
  • push changes will be reflected in the original (if you have permission)

Fork (New, Isolated Copy)

  • completely new copy of repo
    • you are in control of that copy
  • original developers do not know about forked copy
  • push changes only affect your forked copy
  • edits to original can be suggested via pull requests
slides by Lukas Steinwender

Pull Requests

h:

  • fork repository you want to contribute to
  • git clone forked repo to your local machine
  • create new working branch: git checkout -b <your feature>
  • make changes
  • add, commit, push
  • on GitHub
    • navigate to pull request tab
    • click new pull request
    • select you want to merge from on the right (your new branch)
    • select branch to merge into on the left (main of original repo)
    • click create pull request
slides by Lukas Steinwender

Industry Workflows

slides by Lukas Steinwender

GitHub Flow

gitGraph
    commit id: "initial commit"
    branch feature1
    checkout feature1
        commit id: "change1"
    checkout main
    merge feature1 id: "v1.0"
    branch feature2
    checkout feature2
        commit id: "change2.1"
    checkout main
    branch feature3
    checkout feature3
        commit id: "change3"
    checkout feature2
    commit id: "change2.2"
    checkout main
    merge feature2 id: "v2.0"
    merge feature3 id: "v3.0"
  • lightweight
  • small projects
  • straightforward
  • continuous integration
  • fast feedback
slides by Lukas Steinwender

Git Flow

gitGraph
    commit id: "v0.1"
    %%setup long-lived branches
    branch hotfix order: 2
    branch dev order: 3
    %%add commit history
    checkout dev
        commit id: "new feature1"
    checkout hotfix
        commit id: "fix1"
    checkout main
        merge hotfix id: "v0.2"
    checkout dev
        branch feature1 order: 5
    checkout feature1
        commit id: "feature1.1"
    checkout dev
        commit id: "new feature2"
        branch feature2 order: 4
    checkout feature2
        commit id: "feature2.1"
    checkout dev
        merge hotfix
    checkout feature2
        commit id: "feature2.2"
    checkout feature1
        commit id: "feature1.2"
    checkout dev
        merge feature2
    branch release order: 2
    checkout release
        commit id: "release testing"
        commit id: "release bug fixes"
    checkout main
        merge release id: "v1.0"
    checkout dev
        merge release
    checkout feature1
        commit id: "feature1.3"
  • more complex
  • allows parallel development
  • main branch remains stable
slides by Lukas Steinwender

Dev-Feature (Simplification of Git Flow)

gitGraph
    commit id: "v0.1"
    branch hotfix
    branch dev
    checkout hotfix
        commit id: "hf1"
        commit id: "hf2"
    checkout main
    merge hotfix id: "v0.2"
    checkout dev
        branch featureA
        checkout featureA
            commit id: "devA1"
            commit id: "devA2"
        checkout dev
        merge featureA id: "dev1.0"
    checkout main
    merge dev id: "v1.0"
    checkout main
    checkout dev
    commit id: "dev2.0"
        branch featureB
        checkout featureB
        commit id: "devB1"
        commit id: "devB2"
        commit id: "devB3"
    checkout dev
    merge featureB id: "dev2.01"
    checkout main
    merge dev id: "v2.0"
  • my simplification of Git Flow
  • stable main branch
  • no parallel development of features
  • dev for nightly builds
slides by Lukas Steinwender

Version-Feature

gitGraph
    commit id: "v0.1"
    branch version1.0
    checkout version1.0
        branch featureA
        checkout featureA
            commit id: "devA1"
            commit id: "devA2"
        checkout version1.0
        merge featureA id: "v0.2"
    checkout main
    merge version1.0 id: "v1.0"
    branch version2.0
    checkout version2.0
        commit id: "v1.1" 
        branch featureB
        checkout featureB
            commit id: "devB1"
            commit id: "devB2"
            commit id: "devB3"
        checkout version2.0
        merge featureB id: "v1.2"
    checkout main
    merge version2.0 id: "v2.0"
  • my enhancement on GitHub Flow
  • stable main branch
  • version branches
    * keep history
    * nightly builds
  • feature branches for each version
  • no parallel development of features
slides by Lukas Steinwender

Submodules

  • linking to other git-repos
  • repo-within a repo
    • changes synced upon git pull
#linking
git submodule add <url/to/git repo>
#unlinking
git config -f .gitmodules --remove-section submodule.path/to/submodule #remvove submodule entry
git config -f .git/config --remove-section submodule.path/to/submodule #remove submodule from git config
git rm --cached path/to/submodule #remove submodule from git's tracking
rm -rf path/to/submodule/.git #delete .git directory (convert to normal directory)

#track changes
git add path/to/submodule
git commit -m "Convert submodule to regular directory"
slides by Lukas Steinwender

VSCode

  • suite of tools for git-incorporation
slides by Lukas Steinwender

Action!

  1. clone the repo used in this course:
    https://github.com/swincas/cnc-computing-course.git
  1. on GitHub, init a new repo that can host all your useful code-snippets
  2. based on one of the industry workflows, create a dev branch that will host your developments detached from main
  3. modify the README.md file and push the changes
slides by Lukas Steinwender

Good To Know

GitHub Education

  • requires picture of student card
  • benefits
slides by Lukas Steinwender

* let's look at a [remote repo](https://github.com/TheRedElement/LuStCodeSnippets/tree/main)

<div class="mermaid">

</div>