Git and GitHub basics for Data Scientists | by Philip Wilkinson | Towards Data Science

This year, as Head of Science for the UCL Data Science Society, the society is presenting a series of 20 workshops covering topics such as introduction to Python, a Data Scientists toolkit and Machine learning methods, throughout the academic year. For each of these the aim is to create a series of small blogposts that will outline the main points with links to the full workshop for anyone who wishes to follow along. All of these can be found in our GitHub repository, and will be updated throughout the year with new workshops and challenges.
The eighth workshop in the series is an introduction Git and GitHub where we will cover what is Git and GitHub, creating your first repository, making your first commit and then linking this to a remote repository. Further details on branching, secret files and going back to a previous commit can also be found on our GitHub page.
If you have missed any of our previous workshops you can find the last three at the following links:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
According to the Git Website:
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
Including:
Acting as an industrial production standard
For use during a Hackathon
For use in writing a book
For keeping lecture and practical notes
Any many other uses ...
Git itself differs from GitHub in that Git is a system that manages the version control of a project, while GitHub is a remote platform that hosts project using Git. This means that while can be using the Git system, you don’t necessarily have to use GitHub, unless you want a backup of your repository on another platform.
For interacting with Git and GitHub, we will not be using Jupyter Notebooks that we have already but instead will be interacting with the comand line/terminal. For this, specifically in relation to GitHub operations, I tend to prefer using GitBash which allows me to interact with GitHub nice and easily, but you can use your own term on Mac or Bash on Windows.
In order to create a project we need to create a directory/folder for your project. In the command line we can do that using the mkdir command, followed by the name of the folder, which will create a folder on your machine.
 mkdir my-novel
We then navigate to that folder through the command line using the cd command which will allow us to change directory as follows:
cd my-novel
Once we are in that directory then we can initialise the project with Git so that Git can be used to manage the versions of the project using the git init command:
git init
Now, you should have a .git folder in your directory which should current be invisible. Your project that you just created should be called the local workspace.
Of course, now we have created the repository then we need to actually fill it! We can go ahead and create the start of our novel still using the command line by using the nano command which opens a text editor to begin creating a file. In our case we can call this intro.txt as:
nano intro.txt
Where you can go ahead and write your own intro to the novel. Once this has been done you can use the commands ctrl o to save the file and then ctrl x to leave the file.
Before we can then commit our changes, to make sure we have a record of our initial work as it goes along, we need to add our changes to the stage (otherwise known as staging the changes). This is done using the git add command whereby you can either specify the file names you wish to add or you can use . to specify any changes as follows:
git add intro.txt
We can then check our stage using the status command as:
git status
Where we will see
On branch masterNo commits yetChanges to be committed:  (use "git rm --cached <file>..." to unstage)	new file:   intro.txt
The key thing for us is that this tells us we have changes to be committed, the new file intro.txt , but that so far we have made no commits.
With this in mind then we can commit our changes using the commit command. The key thing here is that we can use the -m argument where after we specify a comment string which explains what you are committing, any changes that you made and why this may be. This just ensures that you remember any significant changes that you made so that if your code ends up breaking, or in our case we want to undo a chapter of our novel, we know which commit to go back to according to the message. Here we can do this as:
git commit -m "Initialise the novel with an intro chapter"
Where we have now made our first contribution to the project!
Of course, having all of the changes on our local machine is all well and good for version control (no more final final final version 27.txt files!), but what if we want to then collaborate with other people or we want to have a remote back-up in case something goes wrong? For this, we can use the magic of GitHub.
For this, we will need to create a GitHub account and initialise a new repository. For this, once you have set up your account, you need to create a new repository but make sure that you do not initialise this with either a README, licence or gitignore files as this will create errors. Don’t worry though, you can create these files after you have pushed to the remote repository.
Since we have already made our commits to our own local repository all we need to do is to link the remote repository to our own. For this we need to use the repository url, which is found here:
On your project which we can then link this through our terminal using git remote add origin as:
git remote add origin <REMOTE_URL>
which make sure you are using your own URL.
We can then verify that this connection has been made using git remote -v which should print the remote url that you have specified. Now we have connected to our remote repository!
One issue here however is that we have not set where exactly in the remote origin to push to. While we have our local respository, we don’t have a branch yet in our remote repository. For this, we can specify:
git push --set-upstream origin HEAD:master
Which will tell the current head of the repository to push to the master branch within our repository.
We can then make our first push to our remote repository by specifying we are pushing the HEAD as:
git push origin HEAD
Which will mean in the future all we need to do, after we have added all of our files and committed them, is to use git push in the future, which is nice and simple! You can check to make sure that your files are on the remote repository by going to the repository within you GitHub account and the files should be there. This will then allow you to not only store your work on a remote repository but to also work collaboratively with other programmers as well!
So there you go, you have now created your first local repository, committed your files, then pushed those to a remote repository. In the practical itself, more details are provided on branching, .gitignore files (to keep things secret!) and going back to previous commits as well, which can all be found HERE.
If you want any further information on our society feel free to follow us on our socials:
Facebook: https://www.facebook.com/ucldata
Instagram: https://www.instagram.com/ucl.datasci/
LinkedIn: https://www.linkedin.com/company/ucldata/
And if you want to keep up date with stores from the UCL Data Science Society and other amazing authors, feel free to sign up to medium using my referral code below.
philip-wilkinson.medium.com
Other medium articles by me can be found at: