Git on Raspberry Pi: Your Ultimate Guide to Version Control
Git is a great version control system that can help you with your coding projects, whether in a team or working solo. It keeps a history of all changes, avoids conflicts when working in a team and can serve as a reliable backup for your code. Want to give it a try? Let me tell you everything you need to know.
Git can be easily installed using a single apt-get command in the official repositories. Using Git is relatively straightforward and beginner-friendly if you understand a few basic concepts.
Whether you plan to collaborate with your team or want a reliable backup for your solo project, this tutorial will guide you through the entire process. I will cover everything from installation to basic and intermediate functionalities, as well as some advanced features of Git.
If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!
Start Here: Introduction to Git
To put it simply, Git is like a time machine. It helps you keep track of changes you have made to your code, documents, or any other digital content. If anything goes wrong, you can easily revert to an earlier version.
How does Git work?
Git is a software with a command line interface developed by Linus Torvalds (creator of Linux). It has several commands that help you manage changes made to your code. In Git terminology, each change is referred to as a commit.
Besides serving as a time machine for your digital work, Git also helps you collaborate with your team. Each member can work on their local copy of the code and then push their commits to a shared user space. Git will then intelligently figure out who made what changes and what the final shape of the code should be.
Check this: Need a clear GPIO pinout diagram? Here's mine.
One key point is that Git is not a file storage service like Google Drive. While services like GitHub provide a platform to share your projects with your team, Git works completely off your local storage.
Git vs GitHub vs GitLab
When talking about Git, it is essential to understand the difference between Git, GitHub, and GitLab. These are three different things and are used for various purposes:
- Git is the underlying tool or system.
It’s an open-source version control system that lets you track changes to your files, collaborate with others, and go back to previous versions of your work. Git runs locally on your computer and works entirely offline unless you decide to share your work with others. - GitHub is a platform built on top of Git.
It provides an online space to store, share, and collaborate on Git repositories. It also offers additional features like issue tracking, project management tools, and integration with other services. GitHub is widely popular and primarily used to host open-source projects, although it’s also used for private projects. - GitLab is similar to GitHub, but it has some unique features.
It’s another platform for hosting and managing Git repositories, but it emphasizes DevOps, allowing teams to manage the entire software development lifecycle in one place. GitLab is often chosen by organizations that want a self-hosted option, as it provides a robust free version that can run on their servers.
In a nutshell, Git is the core tool used for version control, while GitHub and GitLab are platforms that provide additional collaboration and management tools.
This guide will primarily focus on Git and use GitHub to show online collaboration options. However, if you are looking for a more local approach to using GitLab, you can follow these in-depth tutorials on how to set up your own GitLab.
Related Tutorials:
- How to install GitLab on your Raspberry Pi?
- How To Install & Configure GitLab Server on Ubuntu?
- SVN Server on Raspberry Pi: A Complete Installation Guide
Setting Up Git on your Raspberry Pi
Installing Git is very straightforward from official repositories using an apt command. However, a few prerequisites and post-installation configurations are recommended to be done along with that.
Prerequisites
Before installing Git on your Raspberry Pi, make sure that you have the following:
- A Raspberry Pi with all the necessary accessories.
Any model will do for a basic test, make sure it’s powerful enough if you work in a team, and has enough disk space for your projects. - The latest version of Raspberry Pi OS is installed.
If you are just getting started with Raspberry Pi, you can follow these guides to get you up and ready to follow this guide.
Related Tutorials:
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
- The resources I recommend to get started
- What Is Raspberry Pi OS? (Goal, Installation & Benefits)
- Raspberry Pi OS step-by-step Installation
- Getting Started with Raspberry Pi Imager: The Ultimate Guide
Installation
Installation of Git on Raspberry Pi is pretty straightforward. Follow these steps to get it set up and running.
- Make sure your system is up to date using the command:
sudo apt update && sudo apt full-upgrade
- Now, you can install Git from the official repositories using the command:
sudo apt install git
- You can now verify the installation and its version using the command:
git --version.
That is it; installing Git on Raspberry Pi is that simple. However, we do need to make a few configurations to be able to use it reliably.
Configuring Git
Now that you have installed Git, there are a few things that we have to configure before getting started with it. To configure your Git installation, follow these steps:
- Configure your commit username and email address for easy reference when collaborating with a team in the future. You can do so with this command:
git config --global user.name "Your Username"git config --global user.email "[email protected]"
- You can verify your configuration with the following command:
git config --list
Now, we are ready to start using Git on our Raspberry Pi.
Getting Started with Git: Key concepts & first steps
Having successfully installed and configured Git on our Raspberry Pi, we can use it to manage our code.
Check this: 7 Surprising Uses for a Raspberry Pi
Key Concepts
Before starting, though, it is essential to learn a few basic concepts regarding Git. These concepts will help us understand how Git works.
Working Directory vs. Staging Area vs. Repository
When working with Git, there are different use spaces that you have to understand:
- Working Directory: This is the local directory in which you are making changes to your project. It only shows a single checkout of one version of the project that has been pulled out from the more compressed Git repository.
- Staging Area: The Staging Area, more commonly referred to as the “index,” is a file contained in your Git directory (the directory that contains all the metadata related to the Git repository). It includes information about all the changes that will go on in your next commit.
- Repository: As previously discussed, the Repository is where your code is saved along with all the metadata indicating the changes and versions of your project. The repository can be local or remote.
The Basic Git Workflow goes like this:
- You modify files in your Working Directory.
- You selectively stage just the files you want to be part of your next commit. Only these files are contained in the Staging Area.
- You commit your changes, which take the files in your Staging Area and save a snapshot of that to your Repository.
- You can pull/ push to sync your local repository with a remote repository to collaborate with your team.

If a particular version of a file is in the Git directory, it’s considered committed. It is staged if modified and added to the staging area. And if it was changed since it was checked out but has not been staged, it is modified.
Repository: Local vs Remote
A Repository in Git is a centrally located directory that contains all the code and files that track all the changes made to that code. Once you have a Git Repository, you can change it and track all the versions.
There are two types of Repositories:
- Local Repository: A Repository hosted on a local machine for single users.
- Remote Repository: A Repository hosted on a remote server. It could be on the internet like GitHub, a remote server like GitLabs, or the same machine on a different path. It is used when working with multiple users/ team members to collaborate.
You can also use the version control features of Git using a local repository for an individual user. However, collaboration tools like pull and push are only available through a remote repository.
Commit: Recording Changes
When working with Git, the most important command is the Commit command. Using this command, you save the changes made to your project thus far as a different snapshot/version. Git keeps track of all your commits, and you can eventually revert your project to an earlier commit.
When you commit in Git, it stores a commit object that includes a pointer to the snapshot of your staged content. This object also contains the author’s name and email address, the message you provided, and pointers to the previous commit or commits that directly preceded this one.
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
Without committing, all the changes that you have made are only stored in the working directory and are not saved in the Git Repository. If you load from your local repository or pull from your remote repository, all changes will be lost at this point.
Another thing to understand is that the Commit command in Git only saves changes to your Local Repository (unlike SVN). If you want to collaborate on these changes with your team members, Push to a Remote Repository.
Branches: Working on Features Independently
Branches are used when you want your Project to go in different directions, e.g., when one person works on one feature while the other works on a separate one. These branches can later be merged into the main branch to collaborate on changes.
Another use case is if you want to test some changes out, you can do so in a separate branch and then selectively make changes to your main branch based on your testing.
The key advantage of Git over other VCS (Version Control System) tools is that its branching and merging are incredibly lightweight and instantaneous, as it does not require you to create multiple copies of the entire project.
Head
Head in Git refers to a Pointer pointing to the latest commit on your checked-out branch. It represents the tip of the branch. It can be considered the “current branch marker” and should always be pointed towards what you are working on.
Basic Usage: Your first project
With this basic understanding of how Git works, let us get started with it.
Initializing a Repository
The first thing we need to do when working with Git is to create a project and initialize our repository. To do so, follow these steps:
- Make a directory that will host all the files of your project:
mkdir <Name of Your Project>
- Change the directory to your project directory:
cd <Name of Your Project>
- Make a README.md file. This file will contain information regarding the project so that people can read and understand what the project is. We are using Nano as a text editor, but you can use any text editor you are comfortable with:
nano README.md
- Now, edit this file and save it:

- Now, you can check all the files in your project folder using the command:
ls -a
- You can initiate your repository using the init command:
git init
- This command creates a .git folder inside your project directory that contains all the metadata regarding your project. You can look inside it using this command:
ls .git -a
That is it; we have created our first Git repository. Now, we can change it and use Git to track all our changes.
Adding Files to Your Repository
Although we have created our Git repository, the prompt message showed that it was empty. The README.md file we created is not a part of this repository, so Git is not tracking changes made to it.
To add files to your Git repository, follow these steps:
- To add a file to the Git repository, we need to use the add command:
git add README.md
- Alternatively, you can use the “–all” flag to add all files to the Git repository:
git add --all - Now Git will track changes made to the files added to your Repository.
- At any time, you can check the status of your repository using the status command:
git status
- As you can see, there have been no commits to this repository. Further, we can see that the README.md file has been staged and will be added to the repository in our next commit.
- Now, we can use the command to commit changes to our repository:
git commit -am "<Your Message Here>"
The README.md file has been added to our repository and is being tracked for changes. You can repeat these steps to add all the files in your project to your Git repository.
Syncing with GitHub
We have created our first Git repository and added all project files to it. While Git tracks changes made to it, you can quickly revert to an earlier version. However, having another backup in a cloud/ remote server can add an extra layer of protection and allow you to share your project and collaborate with your team.
There are multiple options for this, as discussed earlier. You can use GitHub, GitLab or Bitbucket. For this tutorial, we’ll be using GitHub.
Download the free PDF, keep it open, and stop wasting time on Google.
Download now
To push your changes to GitHub, follow these steps.
- The first step is to create an account on GitHub. Proceed to this link and sign up for a free GitHub account.

- Once your account has been created and verified, sign in and click “Create Repository.”

- Set your repository’s name and click “Create Repository” towards the bottom of the webpage.

- This will now bring you to a page showing instructions regarding usage.

- Next, we need to set your repository’s origin on our Raspberry Pi to this newly created repository on GitHub. To do so, use this command:
git remote add origin <Link to your GitHub Repository>
- Now, we can use the push command to push all changes to our repository on GitHub:
git push -u origin <Branch Name>
- If this command gives an “Authentication Error,” see the note below to resolve it.
- If you look at your GitHub repository, you will see that all changes have been synced.

Your local repository has been synced to GitHub, and you can start collaborating with your team.
Note: If you get an “Authentication Error” when trying to push your repository, you need to generate a Personal Token and use that as the Password when prompted. You can follow this in-depth guide on generating a Personal Token.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.
Going Pro: Intermediate Usage & Features
So far, we have created our own Git repository, added files, and synced it with our GitHub account. Now, let us look into some intermediate functionalities that Git offers.
Undoing Changes
The time machine functionality of Git is the most critical intermediate functionality to learn. Suppose you were working on a project and realized that you had made a mistake and would like to revert to an earlier version of the project. You can revert to a previous commit and undo any changes.
Un-Staging Files
If you caught the mistake before committing and had only staged the files for the next commit, you can use this command to un-stage a file:git restore --staged <Files to be Unstaged>
Amending Last Commit
However, suppose you have already made the commit but forgot to stage some files or wish to change the message for the commit. In such a case, you can use the –amend flag to change the commit that you just made:git commit -amend -m "<Your amneded message>"
Using the “–amend” flag, the changes will no longer be saved as a new commit but as changes made to the already implemented commit.
Reverting to an Earlier Commit
If you have made several commits and wish to revert your project to an earlier commit, that can be done as well.
- You can see all changes made to a file using the log command:
git log <File Name>
- Here, you can see what commit changes were made to this file and the remarks entered in each commit. However, if you did not add accurate remarks when making the commits, you can see detailed changes in the files using this command instead:
git log -p <File Name> - To restore to an earlier commit, you can use the checkout command with the Hash of the commit you wish to return to:
git checkout <HASH> <File>
- The file in your working directory will now be restored to the earlier version. You can now commit these changes to make the restoration permanent:
git commit -am "<Your Remarks>"
Using these functions, you can undo changes made to your project at any stage and quickly revert to an earlier version of the file if you ever make a mistake in your project.
Branching and Merging
Branching is a powerful feature in Git that lets you try out changes in a separate branch, and if the changes meet your expectations, you can merge them with your master branch.
To create a branch of your project, follow these steps:
- You can check which branch you are presently working on using the command:
git status
- Now, you can create a new branch and change your head to it using the checkout command:
git checkout -b testing-changes
- You can make changes and commit now without impacting your master branch. If you are unhappy with the changes, you can delete the branch using the command:
git branch -D <Name of Branch>
- Alternatively, if changes meet your expectations, you can merge this branch with the master branch by first changing back to the master branch:
git checkout master
- Now, you can merge the changes using this command:
git merge <Branch Name>
- You can use this command to check out what branches are in your project and what branch you are currently working on:
git branch
Tracking Changes and Viewing History
When working on a large project with multiple commits and branches, it is essential to track all changes and view the overall history of the project. Several useful tools are available in Git to monitor changes to your files.
- You can view a simple log of all commits made to your project using the log command:
git log
- You can use the -p flag to show details of what lines of text or code were added or removed from each file:
git log -p
- You can additionally use the –stat flag to check the stats of each commit:
git log --stat
- This command can view the branch history with all the forks and merges:
git log --pretty=format:"%h %s" --graph
Download the free PDF, keep it open, and stop wasting time on Google.
Download now
Using these commands, you can effectively review the history of your project and track all changes made to it.
Tagging
In Git, you can tag specific commits to mark them as important. One of the most common use case scenarios is to add version names like V1.0, V1.2, etc, to your commits.
- You can add a tag to your current commit using the command:
git tag -a <Your Annotation> -m "<Your Remarks or Message>"
- You can also add a tag to an older commit using the hash of that commit:
git tag -a v<Your Annotation> -m "<Your Remarks or Messages> <HASH of the Commit>
- You can see all the tags in your project using the command:
git tag
- The push command does not automatically upload your tags to the remote server. If you want to update the tags to the remote server like GitHub, use this command to push instead:
git push origin --tags
If you prefer watching videos instead of reading tutorials, you’ll love the RaspberryTips Community. I post a new lesson every month (only for members), and you can unlock them all with a 7-day trial for $1.
Bonus: Advanced Functionalities
Equipped with these tools, you are now ready to use Git comfortably. However, some advanced functionalities could prove helpful as well. Let me briefly go over a few such advanced functionalities.
Stashing Changes
Suppose you were working on your project, and midway through implementing something, you realized something important must be done before committing these changes. In such a case, you can stash your changes and revert to your project. Then, pop the stashed changes and apply them to your project.
This command can stash all changes in your staged and unstaged file:git stash
You might also like: Pi5 vs. Pi4: I tested them, here's the result
You can do any Git operation on your project, make changes, commit changes, switch branches, etc. Later on, when you are ready, you can use this command to reapply previously stashed changes:git stash pop
It is important to note that the stash is kept on your local machine only and does not get synced with your GitHub project or any other server-based project.
Rebasing
We previously saw how you can create a branch and how to merge these two branches to continue work in the master branch. Rebasing is another option to keep your project’s merge and branching history clean.
Rebasing, instead of making a three-way merge of the two branches and the common ancestor, only applies the differences between your branch and the common ancestor to the rebased branch.
While the result of both merging and rebasing is often the same, rebasing allows you to clean up the branches and make it seem like the changes were all implemented using sequential commits.
You can follow this in-depth tutorial to learn more about rebasing.
Cherry Picking
Another cool functionality available in Git is cherry-picking. You can apply a specific commit in a different branch to your current branch with cherry-picking. This, instead of merging both branches completely, only applies the selected changes to your master branch.
You can follow this in-depth tutorial on cherry-picking.
Hooks
Hooks is another advanced functionality available in Git. Hooks are special scripts that can be run whenever a specific event occurs in your Git repository. Hooks are a powerful tool that can encourage a commit policy, alter the project environment whenever a commit is made, etc.
To learn more about hooks, you can follow this tutorial.
Whenever you’re ready, here are other ways I can help you:
Test Your Raspberry Pi Level (Free): Not sure why everything takes so long on your Raspberry Pi? Take this free 3-minute assessment and see what’s causing the problems.
The RaspberryTips Community: Need help or want to discuss your Raspberry Pi projects with others who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct help (try it for just $1).
Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.
You can also find all my recommendations for tools and hardware on this page.
