/workshop-git

Hands-on Git workshop

Primary LanguageShell

Git Workshop

This is a practical workshop consisting of common Git-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-git
cd workshop-git/

And let's get going! 🚀

Inspect Repository

  1. See the current branch and status:

    git status
  2. See local branches:

    git branch
  3. See local and remote branches:

    git branch -a
  4. Check out remote branches locally:

    git branch base origin/base
    git branch scripts origin/scripts
  5. Show verbose information about branches:

    git branch -vv
    git branch -vv -a
  6. Check out to a local branch:

    git checkout base
    ls
    git checkout scripts
    ls
    git checkout main
  7. List contents of another branch without checking out:

    git ls-tree scripts
    git ls-tree scripts:c-fs
    git ls-tree scripts:c-fs/rootfs
    git ls-tree scripts --name-only
    git ls-tree scripts:c-fs --name-only
    git ls-tree scripts:c-fs/rootfs --name-only
    git ls-tree -r scripts --name-only

    The construct scripts:c-fs means the c-fs filesystem entry in the scripts branch. Similarly, the construct scripts:c-fs/rootfs means the c-fs/rootfs filesystem entry in the scripts branch.

  8. Show contents of file on another branch without checking out:

    git show scripts:c-fs/build.qemu.x86_64
    git show scripts:c-fs/README.scripts.md

Do It Yourself.

  1. Do more listing. Try out the other commands here.

  2. Check out the test branch from the remote origin/test branch. List contents, check out the branch, get back to the main branch.

Inspect Commit History

  1. Make sure you are on the main branch:

    git checkout main
    git status
  2. List commit history:

    git log
    git log base
    git log --oneline base
    git log --pretty=fuller
  3. Show top commit contents:

    git show
    git show HEAD
  4. Show commit contents by commit ID:

    git show 1a02ae3
  5. Only show actual contents, no metadata:

    git show --pretty="" 1a02ae3
  6. Only show modified files:

    git show --pretty="" --name-only 1a02ae3
  7. Show commits that modified a file:

    git checkout base
    git blame README.md
    git checkout main
  8. Show commits that modified a path:

    git log scripts -- README.md
    git log scripts -- c-fs
  9. Show difference between two commits / references:

    git diff base scripts
  10. Show commit difference between two references:

    git cherry -v main scripts
    git cherry -v scripts main
    git cherry -v main d379011
    git cherry -v d379011 main

Do It Yourself

  1. How are the three branches (base, scripts, test) constructed? Which is constructed on top of the other?

  2. Do more history inspection.

Configure Git

  1. If not already configured, configure your name and e-mail:

    git config --global user.name "<your-full-name-here>"
    git config --global user.email "<your-email-here>"

    Use your full name as you would typically do: Firstname Lastname.

  2. Configure colored output:

    git config --global color.ui "auto"
  3. Configure aliases:

    git config --global alias.lg 'log --pretty=fuller'
    git lg
  4. Check the configuration:

    cat .git/config
    cat ~/.gitconfig

Clean Up / Reset Your Git Environment

We make mistakes:

  • We create commits we shouldn't have (yet) created.
  • We leave changes out of commits.
  • We amend the wrong commit.
  • We do merges / rebases / cherry-picks that fail.
  • We add a change in the staging area that we shouldn't have (yet) added.
  • We delete a file by mistake.

These things happen. We solve them. We just need to know how to do that.

  1. First prepare a messed up environment, by running:

    ./mess-it-up.sh
  2. Now look at the mess:

    git status
  3. See the commit history:

    git log

    See the bla bla commit (latest). And see the wrong message (Bue instead of Bye) for the other other commit.

  4. Note: If, at any point in time, you miss a command, or something bad simply happened, reset the environment by running:

    ./reset-all.sh

    The go back to step 1 and prepare the messed up environment again.

  5. Let's first get rid of all the temporary files we do not require (they were added by mistake):

    git clean -d -f
    git status
  6. Restore the deleted Makefile:

    git status
    git restore c-hello/Makefile
    git status
  7. Restore the staged deleted hello.c:

    git status
    git restore --staged c-hello/hello.c
    git status
    git restore c-hello/hello.c
    git status
  8. Restore the staged new file c-bye/build.fc.x86_64 to create a proper commit:

    git status
    git restore c-bye/build.fc.x86_64
    git status
  9. See the commit history again:

    git log
  10. Remove the bla bla commit:

    git reset HEAD^
    git status
    git log
    rm blabla.txt
  11. Update the commit message from Bue to Bye:

    git log
    git commit --amend    # do edit as required
    git log
    git show
  12. Create a new commit with the c-bye/build.fc.x86_64 file:

    git add c-bye/build.fc.x86_64
    git commit -s -m 'c-bye: Add Firecracker build script for x86_64'
    git log
    git show
    git status

Do It Yourself

  1. Repeat the above steps at least 2 more times.

    Aim to have one time without checking the instructions. That is, run the ./mess-it-up.sh script and then repair the mess by yourself.

    If, at any point, you get lost, run the reset script:

    ./reset-all.sh
  2. Do your own messing up of the environment. Go to a given branch, remove files, create new files, create commits, add files to staging area etc. Then repair the environment.

    If, at any point, you get lost, run the reset script:

    ./reset-all.sh

Edit Commit History

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 bla commit)
  • (next-next) commit with a typo (Bue instead of Bye)

We want to edit the commit history and:

  • Remove the bla bla commit.
  • Fix the typo.
  1. Create the setup:

    ./set-up-history-edit.sh
    git status
    git log
  2. Note: If, at any point in time, you miss a command, or something bad simply happened, reset the environment by running:

    ./reset-all.sh

    The go back to step 1 and prepare the setup for commit history editing.

  3. Go into commit history editing mode:

    git rebase -i HEAD~3

    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
    
  4. Edit the rebase screen contents in order to edit the commit with the typo (Bue instead of Bye) and to drop the extra commit (the one with bla 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_64
    

    That is, the first line (the bad commit message) should have edit instead of pick - we edit the commit. And the second line (the extra commit) should have drop instead of pick - we drop the commit.

    Save the editor screen.

  5. We are currently editing the typo commit:

    git log
    git show
  6. Update the commit message from Bue to Bye:

    git log
    git commit --amend    # do edit as required
    git log
    git show
  7. Continue the commit history editing:

    git rebase --continue
  8. The extra commit has been dropped:

    git log
    git status
  9. The commit history editing (aka the rebase) is done:

    git rebase --continue

    It says "No rebase in progress?", meaning the rebase is done.

Do It Yourself

  1. 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.sh script and then repair the commit history by yourself.

    If, at any point, you get lost, run the reset script:

    ./reset-all.sh
  2. 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

Create Commits

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 main

Or, reset the repository:

./reset-all.sh
  1. 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.

    1. The bye.c, Makefile, Makefile.uk, fc..., xen..., README.md files will go to the base branch.
    2. The defconfig..., build..., run..., README.scripts.md files will go to the scripts branch.
    3. The test... files will go to the test branch.
  2. Go to the c-bye/ directory:

    cd c-bye/
    ls

base branch

Let's create commit to base branch:

  1. Check out the base branch:

    git checkout base
  2. Add contents to staged area:

    git add bye.c Makefile Makefile.uk README.md xen.* fc.* .gitignore
    git status
  3. Create the commit:

    git commit -s -m 'Introduce C Bye on Unikraft'
  4. List the commit:

    git log
    git show
  5. Look at the commit for the initial C Hello program:

    git show 7fd6e9290dddc2ae799ae5df684668a7d16e87f3

    The 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 files
    

    We want something similar for our C bye commit.

  6. Update the commit message by amending:

    git commit --amend

    You 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 --amend to constantly update the commit.

    See the final result with:

    git log
    git show

scripts branch

Let's create commit to scripts branch:

  1. Check out the scripts branch:

    git checkout scripts
  2. Add contents to staged area:

    git add defconfig.* build.* run.* README.scripts.md
    git status
  3. Create the commit:

    git commit -s -m 'c-bye: Add scripts'
  4. List the commit:

    git log
    git show
  5. Look at the commit for the initial C Hello program:

    git show 04cf0f57f2853d73a5c25082d4652fef63da8f57

    The 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 instructions
    

    We want something similar for our C bye commit.

  6. Update the commit message by amending:

    git commit --amend

    You 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 --amend to constantly update the commit.

    See the final result with:

    git log
    git show

test branch

Let's create commit to test branch:

  1. Check out the test branch:

    git checkout test
  2. Add contents to staged area:

    git add test*
    git status
  3. Create the commit:

    git commit -s -m 'c-bye: Add test scripts'
  4. List the commit:

    git log
    git show

Do It Yourself

  1. Reset the configuration:

    ./reset-all.sh
  2. 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
  3. Do the same for the C++ Bye program.

    Make sure you are on the main branch:

    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.

    1. The bye.cpp, Makefile, Makefile.uk, Config.uk, fc..., xen..., README.md files will go to the base branch.
    2. The defconfig..., build..., run..., README.scripts.md files will go to the scripts branch.
    3. The test... files will go to the test branch.

    Follow the steps for C Bye to create the commits for C++ Bye.

  4. Do the same for the Python Bye program.

    Make sure you are on the main branch:

    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.

    1. The bye.py, Makefile, Makefile.uk, Config.uk, fc..., xen..., README.md files will go to the base branch.
    2. The defconfig..., build..., run..., README.scripts.md files will go to the scripts branch.
    3. The test... files will go to the test branch.

    Follow the steps for C bye to create the commits for Python3 Bye.

Create Commit History

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.

  1. To get the c-bye commit from the base branch to the scripts branch, first check out the scripts branch:

    git checkout scripts
  2. Find out the commit ID in the base branch:

    git log base

    Scroll the commit history and copy the commit ID belonging to the c-bye program. That is, the ID of the commit you created earlier with the message: Introduce C Bye on Unikraft.

  3. Use the commit ID cherry pick the commit from the base branch to the scripts branch:

    git cherry-pick <commit-id>

    Replace <commit-id> with the commit ID you copied above (copy & paste).

  4. Check the updated history of the scripts branch:

    git log
  5. To get the c-bye commits from the scripts branch to the test branch, first check out the test branch:

    git checkout test
  6. First chery pick the the base commit that is now on scripts:

    git cherry-pick <commit-id>

    This is the same commit ID from above.

  7. Now let's get the c-bye commit from the scripts branch. Find out the commit ID in the scripts branch:

    git log scripts

    Scroll the commit history and copy the commit ID belonging to the c-bye program. That is, the ID of the commit you created earlier with the message: c-hello: Add scripts.

  8. Use the commit ID cherry pick the commit from the scripts branch to the test branch:

    git cherry-pick <new-commit-id>

    Replace <new-commit-id> with the commit ID you copied above (copy & paste).

  9. Check the updated history of the scripts branch:

    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^

Do It Yourself

  1. Repeat the above steps at least 2 more times.

    Aim to have one time without checking the instructions. That is, have the scripts based on the base branch, and have the test branch based on the scripts branch.

    For starters, drop the newly cherry-picked commit from the scripts branch:

    git checkout scripts
    git reset --hard HEAD^

    And drop the newly cherry-picked commits from the test branch:

    git checkout test
    git reset --hard HEAD^^

    Now repeat the steps above.

  2. Do the same steps for the C++ Bye program. That is, have the scripts based on the base branch, and have the test branch based on the scripts branch.

  3. Do the same steps for the Python Bye program. That is, have the scripts based on the base branch, and have the test branch based on the scripts branch.

Update Commit History

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 --oneline

The 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