Ubuntu Linux for Robotics · Lesson 21 of 96
Git and GitHub
Repositories, commits, branches, remotes, .gitignore and the everyday workflow.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain git and github in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Git records the history of your project as commits. The everyday cycle is: change files, git add the ones you want, git commit -m 'message', and git push to GitHub. git status shows what changed, git diff shows how, git log shows history, and git branch or git switch -c makes a branch for a new feature.
Set your name and email once, use SSH keys with GitHub, and add a .gitignore so generated folders (build/, install/, log/) never enter the repository. Write commit messages that say why. Commit small, working steps and never rewrite history that others have pulled.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the git and github commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
git config --global user.name "Your Name"
# Set the name recorded in your commits.
git config --global user.email "you@example.com"
# Set your email.
git init robot_ws/src/robot_nodes
# Turn a folder into a repository.
git add . && git commit -m "Add talker node"
# Stage everything and record a commit with a message.
git switch -c feature/listener
# Create and switch to a branch.
git remote add origin git@github.com:you/robot_nodes.git
# Connect to the GitHub repository.
git push -u origin main
# Upload the branch and remember it for later pushes.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · Build and run
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Git and GitHub. Expected result: 1
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
d=$(mktemp -d) && cd "$d"
git init -q
git config user.name "Tester"
git config user.email "tester@example.com"
echo "robot" > README.md
# TODO: stage all files and commit with the message "First commit"
git log --oneline | wc -lExpected output
1Stuck? Show the full solution (try it yourself first)
d=$(mktemp -d) && cd "$d"
git init -q
git config user.name "Tester"
git config user.email "tester@example.com"
echo "robot" > README.md
git add .
git commit -q -m "First commit"
git log --oneline | wc -lExpected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Create a repository for a ROS package with a .gitignore, make three commits, create a branch, merge it back, and push to a GitHub repository over SSH. Show the history with git log --oneline --graph.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.