From 63acc390e6417a716ebd789c07530414db7d0981 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Mon, 26 Feb 2024 14:06:51 +0100 Subject: Add git course (reveal.js) and SQLAlchemy 2 (notebooks) --- notebooks/python/python_3_8_to_3_11.ipynb | 2 +- notebooks/python/python_intro.ipynb | 2 +- notebooks/python/python_oo.ipynb | 2 +- notebooks/sqlalchemy/sqlalchemy.ipynb | 2 +- reveal.js/free_software.html | 219 +++++++++++++ reveal.js/git.html | 513 ++++++++++++++++++++++++++++++ reveal.js/images/git/advance-master.png | Bin 0 -> 7140 bytes reveal.js/images/git/advance-testing.png | Bin 0 -> 8146 bytes reveal.js/images/git/basic-rebase-2.png | Bin 0 -> 8218 bytes reveal.js/images/git/basic-rebase-3.png | Bin 0 -> 7741 bytes reveal.js/images/git/branches.png | Bin 0 -> 22842 bytes reveal.js/images/git/checkout-master.png | Bin 0 -> 7583 bytes reveal.js/images/git/commit.png | Bin 0 -> 19774 bytes reveal.js/images/git/head-to-master.png | Bin 0 -> 9977 bytes reveal.js/images/git/head-to-testing.png | Bin 0 -> 9884 bytes reveal.js/sqlalchemy.html | 301 +++++++++--------- 16 files changed, 887 insertions(+), 154 deletions(-) create mode 100644 reveal.js/free_software.html create mode 100755 reveal.js/git.html create mode 100644 reveal.js/images/git/advance-master.png create mode 100644 reveal.js/images/git/advance-testing.png create mode 100644 reveal.js/images/git/basic-rebase-2.png create mode 100644 reveal.js/images/git/basic-rebase-3.png create mode 100644 reveal.js/images/git/branches.png create mode 100644 reveal.js/images/git/checkout-master.png create mode 100644 reveal.js/images/git/commit.png create mode 100644 reveal.js/images/git/head-to-master.png create mode 100644 reveal.js/images/git/head-to-testing.png diff --git a/notebooks/python/python_3_8_to_3_11.ipynb b/notebooks/python/python_3_8_to_3_11.ipynb index e2a08e9..b8724e8 100644 --- a/notebooks/python/python_3_8_to_3_11.ipynb +++ b/notebooks/python/python_3_8_to_3_11.ipynb @@ -834,7 +834,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/notebooks/python/python_intro.ipynb b/notebooks/python/python_intro.ipynb index 171b83c..f129ffe 100644 --- a/notebooks/python/python_intro.ipynb +++ b/notebooks/python/python_intro.ipynb @@ -1053,7 +1053,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/notebooks/python/python_oo.ipynb b/notebooks/python/python_oo.ipynb index 0ab369b..e38d7f3 100644 --- a/notebooks/python/python_oo.ipynb +++ b/notebooks/python/python_oo.ipynb @@ -1418,7 +1418,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/notebooks/sqlalchemy/sqlalchemy.ipynb b/notebooks/sqlalchemy/sqlalchemy.ipynb index 58a7eb9..0d2a966 100644 --- a/notebooks/sqlalchemy/sqlalchemy.ipynb +++ b/notebooks/sqlalchemy/sqlalchemy.ipynb @@ -991,7 +991,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/reveal.js/free_software.html b/reveal.js/free_software.html new file mode 100644 index 0000000..71bea85 --- /dev/null +++ b/reveal.js/free_software.html @@ -0,0 +1,219 @@ + + + + + Free software in a nutshell + + + + + + + + + + + + + + + +
+ + +
+ +
+

Free software in a nutshell

+

Free as in freedom

+
+

Simeon Simeonov - TDE

+
+ +
+ +
+

Agenda

+
+
    +
  • What is free software - historical context
  • +
  • Free software vs. "open source"
  • +
  • Free software licenses
  • +
  • Debunking some of the most common myths about free software
  • +
  • The main practical advantages of free software
  • +
  • What can Statnett and I do?
  • +
  • Q & A
  • +
+
+ +
+ +
+

Historical context

+
    +
  • In the late 1970s while working as a programmer at MIT AI Laboratory, Richard Matthew Stallman (b. 1953), also known by his initials rms, became frustrated when most manufacturers stopped distributing source code and began using copyright and restrictive software licenses to limit or prohibit copying and redistribution. This was a consequence triggered by the US Copyright Act of 1976.

  • +
  • In September 1983 Stallman announced the plan for the GNU operating system - a free operating system "compatible with Unix so that it would be portable, and so that Unix users could easily switch to it". The name GNU is a recursive acronym for "GNU's Not Unix".

  • +
  • In February 1984, Stallman quit his job at MIT to work full-time on the GNU project. Since then, he had remained affiliated with MIT as an unpaid "visiting scientist" in the Computer Science and Artificial Intelligence Laboratory.

  • +
  • In 1985, Stallman published the GNU Manifesto, which outlined his motivation for creating a free operating system called GNU. Soon after, he started a nonprofit corporation called the Free Software Foundation (FSF) to employ free software programmers and provide a legal infrastructure for the free software movement. Stallman was the nonsalaried president of the FSF, which is a 501(c)(3) nonprofit organization founded in Massachusetts. Stallman popularized the concept of copyleft, a legal mechanism to protect the modification and redistribution rights for free software.

  • +
+
+ +
+

What is free software?

+

A program is free software if the program's users have the four essential freedoms:

+
    +
  • Freedom 0 - The freedom to run the program as you wish, for any purpose

  • +
  • Freedom 1 - The freedom to study how the program works, and change it so it does your computing as you wish. Access to the source code is a precondition for this

  • +
  • Freedom 2 - The freedom to redistribute copies so you can help others

  • +
  • Freedom 3 - The freedom to distribute copies of your modified versions to others. By doing this you can give the whole community a chance to benefit from your changes. Access to the source code is a precondition for this

  • +
+

"A program is free software if it gives users adequately all of these freedoms. Otherwise, it is nonfree. While we can distinguish various nonfree distribution schemes in terms of how far they fall short of being free, we consider them all equally unethical." - GNU (Philosophy)

+

The term "libre" has been used lately

+
+ +
+

The GNU operating system

+
    +
  • By June 1987, the project had accumulated and developed free software for an assembler, an almost finished portable optimizing C compiler - GCC, an editor - GNU Emacs, and various Unix utilities (such as ls, grep, awk, make and ld). They had an initial kernel that needed more updates.

  • +
  • By 1992, the GNU project had completed all of the major operating system utilities, but had not completed their proposed operating system kernel - GNU Hurd. With the release of the Linux kernel, started independently by Linus Torvalds in 1991, and released under the GPLv2 with version 0.12 in 1992, for the first time it was possible to run an operating system composed completely of free software. Though the Linux kernel is not part of the GNU project, it was developed using GCC and other GNU programming tools and was released as free software under the GNU General Public License. Most compilation of the Linux kernel is still done with GNU toolchains.

  • +
  • The GNU packages consist of numerous operating system tools and utilities (shell, coreutils, compilers, libraries, etc.) including a library implementation of all of the functions specified in POSIX System Application Program Interface (POSIX.1). The GCC compiler can generate machine-code for a large variety of computer-architectures.

  • +
  • Linux kernel – this implements program scheduling, multitasking, device drivers, memory management, etc. and allows the system to run on a large variety of computer-architectures.

  • +
  • The packaging of GNU tools, together with the Linux kernel and other programs, is usually called a Linux distribution (distro). The GNU Project calls the combination of GNU and the Linux kernel "GNU/Linux", and asks others to do the same, resulting in the GNU/Linux naming controversy.

  • +
+

Visit https://www.gnu.org/software/ for a complete list of the GNU packages

+
+ +
+

Free software vs. "open source"

+
    +
  • In February 1998 Bruce Perens and Eric S. Raymond (esr) founded the Open Source Initiative

  • +
  • Stallman argues that software users should have the freedom to share with their neighbors and be able to study and make changes to the software that they use. He maintains that attempts by proprietary software vendors to prohibit these acts are antisocial and unethical. He argues that freedom is vital for the sake of users and society as a moral value, and not merely for pragmatic reasons such as possibly developing technically superior software.

  • +
  • Eric S. Raymond argues that moral arguments, rather than pragmatic ones, alienate potential allies and hurt the end goal of removing code secrecy.

  • +
+

Most "open source" software is a free software, but there are exceptions (f.i. qmail, nmap, True crypt... )

+

The term FOSS - (Free and open-source software) is widely used to describe specific software.

+
+ +
+

Other free software

+
    +
  • The earliest distributions of Unix from Bell Labs in the 1970s included the source code to the operating system, allowing researchers at universities to modify and extend Unix

  • +
  • In 1974 the operating system arrived at University of California Berkeley, at the request of computer science professor Bob Fabry

  • +
  • The Berkeley Software Distribution - BSD just included some extra features, which were intertwined with code owned by AT&T

  • +
  • Extensive academic research during the 80s. 4.3BSD was released in June 1986

  • +
  • 4.3BSD + TCP/IP stack was freely redistributable under the terms of the BSD license. It was released in June 1989

  • +
  • 1993 - FreeBSD and NetBSD are first released

  • +
  • 1995 - The last 4.4BSD is released. OpenBSD is released

  • +
+

The permissive nature of the BSD license has allowed many other operating systems, both open-source and proprietary, to incorporate BSD source code. Microsoft Windows used BSD code in its implementation of TCP/IP and bundles recompiled versions of BSD's command-line networking tools since Windows 2000. Darwin, the basis for Apple's macOS and iOS, is based on 4.4BSD-Lite2 and FreeBSD. Plastation (3,4,5), Nintendo Switch...

+
+ +
+

Free software licenses (GPL)

+

The Free Software Foundations is behind several versions of the GNU General Public License (GNU GPL or simply GPL)

+

Copyleft - any derivative work must be distributed under the same or equivalent license terms

+
    +
  • GPLv1 - Released in 1989 and no longer used

  • +
  • GPLv2 - Released in 1991 and still in use (f.i. the Linux kernel) - "strong copyleft" not even allowing integration of GPL licensed software components into non GPL software

  • +
  • GPLv3 - Released in 2007 - changes in regard to software patents, DRM and alike.

  • +
  • LGPL(2 and 3) - "weak copyleft" - making it possible to use and integrate a software component released under the LGPL into (even proprietary) software without being required by the terms of a strong copyleft license to release the source code of their own components

  • +
+

GPLv2+ or GPLv3+ stands for "2 (or 3) or any later version" and is the default form of the license

+
+ +
+

Free software licenses (BSD)

+

3-clause BSD license ("BSD License 2.0", "Revised BSD License", "New BSD License", or "Modified BSD License") and 2-clause BSD license ("Simplified BSD License" or "FreeBSD License") are the most popular BSD licenses. No copyleft.

+
    +
  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer

  • +
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution

  • +
  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission (3rd clause)

  • +
+
+ +
+

Free software licenses (Apache)

+

A permissive free software license written by the Apache Software Foundation (ASF). No copyleft.

+

Requiring a patent license on contributions that necessarily infringe a contributor's own patents. This license requires preservation of the copyright notice and disclaimer.

+
+ +
+

Free software licenses (MIT)

+

A permissive free software license written by MIT. No copyleft. Does not mention patents.

+

"The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software."

+
+ +
+
+

Some of the most common myths about free software

+
    +
  • If you use free software and modify it, you have to release your modifications (licensed under the GPL)

  • +
  • You will never get proper / commercial support if you use free software

  • +
  • Free software is free of charge

  • +
  • Free software is not / anti commercial

  • +
+
+
+ +
+

The main practical advantages of free software

+
    +
  • Better personal control - avoiding vendor lock-in

  • +
  • Privacy and security - nothing happens behind your back, the security of a software is verified by the community and not by the company that made it

  • +
  • Quality over profit

  • +
+
+ +
+

What can Statnett do?

+
    +
  • Using free software with commercial support

  • +
  • Developing and sharing free software

  • +
  • Modifying and sharing free software

  • +
  • Supporting free software projects - software that is already in use at Statnett

  • +
  • Influencing free software projects by purchasing / ordering new features

  • +
+
+ +
+

What can I do?

+
    +
  • Consciousness raising

  • +
  • Developing and sharing free software

  • +
  • Donating (FSF, FSFE, or any other free software peoject) instead of "When are they going to implement / fix this?"

  • +
+
+ +
+

Q & A

+
+ +
+
+ + + + + + + + + + + diff --git a/reveal.js/git.html b/reveal.js/git.html new file mode 100755 index 0000000..6ee3406 --- /dev/null +++ b/reveal.js/git.html @@ -0,0 +1,513 @@ + + + + + Introduction to git + + + + + + + + + + + + + + + +
+ + +
+ +
+

Introduction to Git

+
+

Competence group (Faggruppe) Python

+

Simeon Simeonov - TDE

+
+ +
+ +
+

Agenda

+
+
    +
  • What is Git? A brief history
  • +
  • Working locally. Dealing with commits and branches
  • +
  • Using remotes and GitLab
  • +
  • Walkthrough. Demonstrating some common Git flows
  • +
+
+ +
+ + +
+

What is Git?

+

Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development.

+

Distributed version control is a form of version control in which the complete codebase, including its full history, is mirrored on every developer's computer.

+
    +
  • Git development was started by Linus Torvalds on 3. April 2005
  • +
  • Torvalds wanted a distributed system that he could use like BitKeeper, but none of the available free (as in freedom) systems met his needs
  • +
  • The main objectives were: performance, very strong safeguards against corruption, either accidental or malicious, distributed workflow
  • +
  • The name: git means "unpleasant person" in British English slang. "I'm an egotistical bastard, and I name all my projects after myself. First 'Linux', now 'git'.". The man page describes Git as "the stupid content tracker".
  • +
  • Torvalds achieved his performance goals on 29 April 2005
  • +
  • Torvalds turned over maintenance on 26 July 2005 to Junio Hamano, a major contributor to the project. Hamano was responsible for the 1.0 release on 21 December 2005
  • +
  • Current (23. February 2024) stable version: 2.44.0
  • +
+
+ + +
+

Configuring Git

+
+

We start be configuring Git for our needs

+
    +
  • git config - gets and sets repository options
  • +
  • git config --global - gets and sets global options
  • +
+ +

Using WSL / devbox or Git for Windows (from Software Center):

+
+            
+              # - this is a comment and the remainder of this line will be ignored by the shell
+              git config --global user.name "Simeon Simeonov"
+              git config --global user.email "simeon.simeonov@statnett.no"
+              git config --global init.defaultBranch master  # sets 'master' as the default branch name
+              # git config --global init.defaultBranch main  # sets 'main' as the default branch name
+            
+          
+

Note: The recommended name for the default branch is 'main'

+
+ + +
+

The Git repository

+
+

A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.

+
    +
  • git init - creates an empty Git repository or reinitializes an existing. A new .git subdirectory is created in your current working directory. This will also create a new master branch.
  • +
  • git add - add file contents to the index - a file or directory (folder) contents are selected to be tracked
  • +
+
+            
+              mkdir myproject  # creates a new folder named 'myproject'
+              cd myproject  # sets 'myproject' as current working directory
+              touch README.md pycode.py  # creates the empty files 'README.md' and 'pycode.py' in the new folder
+              git init  # creates an empty Git repository inside 'myproject' - a .git folder is added
+              git add README.md pycode.py  # selects 'README.md' and 'pycode.py' for tracking
+            
+          
+
+ + +
+

Tracking content

+
+
    +
  • git status - shows the working tree status
  • +
+

Each file in your working directory can be in one of two states:

+
    +
  • tracked - the files Git knows and "cares" about - files that were in the last snapshot, as well as any newly staged files. They can be unmodified, modified, or staged.
  • +
  • untracked - files that Git doesn't "care" about
  • +
+

Often, you'll have a class of files that you don't want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:

+
+            
+              cat .gitignore
+              *.pyc
+              *~              
+            
+          
+
+ + +
+

Committing changes

+
+

A commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as "safe" versions of a project. A snapshot can be seen as a "node" (with an unique id) related to the previous commit (unless initial commit)

+
    +
  • git commit - records changes to the repository
  • +
+ +

Any files you have created or modified that you haven't run git add on since you edited them - won't go into this commit. They will stay as modified files on your disk.

+
+ + +
+

Committing changes (cont.)

+
+
    +
  • git diff - shows changes between commits, commit and working tree, etc
  • +
  • git log - shows commit logs
  • +
  • git show - shows various types of objects
  • +
  • git tag - creates, lists, deletes or verifies a tag object
  • +
+
+            
+              cd myproject
+              echo "# My project documentation" > README.md  # sets a basic content for the file README.md
+              echo "import os" >> pycode.py  # appends a new content to the file pycode.py
+              git add README.md pycode.py  # stages the changes
+              git commit -m "Initial commit"  # creates a commit with commit message
+              # git commit -a -m "Initial commit"  # automatically stages files that have been modified
+              git log  # shows the existing commits (only one so far)
+              echo "import sys" >> pycode.py  # appends a new line to 'pycode.py'
+              git status  # shows that one file - 'pycode.py' has been modified since the last commit
+              git diff  # shows that changes for this repository
+              git diff pycode.py  # shows the changes for a particular file
+              git commit -a -m "Import the sys module"  # stages and commits the last changes
+              git tag v0.1  # creates a lightweight tag 'v0.1'
+              git log  # shows that a new commit has been added
+              git show <commit id>  # shows the files and their changes that are part of a given commit
+            
+          
+
+ + +
+

Undoing things

+
+
    +
  • git restore - restores working tree files (Git version >= 2.23.0)
  • +
+
+            
+              cd myproject
+              touch newpycode.py  # creates a new file
+              git add newpycode.py  # sets the file as tracked (and stages it)
+              echo "import sys" >> pycode.py  # appends yet another new line
+              echo "The new project documentation is here" >> README.md  # appends yet another new line
+              git add README.md  # stages README.md
+              git status  # shows 'newpycode.py' (new file), 'pycode.py' (modified), 'README.md' (modified and staged)
+
+              git restore --staged newpycode.py  # 'newpycode.py' now becomes untracked
+              # git reset HEAD newpycode.py  # Git version < 2.23.0
+
+              git restore --staged README.md  # unstages 'README.md' (modified)
+              # git reset HEAD README.md  # Git version < 2.23.0
+
+              git restore pycode.py  # reverts all modifications on 'pycode.py'
+              # git checkout -- pycode.py  # Git version < 2.23.0
+            
+          
+
+ + +
+

Git branching

+

Branching means you diverge from the main line of development and continue to do work without messing with that main line. Some people refer to the Git branching model as its "killer feature", and it certainly sets Git apart in the VCS community. The way Git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast. Unlike many other VCSs, Git encourages workflows that branch and merge often, even multiple times in a day. Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.

+ +
+ + +
+

Git branching (cont.)

+
+
    +
  • git branch - lists, creates, or deletes branches
  • +
  • git checkout - switches branches or restores working tree files
  • +
+
+            
+              git branch testing  # creates a new branch from 'master' called 'testing'
+            
+          
+ +
+ + +
+

Git branching (cont.)

+
+
+            
+              git checkout testing  # makes 'testing' testing the current (active) branch
+              # git checkout -b testing  # creates a new branch named 'testing' and makes it active
+            
+          
+ +
+ + +
+

Git branching (cont.)

+
+
+            
+              echo "Extra content" >> README.md
+              git commit -a -m "Improve the documentation"  # creates a new commit on the 'testing' branch
+            
+          
+ +
+ + +
+

Git branching (cont.)

+
+
+            
+              git checkout master  # "switches" to 'master'
+            
+          
+ +
+ + +
+

Git branching (cont.)

+
+
+            
+              echo "# Extra comment" >> pycode.py
+              git commit -a -m "Add a very useful comment to the Python code"  # creates a new commit on the 'master' branch
+            
+          
+ +
+ + +
+

Merging

+

In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase

+
    +
  • git merge - joins two or more development histories together
  • +
+

There are three possible outcomes when attempting to merge two different branches:

+
    +
  • fast forward - the last common node between the source and the target branch is also the last node on the target branch (no changes since the source branch branched out). The changes (the commits) on the source branch are simply appended to the target branch. No new commits are created.
  • +
  • merge commit - both the source and the target branch have new commits after the last common node. A new commit node is created on the target branch
  • +
  • conflict - both the source and the target branch have new commits after the last common node. At least one file has been edited differently on both branches. A three-way-merge is performed resulting in a new commit node is created on the target branch
  • +
+
+ + +
+

Rebasing

+

With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.

+
    +
  • git rebase - reapply commits on top of another base tip
  • +
+

The following is a typical situation when dealing with two diverged branches. A new merge commit (C5) is created when using merge:

+
+            
+              git checkout master
+              git merge experiment  # merges 'experiment' into 'master'
+            
+          
+ +
+ + +
+

Rebasing (cont.)

+
+            
+              git checkout experiment  # Note!! (not master)
+              git rebase master
+            
+          
+ +

...now the following operation will result in a fast-forward merge:

+
+            
+              git checkout master
+              git merge experiment
+            
+          
+

N.B. Do not rebase commits that exist outside your repository and that people may have based work on!

+
+ + +
+

Remotes

+
+

To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Locally a remote can be seen as a sort of bookmark.

+
    +
  • git remote - manages set of tracked repositories (remotes)
  • +
  • git clone - clones a repository into a new directory. A remote named "origin" will be automatically created
  • +
  • git fetch - downloads objects and refs from another repository
  • +
  • git pull - fetches from and integrates with another repository or a local branch
  • +
  • git push - updates remote refs along with associated objects
  • +
+

The two most popular protocols (schema) for interacting with remotes are ssh and http(s) (usually read-only)

+
+ + +
+

GitLab

+
+

GitLab is a developer platform that allows developers to create, store, manage and share their code. It uses Git, providing the distributed version control of Git plus access control, bug tracking, software feature requests, task management, continuous integration...

+

Statnett operates its own instance at https://gitlab.statnett.no. It is used for storing / managing Statnett's own Git repositories

+

Another popular and widely used platform is GitHub - https://github.com

+
+ + +
+

Walkthrough

+
+

The following walkthrough illustrates some of the most common patterns when two or more parties are using Git and GitLab in a typical project at Statnett.

+

Note: GitLab's CI/CD will not be included in this presentation.

+

Note: This presentation should not be considered as a reference but merely as an introduction.

+
+ + +
+

Walkthrough - Accessing GitLab

+
+

In order for a local repository to be able to interact with GitLab through the SSH protocol, a private / public SSH key pair has to be created

+
+            
+              ssh-keygen -t ed25519  # interactively creates a key pair in ~/.ssh
+              cat ~/.ssh/id_ed25519.pub  # displays the public key
+            
+          
+

The public key is then added (pasted) under User settings -> SSH Keys -> Add new key

+
+ + +
+

Walkthrough - Initial code / repository

+
+

When starting a new repository that is about to be shared using GitLab, a corresponding GitLab project is created first. Then there are two possible approaches.

+
    +
  • a new repository is created on GitLab and then cloned (git clone) by all other parties
  • +
  • the repository is initialized locally and then pushed (git push) to GitLab
  • +
+
+            
+              git clone <url>  # clones (copies) the repository
+
+              git remote add origin <url>  # adds a new remote in the local repository
+              git push origin main
+            
+          
+

All parties should have a local copy of the same repository. For this demonstration we can imagine two users, randomly called "Daniel" and "Simeon".

+
+ + +
+

Walkthrough - A very simple flow

+
+

Usually changes should not be committed directly into the main branch. Daniel starts by creating a new branch from "main" called "feature1"

+

He commits his changes there and pushes them to the remote (origin) branch "feature1". He then creates a merge request (MR) (also known as a pull request on GitHub) to the main branch on the remote origin

+
+            
+              git checkout main
+              git checkout -b feature1
+              # editing some code
+              git commit -a -m "Add a new function"
+              git push origin feature1
+              # MR is created and reviewed by Simeon. "feature1" is then merged into "main"
+              git checkout main
+              git pull origin main  # done before the next time we want to branch out from "main"
+            
+          
+
+ + +
+

Walkthrough - A more complex flow using rebase

+
+

Simeon starts by updating his local main branch. He then branches out from "main" to a branch called "interesting". He commits his changes there.

+

At the same time Daniel branches out to "moreinteresting" and commits changes (to a different file).Daniel pushes his changes to origin "moreinteresting" and then using MR to origin "main".

+

The next day Simeon rebases "main" onto his "interesting". Finally Simeon creates his own MR after pushing his "intersting" branch to origin interesting

+
+            
+              git checkout main  # both Simeon and Daniel
+              git checkout -b interesting  # Simeon
+              git checkout -b moreinteresting  # Daniel
+              # editing some code
+              git commit -a -m "Add a new interesting feature"  # Simeon
+              git commit -a -m "Add a new even more interesting feature"  # Daniel
+              git push origin moreinteresting  # Daniel
+
+              # The next day (Simeon)
+              git fetch origin  # the current branch is still "interesting"
+              git rebase origin/main
+              git push origin interesting
+              # creates a MR
+              git pull origin main  # done before the next time we want to branch out from "main"
+            
+          
+
+ + +
+

Walkthrough - Squahing commits

+
+

The act of "squashing" your commits means that you combine multiple existing commits into a single one. If you should do this or avoid it is - to some extent - a question of preference: in some teams, for example, squashing commits is the preferred way to merge a feature branch back into a long-running branch like "master" or "main".

+

Squashing can be performed either locally (before MR) or on GitLab (before or after MR)

+
+            
+              # squashing locally
+
+              # squashing only a selected amount of commits...
+              # select the last 3 commits interactively,
+              # ordering them with the last at the bottom of the interactive file
+              git rebase -i HEAD~3
+              # We then mark the line at the top (chronologically the first commit) with "pick"
+              # and the rest of the lines with "squash" or "s".
+              # Finally we are also adding a proper commit message.
+
+              # Using "fixup" or "f" instead of "squash" will produce the same result,
+              # except it will not prompt for a new commit message and will use the commit
+              # message of the first commit.
+
+
+              # squashing everything...
+              git merge --squash <branch-name>
+              # will take all the commits from the branch, squash them,
+              # and stage all changes in the current branch
+            
+          
+
+ + +
+

Sources

+
+

https://en.wikipedia.org - Wikipedia

+

https://git-scm.com/book/en/v2/ - Pro Git

+

The official man pages

+
+ + +
+

Q & A

+
+ +
+
+ + + + + + + + + + + diff --git a/reveal.js/images/git/advance-master.png b/reveal.js/images/git/advance-master.png new file mode 100644 index 0000000..c3a7ebe Binary files /dev/null and b/reveal.js/images/git/advance-master.png differ diff --git a/reveal.js/images/git/advance-testing.png b/reveal.js/images/git/advance-testing.png new file mode 100644 index 0000000..312e65e Binary files /dev/null and b/reveal.js/images/git/advance-testing.png differ diff --git a/reveal.js/images/git/basic-rebase-2.png b/reveal.js/images/git/basic-rebase-2.png new file mode 100644 index 0000000..c82140c Binary files /dev/null and b/reveal.js/images/git/basic-rebase-2.png differ diff --git a/reveal.js/images/git/basic-rebase-3.png b/reveal.js/images/git/basic-rebase-3.png new file mode 100644 index 0000000..a1afaef Binary files /dev/null and b/reveal.js/images/git/basic-rebase-3.png differ diff --git a/reveal.js/images/git/branches.png b/reveal.js/images/git/branches.png new file mode 100644 index 0000000..3ddf1cd Binary files /dev/null and b/reveal.js/images/git/branches.png differ diff --git a/reveal.js/images/git/checkout-master.png b/reveal.js/images/git/checkout-master.png new file mode 100644 index 0000000..06e9878 Binary files /dev/null and b/reveal.js/images/git/checkout-master.png differ diff --git a/reveal.js/images/git/commit.png b/reveal.js/images/git/commit.png new file mode 100644 index 0000000..d563972 Binary files /dev/null and b/reveal.js/images/git/commit.png differ diff --git a/reveal.js/images/git/head-to-master.png b/reveal.js/images/git/head-to-master.png new file mode 100644 index 0000000..86c7ae8 Binary files /dev/null and b/reveal.js/images/git/head-to-master.png differ diff --git a/reveal.js/images/git/head-to-testing.png b/reveal.js/images/git/head-to-testing.png new file mode 100644 index 0000000..08352d6 Binary files /dev/null and b/reveal.js/images/git/head-to-testing.png differ diff --git a/reveal.js/sqlalchemy.html b/reveal.js/sqlalchemy.html index bbcd1ca..a0d6617 100644 --- a/reveal.js/sqlalchemy.html +++ b/reveal.js/sqlalchemy.html @@ -98,29 +98,29 @@ metadata = MetaData() production_types_table = Table( - "production_types", - metadata, - Column("production_type_id", Integer, primary_key=True), - Column("code", String(3), nullable=False, unique=True), - Column("description", String), # Column("name", String(128)) is possible + "production_types", + metadata, + Column("production_type_id", Integer, primary_key=True), + Column("code", String(3), nullable=False, unique=True), + Column("description", String), # Column("name", String(128)) is possible ) bidding_areas_table = Table( - "bidding_areas", - metadata, - Column("bidding_area_id", Integer, primary_key=True), - Column("code", String(3), nullable=False, unique=True), - Column("name", String(32)), + "bidding_areas", + metadata, + Column("bidding_area_id", Integer, primary_key=True), + Column("code", String(3), nullable=False, unique=True), + Column("name", String(32)), ) production_plans_table = Table( - "production_plans", - metadata, - Column("record_created_time", DateTime(timezone=False), primary_key=True), - Column("start_time", DateTime(timezone=False), primary_key=True), - Column("bidding_area_id", Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True), - Column("production_type_id", Integer, ForeignKey("production_types.production_type_id"), primary_key=True), - Column("value", Numeric, nullable=False), + "production_plans", + metadata, + Column("record_created_time", DateTime(timezone=False), primary_key=True), + Column("start_time", DateTime(timezone=False), primary_key=True), + Column("bidding_area_id", Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True), + Column("production_type_id", Integer, ForeignKey("production_types.production_type_id"), primary_key=True), + Column("value", Numeric, nullable=False), ) metadata.create_all(engine) # creates the tables @@ -150,13 +150,13 @@ insert_stmt.execute(bidding_area_id=1, code="NO1", name="Elspot NO1") # insert a single entry # ... or a list of entries insert_stmt.execute( - [ - {"bidding_area_id": 2, "code": "NO2", "name": "Elspot NO2"}, - {"bidding_area_id": 3, "code": "NO3", "name": "Elspot NO3"}, - {"bidding_area_id": 4, "code": "NO4", "name": "Elspot NO4"}, - {"bidding_area_id": 5, "code": "NO5", "name": "Elspot NO5"}, - {"bidding_area_id": 6, "code": "NO6", "name": "Elspot NO6"}, - ] + [ + {"bidding_area_id": 2, "code": "NO2", "name": "Elspot NO2"}, + {"bidding_area_id": 3, "code": "NO3", "name": "Elspot NO3"}, + {"bidding_area_id": 4, "code": "NO4", "name": "Elspot NO4"}, + {"bidding_area_id": 5, "code": "NO5", "name": "Elspot NO5"}, + {"bidding_area_id": 6, "code": "NO6", "name": "Elspot NO6"}, + ] ) metadata.bind = engine # no need to explicitly bind the engine from now on @@ -182,21 +182,21 @@ from sqlalchemy.orm import mapper class ProductionType: - def __init__(self, code, description): - self.code = code - self.description = description + def __init__(self, code, description): + self.code = code + self.description = description - def __str__(self): - return self.code + def __str__(self): + return self.code class BiddingArea: - def __init__(self, code, name): - self.code = code - self.name = name + def __init__(self, code, name): + self.code = code + self.name = name - def __str__(self): - return self.code + def __str__(self): + return self.code mapper(ProductionType, production_types_table) mapper(BiddingArea, bidding_areas_table) @@ -212,27 +212,27 @@ from sqlalchemy.orm import relationship class ProductionPlan: - def __init__(self, record_created_time, start_time, production_type, bidding_area, value): - self.record_created_time = record_created_time - self.start_time = start_time - self.production_type = production_type - self.bidding_area = bidding_area - self.value = value - - def __str__(self): - return ( - f"{self.record_created_time} {self.start_time} " - f"{self.production_type} {self.bidding_area} {self.value}" - ) + def __init__(self, record_created_time, start_time, production_type, bidding_area, value): + self.record_created_time = record_created_time + self.start_time = start_time + self.production_type = production_type + self.bidding_area = bidding_area + self.value = value + + def __str__(self): + return ( + f"{self.record_created_time} {self.start_time} " + f"{self.production_type} {self.bidding_area} {self.value}" + ) mapper( - ProductionPlan, - production_plans_table, - properties = { - "production_type": relationship(ProductionType, backref="production_plans"), - "bidding_area": relationship(BiddingArea, backref="production_plans"), - }, + ProductionPlan, + production_plans_table, + properties = { + "production_type": relationship(ProductionType, backref="production_plans"), + "bidding_area": relationship(BiddingArea, backref="production_plans"), + }, ) @@ -249,32 +249,32 @@ Base = declarative_base() class ProductionType(Base): - __tablename__ = "production_types" + __tablename__ = "production_types" - production_type_id = Column(Integer, primary_key=True) - code = Column(String(3), nullable=False, unique=True) - description = Column(String) + production_type_id = Column(Integer, primary_key=True) + code = Column(String(3), nullable=False, unique=True) + description = Column(String) - def __init__(self, code, description): - self.code = code - self.description = description + def __init__(self, code, description): + self.code = code + self.description = description - def __str__(self): - return self.code + def __str__(self): + return self.code class BiddingArea(Base): - __tablename__ = "bidding_areas" + __tablename__ = "bidding_areas" - bidding_area_id = Column(Integer, primary_key=True) - code = Column(String(3), nullable=False, unique=True) - name = Column(String(32)) + bidding_area_id = Column(Integer, primary_key=True) + code = Column(String(3), nullable=False, unique=True) + name = Column(String(32)) - def __init__(self, code, name): - self.code = code - self.name = name + def __init__(self, code, name): + self.code = code + self.name = name - def __str__(self): - return self.code + def __str__(self): + return self.code @@ -288,31 +288,31 @@ from sqlalchemy.orm import relationship, backref class ProductionPlan(Base): - __tablename__ = "production_plans" - - record_created_time = Column(DateTime(timezone=False), primary_key=True) - start_time = Column(DateTime(timezone=False), primary_key=True) - bidding_area_id = Column(Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True) - production_type_id = Column(Integer, ForeignKey("production_types.production_type_id"), primary_key=True) - value = Column(Numeric, nullable=False) - - # defining relationships. - # the defined attributes will reference 'ProductionType' and 'BiddingArea' objects - production_type = relationship(ProductionType, backref=backref("production_plans")) - bidding_area = relationship(BiddingArea, backref=backref("production_plans")) - - def __init__(self, record_created_time, start_time, production_type, bidding_area, value): - self.record_created_time = record_created_time - self.start_time = start_time - self.production_type = production_type # a 'ProductionType' object - self.bidding_area = bidding_area # a 'BiddingArea' object - self.value = value - - def __str__(self): - return ( - f"{self.record_created_time} {self.start_time} " - f"{self.production_type} {self.bidding_area} {self.value}" - ) + __tablename__ = "production_plans" + + record_created_time = Column(DateTime(timezone=False), primary_key=True) + start_time = Column(DateTime(timezone=False), primary_key=True) + bidding_area_id = Column(Integer, ForeignKey("bidding_areas.bidding_area_id"), primary_key=True) + production_type_id = Column(Integer, ForeignKey("production_types.production_type_id"), primary_key=True) + value = Column(Numeric, nullable=False) + + # defining relationships. + # the defined attributes will reference 'ProductionType' and 'BiddingArea' objects + production_type = relationship(ProductionType, backref=backref("production_plans")) + bidding_area = relationship(BiddingArea, backref=backref("production_plans")) + + def __init__(self, record_created_time, start_time, production_type, bidding_area, value): + self.record_created_time = record_created_time + self.start_time = start_time + self.production_type = production_type # a 'ProductionType' object + self.bidding_area = bidding_area # a 'BiddingArea' object + self.value = value + + def __str__(self): + return ( + f"{self.record_created_time} {self.start_time} " + f"{self.production_type} {self.bidding_area} {self.value}" + ) Base.metadata.create_all(engine) # create tables @@ -337,27 +337,27 @@ session.add(bidding_area1) session.add_all( - [ - BiddingArea("NO2", "Elspot NO2"), - BiddingArea("NO3", "Elspot NO3"), - BiddingArea("NO4", "Elspot NO4"), - BiddingArea("NO5", "Elspot NO5"), - ] + [ + BiddingArea("NO2", "Elspot NO2"), + BiddingArea("NO3", "Elspot NO3"), + BiddingArea("NO4", "Elspot NO4"), + BiddingArea("NO5", "Elspot NO5"), + ] ) production_type_B37 = ProductionType("B37", "Thermal unspecified") production_type_B30 = ProductionType("B30", "Wind unspecified") session.add_all( - [ - ProductionType("B19", "Wind Onshore"), - ProductionType("B10", "Hydro-electric pure pumped storage head installation"), - ProductionType("B11", "Hydro Run-of-river head installation"), - ProductionType("B12", "Hydro-electric storage head installation"), - ProductionType("A04", "Generation"), - production_type_B37, - production_type_B30, - ] + [ + ProductionType("B19", "Wind Onshore"), + ProductionType("B10", "Hydro-electric pure pumped storage head installation"), + ProductionType("B11", "Hydro Run-of-river head installation"), + ProductionType("B12", "Hydro-electric storage head installation"), + ProductionType("A04", "Generation"), + production_type_B37, + production_type_B30, + ] ) @@ -371,21 +371,21 @@ # adding some production plans... session.add( - ProductionPlan( - datetime.datetime.now(), - datetime.datetime(2022, 11, 2, 1, 0), - production_type_B37, - bidding_area1, - decimal.Decimal("80.5"), - ) + ProductionPlan( + datetime.datetime.now(), + datetime.datetime(2022, 11, 2, 1, 0), + production_type_B37, + bidding_area1, + decimal.Decimal("80.5"), + ) ) production_plan2 = ProductionPlan( - datetime.datetime.now(), - datetime.datetime(2022, 11, 2, 2, 0), - production_type_B37, - bidding_area1, - decimal.Decimal("90.5"), + datetime.datetime.now(), + datetime.datetime(2022, 11, 2, 2, 0), + production_type_B37, + bidding_area1, + decimal.Decimal("90.5"), ) session.add(production_plan2) @@ -427,17 +427,17 @@ # return production plans with production type 'B37' session.query(ProductionPlan).filter( - ProductionPlan.production_type_id == ProductionType.production_type_id + ProductionPlan.production_type_id == ProductionType.production_type_id ).filter(ProductionType.code == "B37").all() session.query(ProductionPlan).join(ProductionType).filter(ProductionType.code == "B37").all() session.query(ProductionPlan).filter(ProductionPlan.production_type == production_type_B37).all() session.query( - ProductionPlan + ProductionPlan ).from_statement( - text( - "SELECT pp.* FROM production_plans pp, production_types pt " - "WHERE pp.production_type_id = pt.production_type_id AND pt.code=:code" - ) + text( + "SELECT pp.* FROM production_plans pp, production_types pt " + "WHERE pp.production_type_id = pt.production_type_id AND pt.code=:code" + ) ).params(code="B37").all() @@ -449,28 +449,29 @@ - - - - - - - - + + + + + + + + -- cgit v1.3