Competence group (Faggruppe) Python
Simeon Simeonov - TDE
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.
We start be configuring Git for our needs
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'
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.
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
Each file in your working directory can be in one of two states:
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
*~
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)
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.
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
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
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 branch testing # creates a new branch from 'master' called 'testing'
git checkout testing # makes 'testing' testing the current (active) branch
# git checkout -b testing # creates a new branch named 'testing' and makes it active
echo "Extra content" >> README.md
git commit -a -m "Improve the documentation" # creates a new commit on the 'testing' branch
git checkout master # "switches" to 'master'
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
In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase
There are three possible outcomes when attempting to merge two different branches:
With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.
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'
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!
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.
The two most popular protocols (schema) for interacting with remotes are ssh and http(s) (usually read-only)
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
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.
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
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.
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".
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"
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"
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
https://en.wikipedia.org - Wikipedia
https://git-scm.com/book/en/v2/ - Pro Git
The official man pages