Git and Github #
Git is a program for tracking changes to software in a fancy structure of files called a repository. Tracking changes to software is called version control
. Git is a DVCS
, for “distributed version control system.” It is by far the most popular DVCS. On a Raspberry Pi, you can install git with a command like this: sudo apt install git
Github, owned by Microsoft, is a website for hosting git repositories. There are lots of competing options, like Gitlab or Sourcehut, but Github is the dominant player. You can also store git repositories on your own computer; they’re just a collection of files in a folder.
What is this “distributed” stuff? #
In the past, version control systems used a central server. This works fine for small groups of harmonious individuals, but for large groups, it’s a mess. In a distributed system like git, no repository has primacy over any other; there is no central server.
The 7 most useful git commands #
git clone
git pull
git add .
git status
git diff --staged
git commit -m "Prevent ARP cache poisoning"
git push
Typical workflow #
-
You either create a new repository locally using
git init
, or you clone a repository from somewhere else usinggit clone
. -
(With a cloned repository, if some time passes before you are actually ready to commit code, you would run
git pull
to get any new changes that have been committed.) -
Actually change the code in your working tree– add new features, fix bugs, whatever. The “working tree” is just the code in your local folder. Take a look at the diagram below.
-
Stage the code for commit with
git add
. -
Check that you didn’t miss anything with
git status
. You can also review the changes that you have staged withgit diff --staged
. (Plaingit diff
shows you the unstaged changes, i.e. stuff you haven’t added to the index yet.) -
Commit the code with
git commit -m 'Write what your changes do in this commit message'
. -
If you’re just working locally, you’re done. You can review your changes with
git log
. Otherwise, you can push your code to a remote repository, like Github, usinggit push
.
Authentication with Github #
In order to let your local computer, running git, push changes to a remote repository on Github, you need to identify your local computer to Github. On a Raspberry Pi, there are two steps. (It’s similar on a Mac and probably on Windows too.)
- Generate a pair of encryption keys, one public and one private, using
ssh-keygen
. - Go to Profile > Settings > SSH and GPG keys on Github and add your public key, which you can find at
/home/pi/.ssh/id_rsa.pub
. (The private key stays secret on the Raspberry Pi.)
You can also push to Github using your username and password, but that method is deprecated and will probably disappear sometime soon.