Git is a version control system. This means you can use it to
This guide teaches you how to use Git via the command line. There are a variety of GUIs, and Visual Studio Code has Git built in. I think it helps to learn using the command line to really master the concepts.
The easiest way is to use the version that ships with MacOS:
This will prompt you to install Xcode
if you haven’t already. Once it is
installed you should see something like this:
If you really want the latest version, you can install Homewbrew and then use:
Use Git for Windows, which will get you Git BASH
for a command line version.
Install with your package manager
Do some basic configuration first:
This way when you add code, git will associate your changes with your name and email address.
You may also want to configure an editor that you will use to edit commit messages:
I prefer to use vim
. If you skip this step, then you will use your system’s
default editor.
When you are working with Git on your own machine, your actions look like this:
Every project gets its own repository. So you were building an app to keep track of ideas for writing a book, you would create a new repository for it. Meanwhile, if you had a website you were building for a friend’s knitting store, you would likewise keep that in a separate repository.
Let’s start with a project that contains text files. This way we don’t have to worry about whether you have the right setup for a programming language.
Open a terminal. On MacOS, use the built-in terminal or any other terminal you
have installed. On Windows, use Git Bash
.
This:
poetry
You should see a message after the last step:
Take a look around:
You should see something like this:
Git will store all of your versions in the hidden .git
directory. If you list
what is there:
you should see:
This is where git is keeping track of different versions of your code, along with some configuration files. We won’t go into the details of what is stored here, just know that this is a very important directory. It is Git repository.
Using your favorite editor, add a file to this project, called
the-read-wheelbarrow.txt
, with the following poem:
This is a famous poem by William Carlos Williams.
Now type:
You should see something like this:
main
branch. We’ll talk more about branches later.To add this new file to your Git repository, type:
Now check the status again:
Now you should see the added file:
Notice how this says that your new file is a change to be committed. What you have done is placed your file in a staging area. This allows you to prepare a set of changes carefully, including adding and removing files as you see fit, before committing those changes to your repository.
You can finalize your commit with:
This will open the editor you configured, e.g. vim
, to edit a commit message:
You can enter a message here, save and quit, and then your commit is done:
Notice how it tells you that you changed 1 file and inserted 11 lines.
For best practices, commit messages should contain details about exactly what you are committing. If you’re writing code, you might explain the new functionality you have added, or the bug you have fixed. Detailed commit messages help your team know exactly what you did, and can likewise help your future self to remember what you were doing. We also often wrap commit messages at 80 characters so they show up well on a terminal.
For very short commit messages, you may alernatively use:
Let’s take a look at what we have done so far:
You will see a log of all the commits you have made so far, which should be just one:
Each commit has a SHA-1 hash, giving it a unique identifier, an author, a date,
and a commit message. This uses a pager
, so you need to type q
to exit.
If you want to see more detail, add the -p
flag, which stands for patch
.
This shows you the details of what was added and removed, using a common patch
format.
All of the added lines are prefaced with a +
.
Edit the poem and make some changes to it on several lines. Then run:
This shows that git sees that the file has been modified. You can commit the change in one line with:
or
depending on whether you want to use the editor. The -a
flag tells git to
automatically stage and then commit all files that have been modified. We did
not need to use git add
to add any new files.
You should see:
You can also view the log to see the changes:
Notice how Git shows your changes in patch format, with removed and added lines.
When you build up a commit history, you have a chain of versions:
We’ll talk about how to access those older versions later.
Git is a distributed version control system. When you work on a team, you can share repositories through a central system, such as GitHub, GitLab, or BitBucket. In that case, you and your teammates are synchronizing your repositories with the service you are using:
To start with GitHub, you’ll need to create an account on GitHub.
Once you do that, you should add an SSH key to your GitHub account.
We’re going to take your poetry repository and push it to GitHub. To start, create a new repo by clicking this button in the top left:
Fill out the form, but don’t click the box to add a README. You do need to decide whether your repository will be public or private.
Once you are done creating the repository as described above, you will have an empty repository. Then you’ll see these instructions:
Since you have an existing repository, follow the bottom instructions:
(Note there is another command GitHub proposes to rename the “master” branch to “main”. But your repository probably already uses “main” for the new branch. This is part of a larger movement to remove “master/slave” terminology from computer science, because it is insensitive, and instead using “main” or “primary” and “secondary” or “backup” instead.)
The first command adds a remote repository to your configuration. This lets you sychronize the repository on your local machine with the one you just created on GitHub.
The second command pushes your local repository to the remote repository. This will copy all the versions you have created to the remote repository so they are identical.
Try this out:
git add
, git commit -am "message"
to commit locallygit push
to push to GitHubNormally if you are working alone on a project, every time you push it will succeed. However, if you are working on a team, conflicts could arise. We’ll discuss that later.
You can clone a repository by using the git clone
command. To start, find a
repository you want to clone. You can use
this small test repo.
Click the Code
button:
Normally, you should use SSH
to clone. Alternatively, you could setup access
tokens if you want to use HTTPS
.
For example, you can clone this repository with:
Now practice using GitHub with a friend:
poetry
repository on GitHub.Settings→Collaborators
and use the Add people
button to add your
friend.git pull
to pull the changes and sync with your local repositoryOften, you will have teammates making changes at the same time. If you make conflicting changes, you’ll end up with a Git conflict. Let’s show you how to resolve this.
git push
. This will succeed.git push
. You should see a message like this:Let’s talk about what this means!
Since your local repository is not synchronized with the remote repository on
GitHub, you need to pull in those changes. Try using git pull
. You’ll now see
something like this:
At this point, any files listed with CONFLICT
next to them will have both
your code and your friend’s code. They will be clearly marked! And Visual Studio
Code has a really nice
3-way merge editor
to help you resolve the conflict.
If you look at the file with a conflict, you will see something like this:
You can see the markers: <
, =
, and >
marking the different versions.
Choose which version you want, delete the lines with markers, and use
git commit
to commit the change. Now you can use git push
to push the fixed
version, and your friend can use git pull
to get the new version.
If we have time, we’ll follow the Git Book to learn about Git Branches in a Nutshell and Git Branching - Basic Branching and Merging.
Once you understand branching, you can consider a basic branching strategy. See for example:
You can look at any of these versions using the commit hash. For example:
If your editor is open, you’ll likely see the file you are editing revert to its older version. Git will warn you that you have checked out an old version:
You can go back to the original with git checkout main
.
If you prefer a GUI, you can use integrations in Visual Studio Code, or you can try GitHub Desktop