This is a practical workshop consisting of common Git Advanced & GitHub-related actions.
It is based on the unikraft/catalog-core repository, giving us a concrete Git repository to screw up ... hmmmm ... to do wonderful amazing great things to.
First of all, clone the repository:
git clone https://github.com/rosedu/workshop-github
cd workshop-github/And let's get going! 🚀
Let's set up GitHub for proper use.
If you haven't already, add your public SSH key to your GitHub account. Follow the instructions here.
Create a personal access token to use as an authentication mechanism for GitHub. Follow the instructions here. Add all permissions to the personal access token.
Follow the instruction here and install GitHub CLI.
Authenticate to GitHub:
gh auth loginUse the username and the personal access token above to authenticate.
Let's first create a work GitHub repository based on the current repository. We will use it for toying around, messing it up and fixing it.
First, make sure you are in the local directory clone of this repository (workshop-github).
Then, create a repository on GitHub from the command line (using GitHub CLI - gh):
./gh-create-repo.shCheck your repository on GitHub using a web browser.
Your repository is now available as the upstream remote.
Check your remotes:
git remote show
git remote show origin
git remote show upstreamNote
If, at any point in time, you miss a command, or something bad simply happened, reset the environment by running:
./reset-all.shImportant
We recommend you write all commands below by hand, i.e. without using copy & paste. This will get you better accustomed to Git and Git commands.
Let's get to a situation where we need to repair the commit history. We will have a setup where we have the following stack of commits:
- (top) correct commit
- (next) commit that shouldn't exist (
bla blacommit) - (next-next) commit with a typo (
Bueinstead ofBye)
We want to edit the commit history and:
- Remove the
bla blacommit. - Fix the typo.
First, let's create the local branches we will work on (base, scripts, test).
They live on the remote, so we just check them out once to get local tracking branches:
git checkout base
git checkout scripts
git checkout test
git checkout main-
Create the setup:
./set-up-history-edit.sh git status git log
-
Note: If, at any point in time, you miss a command, or something bad simply happened, reset the environment by running:
./reset-all.shThen go back to step 1 and prepare the messed up environment again.
-
Go into commit history editing mode:
git rebase -i HEAD~3The
rebasecommand positions you somewhere else in the commit history. You can make updates to commits from that point onward.The
~Nconstruct is a reference toNcommits before the current one.HEAD~3means 3 commits before the top commit.You get an editor screen with an output like this:
pick e5442f0 Add C Bue application pick 06eb1fa bla bla pick 74f3d3e c-bye: Add Firecracker build script for x86_64 -
Edit the rebase screen contents in order to edit the commit with the typo (
Bueinstead ofBye) and to drop the extra commit (the one withbla bla). Have the editor screen have the contents:edit e5442f0 Add C Bue application drop 06eb1fa bla bla pick 74f3d3e c-bye: Add Firecracker build script for x86_64That is, the first line (the bad commit message) should have
editinstead ofpick- we edit the commit. And the second line (the extra commit) should havedropinstead ofpick- we drop the commit.Save and exit the editor screen.
-
We are currently editing the typo commit:
git log git show
-
Update the commit message from
BuetoBye:git log git commit --amend # do edit as required git log git show
-
Continue the commit history editing:
git rebase --continueEach
git rebase --continuecommand gets you to the next commit to update. -
The extra commit has been dropped:
git log git status
-
The commit history editing (aka the rebase) is done:
git rebase --continueIt says "No rebase in progress?", meaning the rebase is done. There are no more commits to update.
-
Repeat the above steps at least 2 more times.
Aim to have one time without checking the instructions. That is, run the
./set-up-history-edit.shscript and then repair the commit history by yourself.If, at any point, you get lost, run the reset script:
./reset-all.sh -
Do your own commit history that you want to edit. Go to a given branch, create commits, create some bad or extra commits. Then repair the commit history.
If, at any point, you get lost, run the reset script:
./reset-all.sh
We will get some new content that we will add as commits to the repository.
Make sure you are on the main branch and everything is clean:
git checkout mainOr, reset the repository:
./reset-all.sh-
Create contents from archive:
unzip support/c-bye.zip git status
We now have a
c-bye/directory:ls c-bye/There are a lot of files. We want to add them as 3 separate commits in 3 separate branches.
- The
bye.c,Makefile,Makefile.uk,fc...,xen...,README.mdfiles will go to thebasebranch. - The
defconfig...,build...,run...,README.scripts.mdfiles will go to thescriptsbranch. - The
test...files will go to thetestbranch.
- The
-
Go to the
c-bye/directory:cd c-bye/ ls
Let's create a commit to base branch:
-
Check out the
basebranch:git checkout base -
Add contents to staged area:
git add bye.c Makefile Makefile.uk README.md xen.* fc.* .gitignore git status
-
Create the commit:
git commit -s -m 'Introduce C Bye on Unikraft' -
List the commit:
git log git show
-
Look at the commit for the initial C Hello program:
git show 7fd6e9290dddc2ae799ae5df684668a7d16e87f3The commit message is:
Introduce C Hello on Unikraft Add `c-hello/` directory: - `hello.c`: the source code file - `Makefile.uk`: defining the source code files used - `Makefile`: for building the application - `fc.x86_64.json` / `fc.arm64.json`: Firecracker configuration - `xen.x86_64.cfg` / `xen.arm64.cfg`: Xen configuration - `README.md`: instructions - `.gitignore`: ignore generated filesWe want something similar for our C bye commit.
-
Update the commit message by amending:
git commit --amendYou get an editable screen. Edit the commit message to have contents similar to the one for C Hello. Use copy & paste.
You can use
git commit --amendto constantly update the commit.See the final result with:
git log git show
Let's create a commit to scripts branch:
-
Check out the
scriptsbranch:git checkout scripts -
Add contents to staged area:
git add defconfig.* build.* run.* README.scripts.md git status
-
Create the commit:
git commit -s -m 'c-bye: Add scripts' -
List the commit:
git log git show
-
Look at the commit for the initial C Hello program:
git show 04cf0f57f2853d73a5c25082d4652fef63da8f57The commit message is:
c-hello: Add scripts Use scripts as quick actions for building and running C Hello on Unikraft. - `defconfig.<plat>.<arch>`: default configs, used by build scripts - `build.<plat>.<arch>`: scripts for building Unikraft images - `run.<plat>.<arch>`: scripts for running Unikraft images - `README.script.md`: companion README with instructionsWe want something similar for our C bye commit.
-
Update the commit message by amending:
git commit --amendYou get an editable screen. Edit the commit message to have contents similar to the one for C Hello. Use copy & paste.
You can use
git commit --amendto constantly update the commit.See the final result with:
git log git show
Let's create a commit to test branch:
-
Check out the
testbranch:git checkout test -
Add contents to staged area:
git add test* git status
-
Create the commit:
git commit -s -m 'c-bye: Add test scripts' -
List the commit:
git log git show
-
Reset the configuration:
./reset-all.sh -
Repeat the above steps at least 2 more times.
Aim to have one time without checking the instructions. That is, create the 3 commits in the 3 branches for C bye.
If, at any point, you get lost, run the reset script:
./reset-all.sh -
Do the same for the C++ Bye program.
Make sure you are on the
mainbranch:git status git checkout main
First unpack the contents:
unzip support/cpp-bye.zip git status
We now have a
cpp-bye/directory:ls cpp-bye/There are a lot of files. We want to add them as 3 separate commits in 3 separate branches.
- The
bye.cpp,Makefile,Makefile.uk,Config.uk,fc...,xen...,README.mdfiles will go to thebasebranch. - The
defconfig...,build...,run...,README.scripts.mdfiles will go to thescriptsbranch. - The
test...files will go to thetestbranch.
Follow the steps for C Bye to create the commits for C++ Bye.
- The
-
Do the same for the Python Bye program.
Make sure you are on the
mainbranch:git status git checkout main
First unpack the contents:
unzip support/python3-bye.zip git status
We now have a
python3-bye/directory:ls python3-bye/There are a lot of files. We want to add them as 3 separate commits in 3 separate branches.
- The
bye.py,Makefile,Makefile.uk,Config.uk,fc...,xen...,README.mdfiles will go to thebasebranch. - The
defconfig...,build...,run...,README.scripts.mdfiles will go to thescriptsbranch. - The
test...files will go to thetestbranch.
Follow the steps for C bye to create the commits for Python3 Bye.
- The
At this point there are commits in the base branch that are not part of the scripts branch.
And there are commits in the scripts branch that are not part of the test branch.
We aim to have the scripts branch built on top of the base branch.
And we want to have the test branch built on top of the scripts branch.
For this, all commits from the base branch will have to be on the scripts branch.
And all commits from the scripts branch will have to be on the test branch.
-
To get the
c-byecommit from thebasebranch to thescriptsbranch, first check out thescriptsbranch:git checkout scripts -
Find out the commit ID in the
basebranch:git log baseScroll the commit history and copy the commit ID belonging to the
c-byeprogram. That is, the ID of the commit you created earlier with the message:Introduce C Bye on Unikraft. -
Use the commit ID cherry pick the commit from the
basebranch to thescriptsbranch:git cherry-pick <commit-id>Replace
<commit-id>with the commit ID you copied above (copy & paste). -
Check the updated history of the
scriptsbranch:git log -
To get the
c-byecommits from thescriptsbranch to thetestbranch, first check out thetestbranch:git checkout test -
First cherry pick the
basecommit that is now onscripts:git cherry-pick <commit-id>This is the same commit ID from above.
Cherry-picking is selecting a commit, or series of commits and adding them on top of the current setup.
-
Now let's get the
c-byecommit from thescriptsbranch. Find out the commit ID in thescriptsbranch:git log scriptsScroll the commit history and copy the commit ID belonging to the
c-byeprogram. That is, the ID of the commit you created earlier with the message:c-bye: Add scripts. -
Use the commit ID cherry pick the commit from the
scriptsbranch to thetestbranch:git cherry-pick <new-commit-id>Replace
<new-commit-id>with the commit ID you copied above (copy & paste). -
Check the updated history of the
scriptsbranch:git log
Note
If, at any point, you did something wrong, recall that you can drop the top commit by doing:
git reset --hard HEAD^-
Repeat the above steps at least 2 more times.
Aim to have one time without checking the instructions. That is, have the
scriptsbased on thebasebranch, and have thetestbranch based on thescriptsbranch.For starters, drop the newly cherry-picked commit from the
scriptsbranch:git checkout scripts git reset --hard HEAD^
And drop the newly cherry-picked commits from the
testbranch:git checkout test git reset --hard HEAD^^
Now repeat the steps above.
-
Do the same steps for the C++ Bye program. That is, have the
scriptsbased on thebasebranch, and have thetestbranch based on thescriptsbranch. -
Do the same steps for the Python Bye program. That is, have the
scriptsbased on thebasebranch, and have thetestbranch based on thescriptsbranch.
At this point, the scripts branch is based on the base branch.
And the test branch is based on the scripts branch.
What we do not like, however, is that the commits in the scripts and the test branch are not in the correct order.
In the scripts branch the commits are (top-to-bottom):
- python3-bye: Add scripts
- Introduce Python3 Bye
- cpp-bye: Add scripts
- Introduce C++ Bye
- c-bye: Add scripts
- Introduce C Bye
- python3-bye: Add test scripts
- cpp-bye: Add test scripts
- c-bye: Add test scripts
Use git log to confirm this:
git log
git log --onelineThe order we want is (top-to-bottom):
- python3-bye: Add test scripts
- python3-bye: Add scripts
- Introduce Python3 Bye
- cpp-bye: Add test scripts
- cpp-bye: Add scripts
- Introduce C++ Bye
- c-bye: Add scripts
- Introduce C Bye
- c-bye: Add test scripts
So, to update the commit history, follow the steps below:
-
Enter the history update mode. Update the last
9commits:git rebase -i HEAD~9You are now in a custom editor mode where you can update the commits.
-
Move the commits (cut & paste) to get to the new commit history. Do this by cutting and pasting the lines in the commit history.
-
Save the custom editor mode.
You're done. Check the new commit history with:
git log
git log --onelineEdit the commit history on the test branch so the commits are in the correct order, just like they are on the scripts branch.
Note
If, at any point in time, you miss a command, or something bad simply happened, reset the environment by running:
./reset-all.sh --githubImportant
We recommend you write all commands below by hand, i.e. without using copy & paste. This will get you better accustomed to GitHub-related commands.
Take a look at the following repositories:
Do a tour of as much information as possible about them:
- See statistics about the programming languages used.
- See information about contributors.
- See number of stars and number of forks.
- Take a quick look at the issues and pull requests.
Take a look at the GitHub projects for the unikraft organization.
Browse the projects.
Look at the Release - Next GitHub project.
Browse the items in the list.
Browse different views (tabs) in the projects.
Take a look at pull requests in the unikraft repository.
Select 3 pull requests and check them out.
Look at the discussions, changes, checks for each pull request.
Identify a pull request that is connected to an issue.
Select pull requests that have been authored by michpappas.
Select pull requests that are to be reviewed by michpappas.
Select pull requests that use the area/plat label.
Let's now create pull requests on our GitHub repository. A pull request is created from a series of commits in a branch.
We do the steps:
-
Create a branch for the new pull request:
git checkout -b add-c-bye -
Create the contents of the
c-byeprogram:unzip support/c-bye.zip -
Create commit:
git add c-bye/ git commit -s -m 'Introduce c-bye program'
-
Push commit to the
upstreamremote:git push upstream add-c-bye -
Create a pull request by clicking on the URL that was printed by the command above. You will end up having a pull request created in the repository. The pull request is requesting for a merge to happen from the
add-c-byetomain.
-
Repeat the above steps at least 2 more times. Reset before each step:
./reset-all.sh --github -
Do the same steps as above for the
cpp-byeandpython3-byeprograms in thesupport/directory. You should end up with three pull requests.
The pull requests should be reviewed and merged. For that, in the GitHub web interface for the pull request follow the steps:
-
Go to the
Files changedtab. -
Click the
Review changesbutton. -
Aim to approve your pull request. You cannot approve your pull request, if you're the author.
-
Now, get back to the
Conversationtab. Go below to theMerge pull requestbutton.Below clicking the button, see the options from the little drop-down option on the right. See what are the options, choose one and do it.
-
Do the steps above for all 3 pull requests.
-
Now reset the repository:
./reset-all.sh --githuband redo the pull requests, and merge them again.
-
Create your own commits and pull requests. Be creative.
Create at least one pull request with two commits. Use the
Squash and mergemerge strategy.
In order to approve a pull request, you need to have another user able to approve your pull requests.
Before everything, reset the repository:
./reset-all.sh --githubAnd create the pull request, as above.
To add someone to be able to approve your pull requests, they need to have Triage permissions.
For this, do the following:
-
Go to the
Settingstab in the GitHub web view of your repository. -
Go to the
Collaboratorsentry in the left menu. You'll have to provide your GitHub password, or use some other authentication method. -
Ask someone around you for their GitHub username. Add them to the repository as collaborator.
-
Ask them to confirm the invite via e-mail or by accessing the invitation URL:
https://github.com/<your-github-username>/workshop-github/invitations. Replace<your-github-username>with your GitHub username. -
Ask them to approve your pull request.
-
Now merge the pull request.
We want to enforce an approval for our pull requests.
Before everything, reset the repository:
./reset-all.sh --githubAnd create the pull request, as above.
Now add a ruleset to add a required approval condition. For this, do the following:
-
Go to the
Settingstab in the GitHub web view of your repository. -
Go to the
Branchesentry in the left menu. -
Click
Add branch ruleset. -
Check
Require a pull request before merging. -
Add
1toRequired approvals. -
Now ask for an approval for your pull request, as above.
-
Merge the pull request with the approval now done.
We want to configure Rebase and merge as the only merge strategy.
For that, do the steps:
-
Go in the
Settingstab in web view of your GitHub repository. -
Go to the
Pull Requestssession. -
Uncheck
Allow mergecommits andAllow squash merging.
Now create a new pull request and see that the only option for merging is Rebase and merge.
A tag marks a specific commit in history, usually to flag a release or version (for example v1.0).
Unlike a branch, a tag does not move - it always points to the same commit.
-
Create an annotated tag on the current commit:
git tag -a v1.0 -m "First release of the C Bye application" -
List the tags and inspect one:
git tag git show v1.0
-
Push the tag to GitHub (tags are not pushed by
git pushby default):git push origin v1.0To push all your tags at once, use
git push origin --tags. -
In the web view of your GitHub repository, go to the
Tags/Releasessection. You will see thev1.0tag. From there you can also turn a tag into a publishedRelease.
To delete a tag (locally and on GitHub), use:
git tag -d v1.0
git push origin --delete v1.0GitHub shines for collaborative / team work. For this, work in pairs of two.
Before this, do a reset of your repository:
./reset-all.sh --github
Each of you should do the following:
-
Create a fork of the other's repository. Be sure to give it a different name, not to clash with your own
workshop-githubrepository name. -
Clone the fork locally:
git clone <fork_url> cd <clone_directory>
where
<fork_url>is the URL of the other's repository, and<clone_directory>is the directory of the repository clone. -
Create a branch and commit(s) from
c-bye. -
Push the branch to the
originremote (belonging to your fork). -
Create a pull request to the other's repository (from fork to the initial repository).
-
The other person should approve and merge your pull request.
Do this multiple times, be creative, use your own ideas.
Now, let's get really creative.
Continue working in pairs of two.
Each of you should create their own repository, with whatever content they want.
Create it from scratch, be creative, do whatever you want.
Configure the repository to use the Rebase and merge strategy.
Ask the other to fork your repository and then ask the other to create a pull request. Toy around.
Before asking for a pull request, create an issue on your repository, and ask the other to "solve" the issue by creating a corresponding pull request. Link the pull request to the issue once it is created, by following instructions here.