Using Git

What is git?

Git is a free and open source distributed version control system. What does that really mean? It is a way to track changes to files on your computer. This is like keeping a detailed log of every time you change a file, what lines and characters were changed. So you could look at the log and see what changed, or undo those changes if you wanted or if you are working with others you can merge your changes together.

It’s a lot to take in, so don’t worry if you aren’t following yet.

If you want a more detailed walk through than we provide here, have a look at the Git Book.

Installing

Let’s start by getting git installed on you machine. You can check if it’s already installed by running git --version from the command line.

Follow your operating system specific instructions in Chapter 1.5 of the Git Book.

Basic concepts

Repository
This is a file structure on disk, like a database, it contains all files and the log of changes.
Staging
When you make changes inside a repository, they are untracked. You decide which changes to track, as you add changes they are added to the staging area. This let's you see all current changes before committing them.
Commit
After you are happy with the changes tracked in staging, you can commit them to the log we mentioned. You have a few options for writing a message that will be stored with the commit in the log, more on that later.
Branch
When you are in the repository the default out of the box is usually called `master`. This is the main branch of the repository (NOTE: if you are creating a new repository of your own, please change the default branch to `main`. `master` is an inappropriate and offensive), the main branch of the repository. Typically you will want to do your work on a new branch for each feature or bug. This allows you to see and work on different versions of the same code in one repository.
Checkout
To check out a branch, is to switch to view that branches version of the files in the repository.
Merge
When you want to incorporate another branch, `main` or someone else's feature for example, into your current branch you will merge the changes. This will apply the other changes on top of yours.
Remote
This is just a repository, that is accessible remotely. You can use the git command to push and pull changes to.
Push
Pushing to a remote will synchronize your locally committed changes to the remote.
Pull
Pulling from a remote will both fetch and merge the changes on the remote with the branch you have currently checked out.
Fetch
When you want to get some remote branch or changes, but not merge them yet, you can fetch them. Just ask the remote for the data and store it locally but not incorporate it into anything. You could then checkout the feature branch and run the code, or read over the changes.

Try it out

There is a great interactive tutorial, for free, available at Code Academy. Take some time to play with it and try out some of the commands.

Fork me on GitHub