An Easy Beginner’s Guide to Git
By: Julia Kho
Tags: Command Line, Git, Getting Started, Data Science, Python

Silly me. In our list of veggies, we listed pumpkins as a vegetable, but actually it’s a fruit! Did you know that? Let’s fix that mistake. Open up the two files we created and remove pumpkin from veggies and add it to the fruits. After you make the changes to those document, go ahead and practice doing another commit.

git add .

git commit -m “Changed pumpkin from veggie to fruit”

I used to be addicted to pumpkin, but then I went on the patch.

Photo by Maddy Baker on Unsplash

We’ve made two commits so far. Think of these as checkpoints that we can revert back to at any point in time.

Helpful reminder: Remember to commit often! It will make your life so much easier!

Review All Commits Made

If you want to see a list of all the commits we’ve made thus far, you can use git log . The log is where you will find the author of the commit, the date it was committed, and the commit message for every commit that was made.

If you’ve made many commits, you could be scrolling through this log for a while. It might be worthwhile to use git log --pretty=oneline which will condense your commit messages to just one line.

Source: Author

There are other cool parameters you can use like git log — author=julia if you want to see all the commits made by Julia.

If you are curious, this article here shows you 10 different ways you can use the log tool.

Ignoring Files in Commits

If there are files in which you would never want committed, you can create a .gitignore file to assist with that problem. This .gitignore file will tell git which files or patterns should be ignored so that they don’t accidentally get staged and committed. It’s the smart thing to do!

Photo by Andrew George on Unsplash

Create .gitignore file with this command in the terminal: touch .gitignore

Open up .gitignore and add the names of the files that you don’t want to be tracked. Note: if you don’t see the .gitignore, it’s probably hidden, so make sure you are viewing hidden files.

The snippet below is an example of what a .gitignore file can look like. The first and second lines are names of files that I may have that I don’t want to include. You can also specify patterns to be matched against file names. For example picture* will look for all files that start with the word picture. The asterisk here is essentially a wildcard that matches zero or more characters.

Source: Author

For more information on pattern matching, check out this link here.

Go ahead and practice a third commit by adding the .gitignore file to your local repository.

Git Branches

One really cool thing you can do with Git is that you can create different branches so that you can keep your work separate from the master branch. This is useful if you want to test code out.

In your terminal, if you type git status , you can see that you’re on the master branch.

Source: Author

By default, you will always be working off the master branch in Git. The two commits we made earlier were on the master branch. We have the ability to create a new branch (for example test) where we can test new code out without messing with the code in the master branch.

Source: Author

In the example above, each circle represents a commit. When we do commits in the test branch, those checkpoints will be kept separately from the master branch. If we like the code in the test branch, we can merge with the master branch. If the ideas implemented in the test branch failed, we can abandon them. When working with multiple collaborators, you can each create your own branch to work from as well.

Let’s create a new branch called test.

git branch test

To see what branch you are on, you can type git branch and the branch you are on will be highlighted in green. We are currently on the master branch.

We will need to move from the master branch to the test branch if we want to commit code to the test branch. To do so, use the following command: git checkout test

Source: Author

If you type git branch again, notice that we are now on the test branch.

Merging branches

While we’re here in the test branch, let’s modify our fruits.py file and commit the changes to this test branch. You may have noticed that we have duplicate fruits in our list. Add in the following code the fruits.py file to get a unique set.

fruits = set(fruits)

Save and let’s commit the new change.

git add fruits.py

git commit -m “Removed duplicate fruits”

This test branch contains the first two commits we made earlier along with our latest code commit while the master branch contains only the first two commits from earlier. However, we can merge the test branch to the master branch so that the master branch has the latest code.

Before merging the two branches, you can take a look at what has changed between the branches by using git diff test master . Test is your source branch and master is your target branch.

To perform the merge, you will have to move back to the master branch and then use the merge function like this:

git checkout master

git merge test

Create a Remote Repository to Share with Others

It’s great that you have saved all the changes in your local repository, but maybe you want to share it with your colleagues or with the world. We can create a remote repository and sync it with your local repository. That way, you can push changes to a central location where other people can access your code and they can help contribute as well.

There are several websites for hosting Git projects, but for the purpose of these exercises, I will be using a website called Github.com. It’s free to sign up! If you don’t already have an account, register on https://github.com/.

On the homepage, click Start a Project and create a repository. I’m going to name it “Git-for-Beginners”.

Source: Author

After you’ve setup the repo, copy the url for your repository. As shown below my repository url is https://github.com/julia-git/Git-for-Beginners.git

Source: Author

Now, we need to tell our local repository where our remote repository is sitting in order to establish a link between the two. Replace the line of code below with your own repository url.

git remote add origin [repository url]

You’ll be asked to enter your Github username and password to be able to connect.

Moving Code Between Local and Remote Repository

Now that a connection has been establish, to move all the code in your local repository to the remote repository, use git push -u origin master .

With this line of code, everything from the master branch on our local repository is going to now also be in the remote repository. If you wish to push a different branch, just replace “master” with the name of your branch that you want to push.

If you go back to your browser tab, you should see that your items have successfully been pushed the remote repository.

Source: Author

If someone else was working on this project and pushed changes to the remote repository, you can pull them down to your local repository on your computer. You will use the following to pull from the master branch: git pull origin master .

In summary, the commands below are some of what we’ve learned thus far.

Source: Author

Cloning a Remote Repository

Now let’s try something different. Let’s say that you’re not starting fresh on a project, but you’re helping out your teammate on their project. We will have to go to their remote repository and clone it to our computer to create a local repository. Feel free to go ahead and clone the one I created for this article using the following repository url: https://github.com/julia-git/Git-for-Beginners

git clone https://github.com/julia-git/Git-for-Beginners

This command essentially downloads the repository to your local computer and preserves the connection between the two repositories.