Collaboration with git
So far we have worked locally with git. You can also collaborate with several people on 1 repository by working with a remote server. Everyone has a local copy of the repository, and can synchronize their changes via a remote server with other people who have the repository.
In theory, you can use any computer on which you have installed the git command tools as a server. It’s just not that practical to work together from home, you have to make sure that your computer can be reached externally and everyone who works together must know your ip address or hostname.
That is why we use a hosted git environment. There are several options for this, we will use GitHub
Create & link GitHub account
We will create a GitHub account and link our GitHub credentials to our computer, so that we don’t have to re-enter our password every time.
Visit https://github.com and create an account. (with your @student.howest.be account) Then go to https://education.github.com/pack and request an educational pack by clicking Get your pack, this will be useful to create private repositories.
Then open a terminal window. We’ll link our name & email address to git so git knows who is doing a commit. Make sure your email address is the one you used to create your GitHub account:
SSH key
The next steps are a summary of this guide When things should be unclear, you can read the guide in more detail.
To authenticate yourself with GitHub, you need to create an SSH key. This is a key that is stored on your computer and is used to authenticate yourself with GitHub. You can create a key with the following command:
When asked for the location of the key, you can give the key a more describing name:
You can press enter to leave the passphrase empty (this can be a security issue).
Now go to your .ssh directory to check if everything is there. You should see (at least) 2 files: github
and github.pub
SSH agent
Make sure you are still in de .ssh directory.
Create a config
file if there isn’t one yet:
And open it in the editor of your choice. (We are using code
here, see Launching VS Code from the command line
Now add this to the config file and save it:
(note that we didn’t add the UseKeychain option —as mentioned in the guide— since we are not using a passphrase)
Now we can add the key to the ssh-agent:
Adding the key to GitHub
Copy the contents of the public key (make sure you are in the .ssh directory) the command pbcopy
lets us put content in our clipboard, it is like a ‘cmd-c’
On GitHub, go to Settings → SSH and GPG keys → New/add SSH key → Give it a descriptive name (e.g. “Macbook”) → Paste the key → Add SSH key
You can test if everything is working by running the following command:
Remember to choose the SSH option (instead of the HTTPS option) when cloning a repository.
Create repository & first push
Login to your GitHub account, and click the “New repository” button. Choose a name for your repository and click the “Create Repository” button.
Don’t add a readme.md
or a .gitignore
file yet. Since we will sync this repo with an existing one (the one we created in a previous chapter) things will get complicated if there are files on both ends. (It is definitely possible to fix this, but we won’t go in to that now) If you would start completely from scratch, this wouldn’t be an issue.
Open a terminal window and navigate via cd
commands to the directory of the git repository containing the “hello world” files. We will make sure that we can synchronize our local repository via GitHub, by adding a “remote”. A remote is a location where you can synchronize a git repository:
(don’t forget to change git@github.com:demouser/hellogit.git
with your own repository url, you can find it under the green ‘code’ button on your GitHub repository.)
The git push
command will upload local, unsynchronized changes to the remote location. You use the -u
attribute the very first time, to ensure that you don’t have to specify the remote name in future synchronizations. You can then just run git push
in the future.
Git add - commit - push
From now on you can start pushing commits to the online repository. Usually you bundle several local commits when you push. In fact, we already did this with our first push: all the commits we ran earlier are now also in the online repository.
Delete the world.txt
file, add commit. We use git add -u .
to ensure that the delete action of that file is staged:
Create a README.md file, with a little info about the repository:
Execute git status
. You can see in the status report that the repository is 2 commits ahead of the online version:
Execute git push
to put your commits on GitHub:
When you view the repository through your browser, you will see that the contents of the README.md
file are shown below the list of files. This is a file in the Markdown format. (the same for the file you are reading now…) Markdown is a simple markup language for formatting documents. More information about this can be found on Wikipedia or this GitHub specific flavor