1.3 Save ROS code on GitHub
Last updated
Last updated
In this tutorial, we will create a ROS 2 workspace and store the project in a repository on GitHub.
ROS 2 has been designed with different layers. For users, the workspace is a folder that is the central location for organizing and developing your robot project. It contains all the code, data files, and configuration scripts.
Open a terminal, and type the first two lines of command to create your workspace. The src folder inside the workspace is where all your code will go for your robot.
We don’t need any code in our workspace now, just run the command below to build the workspace.
This colcon build command needs to be run in the root of your workspace whenever you make additions or changes to the code. Type following command after colcon build:
You will see three folders that are automatically created when you run the colcon build:
build: This is where ROS 2 temporarily stores files while it’s compiling.
install: The finished, ready-to-use parts of your robot’s software are placed.
log: Keeps track of what happened when you built your code.
We can run the following command to make sure this workspace will be found any time we open a new terminal window.
It's always a good idea to keep code backed up and version controlled. Using Git with an online service like Github is an easy way. Git is also a useful tool for multiple developers working on a project, or developing on multiple machines. Here I assume you already got a GitHub account.
The first thing you need to do is install Git. Open a new terminal window, and type:
Check the git version you have.
Configure your git username and email.
Give the package a name (usually same as in ROS), choose to make it public or private, and then we skip the "Initialize" options and hit "Create Repository".
Now we need to push our local content to this repo. Open a terminal in your ros project directory:
We can run the following commands with our username
and repository
name substituted. If you've never used Git on this computer before, you may also need to set a username and email address (it will tell you how).
These commands:
Initialize git for the directory
Stage the whole directory (ready to be committed)
Create a commit with a message
Create a main
branch
Set the URL link for the cloud repo to push to, and give it the name origin
Push the main
branch to the origin
repo
Origin refers to your GitHub repository, and main is the name of your branch. The terminal will respond to let us know it has been pushed.
Now if you go back to GitHub, you can see your repository.
When you make changes to your code on your computer, and want to get these code changes saved to GitHub, here is how you do it:
You’ll repeat these three steps each time you want to update your code on GitHub.
And if you’re working with other people, you should always download their changes first using git fetch, git status, and then git pull (one command right after the other) before you start working.
git fetch checks GitHub to see what changes exist – it’s like looking at a list of updates available but not downloading them yet. It tells you what’s different between your code and GitHub’s code.
git status shows you exactly what’s different between your local code and the code on GitHub after you’ve fetched. It’s like comparing your version with GitHub’s version to see what’s changed.
git pull actually downloads those changes from GitHub to your computer, bringing your local code up to date. It’s like clicking “download” on those updates you found with fetch and status.
These three commands help you safely check for and get any updates before you start working, which helps avoid conflicts with other people’s code changes.
We'll use SSH to authenticate. So if you haven't got that set up already, .
Go back to the main page of your repository on GitHub. Copy the SSH URL of the repo.
and log in. Click on the “+” icon in the upper right corner and select “New repository.”