13  Tutorial - Introduction to Git and GitHub

After completing this tutorial the student should be able to:

13.1 Version Control of Code

Version control is a systematic way to track and manage changes to code over time. Think of it like a time machine for your code that maintains the history of how your code evolved through time. It records and documents who changed what, when and why in your code. If something new that your try does not work as intended, it allows you to revert to the the previous working version of your program. Version control also allows multiple developers to work with the same code base simultaneously of different machines. This is done by each developer initiating a new branch from the common codebase and continue their individual or group development in parallel without affecting the main codebase. Once everyone is finished with their part, the changes can be merged systematically without overwriting each other’s work.

A common problem when version control is not adopted is that the entire code is maintained on a single laptop or a workstation by a single developer. When this hardware fails, the code is also lost unless a backup is maintained in a hard drive or a flash drive. The version control programs help you maintain a copy of the code on remote servers that allows your code to be protected against hardware failures.

Git is the most widely used version control system today, and GitHub is a popular platform that hosts Git repositories and adds collaboration features. Together, they’re essential tools in modern software development, used by individuals and teams from small startups to large enterprises. In this tutorial, you will be learning these concepts hands-on by working with Git and GitHub directly. Let’s get started!

13.2 Creating a GitHub account

On GitHub, your username is your identity. Follow these steps to create your account:

  1. Go to GitHub
  2. Click on “Sign up”
  3. Enter your email, password, and desired username
  4. Follow the verification steps
  5. Choose the free plan

Note: Choose a professional username as it will be visible in your project URLs and to potential employers.

13.3 Installing Git

Before you can use Git, you need to install it on your computer. Follow the instructions for your operating system:

13.3.1 Windows

  1. Download the Git installer from Git for Windows
  2. Run the installer with these recommended settings:
    • Select Components: Keep defaults
    • Choosing default editor: Pick your preferred editor
    • Adjusting PATH: Select “Git from the command line and also from 3rd-party software”
    • HTTPS transport backend: Use the native Windows Secure Channel library
    • Line ending conversions: Checkout Windows-style, commit Unix-style
    • Terminal emulator: Use MinTTY
    • Default branch name: main
  3. Open Windows PowerShell Prompt and verify installation:
git --version

13.3.2 MacOS

  1. Install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Install Git using Homebrew:
brew install git
  1. Verify the installation:
git --version

Alternatively, Git may already be installed on your Mac. Check by opening Terminal and running git --version.

13.3.3 Ubuntu

  1. Update package index:
sudo apt update
  1. Install Git:
sudo apt install git
  1. Verify the installation:
git --version

13.4 Cloning Repository from GitHub Classroom

  1. Accept the GitHub Classroom assignment link provided by your instructor

  2. GitHub will create a private repository for you

  3. Clone your repository

  4. Verify the clone was successful:

git status
  1. Configure your Git identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

13.5 Creating and Committing a New File

  1. Navigate to the ros2_ws/src/student/tut_02 directory and create a new file named test.md:
cd ros2_ws/src/student/tut_02
touch test.md
  1. Add the following content to test.md:
# Test File
This is a test file for the git tutorial.
  1. Add the new file to the repository:
git add test.md
  1. Commit the new file to the repository with a meaningful message:
git commit -m "Add test.md file for git tutorial"
  1. Push the changes to the remote repository:
git push origin main

13.6 Working with Branches and Resolving Conflicts

  1. Create a new branch:
git checkout -b new-feature
  1. Modify ros2_ws/src/student/tut_02/test.md in the new branch:
# Test File
This is a modified test file for the git tutorial.
  1. Add and commit the changes in the new branch:
git add ros2_ws/src/student/tut_02/test.md
git commit -m "Modify test.md in new-feature branch"
  1. Switch back to the main branch:
git checkout main
  1. Modify ros2_ws/src/student/tut_02/test.md in the main branch:
# Test File
This is a different modification in the main branch.
  1. Add and commit the changes in the main branch:
git add ros2_ws/src/student/tut_02/test.md
git commit -m "Modify test.md in main branch"
  1. Merge the new-feature branch into the main branch (this will cause a conflict):
git merge --no-ff new-feature
Check the file `ros2_ws/src/student/tut_02/test.md` and see how git has shown you the conflict.
  1. Resolve the conflict in ros2_ws/src/student/tut_02/test.md by manually editing the file to combine the changes:
# Test File
This is a test file for the git tutorial.
This is a modified test file for the git tutorial.
This is a different modification in the main branch.
  1. After resolving the conflict, add and commit the resolved file:
git add ros2_ws/src/student/tut_02/test.md
git commit -m "Resolve merge conflict in test.md"
  1. Push the changes to the remote repository:
git push origin main

13.7 Implementing Hello World

  1. Navigate to the ros2_ws/src/student/tut_02 directory and open hello.py:
cd ros2_ws/src/student/tut_02
code hello.py
  1. Implement the hello_world() function:
def hello_world():
    # TODO: Return the string "Hello, World!"
    pass
  1. Commit your changes:
git add hello.py
git commit -m "Implement hello_world function"
git push origin main

13.8 Additional Git Functionalities (Optional)

13.8.1 Viewing Commit History

To view the commit history, use:

git log

13.8.2 Reverting Changes

To undo the last commit while keeping the changes in your working directory:

git reset --soft HEAD~1

To completely undo the last commit and discard changes:

git reset --hard HEAD~1

To revert a specific commit while keeping history:

git revert <commit-hash>

To undo changes in a specific file before committing:

git checkout -- <filename>

13.8.3 Stashing Changes

To temporarily save changes without committing:

git stash save "work in progress"

To list stashed changes:

git stash list

To apply the most recent stash:

git stash pop

To apply a specific stash:

git stash apply stash@{n}

To drop a specific stash:

git stash drop stash@{n}