Table of Contents
Git is a version control tool that makes it easy to track changes (and revert if needed), as well as allowing multiple people to easily work with the same code.
Git can be used through a variety of methods. The traditional way to use Git is through the command line but there are also graphical options available (Sourcetree and Git Kraken). It is recommended to use Git through the command line if you are familiar with the command line as it is more flexible and powerful than most graphical solutions. Traditional Git can be downloaded here: https://git-scm.com/downloads
This tutorial assumes that you are using traditional Git through Git Bash. Graphical options will operate on the same principles but with different commands.
To edit an existing Git code repo locally on your machine, it is necessary to clone the repo. You can get a link to clone a repo by clicking on the green “Clone or Download” button in GitHub (see below).
Once you have have copied the clone link, navigate to your preferred code folder in Git Bash, and then run
git clone COPY_LINK_HERE. This will create a directory that contains the code for the repo that you downloaded.
If you're cloning the code for the first time, then it will be completely up to date. However, if you're returning to the code any time after you have first cloned it, then there is the possibility that someone else on the team has edited the code. As a result, every time that you start coding, you should pull the code. Pulling with Git is how to get the most up to date code from the GitHub servers onto your machine.
To do this, simply navigate to the repo and run
git pull in Git Bash.
Again, every time that you start coding, you should pull the code.
Let's say you've made some edits to this code and you're ready to call your work completed and save it. When you are ready to “commit” to the code, run the following steps:
- Navigate to the repo in Git Bash.
git add .to stage all of your edits to be committed.
git commit. This will bring up the vim editor with a commit message. If you are new to vim, read this tutorial: http://vim.wikia.com/wiki/Tutorial.
- Add a concise commit message detailing what you accomplished. Then save and quit the vim window, and your commit will be ready to push to the server.
git pushto upload the code from your computer to the server.
Branches in git allow for there to be multiple versions of the code existing at any given time, which is useful when multiple people want to implement new features that can't be completed in a single commit. By default the code repo will have a “master” branch, which is the “official” code for the repo that should only consist of complete code that is ready for use. Any new features that aren't guaranteed to work should be added in a new branch.
If someone else has already created the branch and you wish to switch to it, navigate to the code repo in Git Bash and run
git checkout BRANCH_NAME, where BRANCH_NAME is the name of the branch that you want to switch to. If you aren't sure of the exact name of the branch that you want to switch to, find the Diffusion page for the Repo and look at the list of available branches at the bottom. This should automatically update your local code to the code for that branch.
If you want to create a new branch, navigate to your code repo in Git Bash and run
git checkout -b BRANCH_NAME, where BRANCH_NAME is the name of the branch that you want to create. Then make your edits and commit some code.