What is Git?Introductions, Software Engineering ·
Git is a version control system developed by Linus Torvalds to help manage the Linux source code. There were lots of developers adding features, removing bugs, and doing other things to Linux and it was hard to keep track of all the changes. It was also difficult to see how changes were effecting the code as a whole, making it much harder to roll back to an older version if a bug was discovered. The aim of git was to create a version control system that would allow lots of developers to work on their own section of the code whilst maintaining a version that was bug free and ready to ship.
What is GitHub?
GitHub is a service built on top of Git that allows developers to collaborate all over the world to solve problems. It’s probably best summed up by this short video.
What you’ll need
- A GitHub account (GitLab or BitBucket should work equally well but this tutorial is focussed on GitHub)
- A git terminal
Setting Up Your Git Terminal
First things first, we need to tell git our name and email. This will allow developers to contact one another to ask about code changes if git is being used outside a service like GitHub.
git config --global user.name "your name here" git config --global user.email "your email here"
Git bash utilises the bash commands popular on macOS and Linux. Here are some of the basics:
ls #list the files in a directory ls -a # list ALL the files in a directory pwd #prints the working directory (you're current directory) touch file-name.txt #creates a file called "file-name.txt" mkdir directory-name #creates a directory called directory-name cd directory-name #changes current directory to directory-name
Creating Repositories on GitHub
A repository, or repo for short, is the place where your code lives. I should emphasis that it is where the code lives, not where other things that go along with a programming project live. Files such as compiler outputs (
.class files in Java for example) should be generated by each developer, not stored on a git server. These kinds of files are excluded automatically by .gitignore files.
Making a New Repository
- Go to github.com
- In the top right corner, press the + button
- Select “New Repository”
- Give it a name (replace spaces with ‘-‘)
- Give it a description
- Decide whether it will be public or private
- Initilise the repo with a README.md file
- Add a
.gitignorefile for the language or IDE you’re using
- Add a license (Any of the first three do the job perfectly)
You will now have a pretty empty repository on GitHub
Basic Git Commands
The git clone command will download a repository into a new folder in your current working directory. When on a project’s page there will be a drop down button to get the URL required to clone that specific project. You can use the clone command to download the repository you just made in the previous step. Go to the repository's GitHub page (
github.com/<your-username>/<repository-name> ) and press the green “Code” button. This will let you copy a link to the
Once you’ve navigated into your git project’s directory, you’ll need to make sure you have the most up to date version of the code. This is useful if you’ve made changes on a computer, synced them with GitHub, and now want to update the code on the computer you’re currently using.
This command will list all the files which have changed since the last commit. However, it will ignore files that are included in your
.gitignore file. For example, in a git repository for a Java project, the
.gitingore file will include the line
*.class which will stop git from saving any
git status will be colour coded; red for files that haven’t been added to the commit, and green for files that have been included.
This is the command adds files or folders to your next commit. The add command also accepts globs as an input, allowing all the files in a directory to be added through one command.
git add file-name #adds file-name git add dir/* #adds everything in the directory 'dir'
Once all the files that have been changed have been added, it’s time to commit your changes. A commit is essentially a version, allowing you to roll back to later in time if something goes wrong. It’s good practice to include a short descriptive message telling others what changes you have made. This is achieved by adding the ‘-m’ option, followed by a message.
git commit -m "A message briefly saying what changes you made"
When you’ve finished for the day, or want to publish your changes for others to see, it’s time to push your code up to GitHub. A git push operation will send your latest commit up to GitHub, so it’s a good idea to run a git status to make sure everything will be included. If not, it’s just a case of using git add to include them in a new commit, before doing a git commit -m to ensure the changes are ready to ship.
The git push command will push the branch you’re currently working on up to GitHub’s version of that branch.
git push origin master#pushes your changes up to GitHub