A Gentle Introduction to Git

For a software tester who has just started writing test automation, using version control software such as Git can seem daunting and confusing.  But being able to pull down the latest code, update it, and submit a pull request is very important for any team project!  In this week’s post, I’ll provide a gentle introduction to the basics of Git.

What is Git?

Git is a version control system.  A version control system is a system that allows a group of people to collaborate on code without accidentally overwriting each other’s work.  It also allows the group to keep track of who changed the code and when it was changed, so it’s easy to trace back to the source of a problem.

Why is Git needed?

Consider what file editing is like when you don’t use a version control system.  Let’s say you have a recipe for brownies.  You send the recipe to your friend, and he decides to change the amount of cocoa in the recipe.  When he makes that change, it is only in his version of the file, not yours.  Your files are now different.  If you make a change to add more vanilla to the recipe, now your versions have diverged even further. 

You can see how this would be unacceptable for software code!  In a version control system, there is one “master version” which is the accepted version of the code.  This master version lives in GitHub (or another version control hosting service), and can be “pulled” down by any user.  When someone wants to make a change to the code, they pull down the master version of the code, create a “branch” that is a copy of the master version, make their changes to the branch, push the branch up to GitHub, and then do a “pull request”, which is asking for someone to review their code and merge it into the master branch.
Confused?  Don’t worry, this will look much simpler with an example.  Let’s imagine that we have a source code repository called “The Thinking Tester Guestbook”.  We’ll take a look at what would happen if Prunella Prunewhip wanted to add her name to the guestbook.

These instructions assume that Prunella has already installed Git on her computer, and has already created a GitHub account.)



Step One: Prunella clones the source code repository

This is often called “cloning the repo” or “pulling down the repo”.  Prunella does this by going to the URL in GitHub that has the source code, and clicking the green “Clone or download” button.  A dropdown appears with the URL she will need to clone the source code.  She clicks the little clipboard button to the right of the URL to copy the URL text.

Prunella opens up a command window and navigates to the folder where she would like put the source code.  Once she’s there, she types git clone and then pastes the URL text next to those words.  The repository is copied from GitHub into a new folder.

Now that the repository is in a folder on her computer, she can open the folder up in her file browser and take a look at what’s in there.  She sees that there is one text file, called “guestBook.txt”.  The text file reads:

Kristin Jackvony was here on May 11, 2019

Step Two: Prunella makes a new branch and adds her changes to that branch

Before Prunella makes any changes to guestBook.txt, she should create a new branch and switch to it.  So in the command line, she navigates to the new folder that was cloned earlier by typing
cd ThinkingTesterGuestBook.

She can verify that she’s in the master branch by typing git status, and she will get a response like this: On branch master.

Now she can create a new branch and switch to it by typing git checkout -b NewEntry.  The command “-b” tells Git to create a new branch.  “NewEntry” is what Prunella has chosen to name her branch.  And the command “checkout” is what causes Git to switch to the new branch.

If Prunella types git status at this point, she will get On branch NewEntry as a response.

Now that Prunella is in the correct branch, she’s going to make a change to the guestBook.txt file, by adding one line, so that the file now reads:

Kristin Jackvony was here on May 11, 2019
Prunella Prunewhip was here on May 13, 2019

Step Three: Prunella commits her changes and pushes them to GitHub

Now that Prunella has made the change she wanted, she needs to commit and push her change.  First, she can run git status and she’ll get this response: 

On branch NewEntry
modified: guestBook.txt

This shows that the guestBook.txt file has been modified. Next, Prunella needs to add the file to the commit, by typing git add guestBook.txt.  Now if she types git status, she’ll see this response:

On branch NewEntry
Changes to be committed:
     modified: guestBook.txt

Next, Prunella commits her change by typing git commit -m “Adding a new entry”.  The “-m” in this command stands for “message”.  The “Adding a new entry” text is the message that she is adding to explain what she is committing.  The command line will respond with how many files and lines were changed.

Once the change has been committed, Prunella can push the change up to the GitHub repository by typing git push origin NewEntry.  The “NewEntry” value explains that the code should go up to the NewEntry branch, which doesn’t exist yet in the GitHub repository, but it will be created with this command.  “Origin” refers to the GitHub repository (this is also referred to as “remote”).  The command line will respond with several lines, the final line of which will be
* [new branch] NewEntry -> NewEntry, which shows that a new branch called NewEntry has been created in the origin, and that it was copied from the local branch Prunella created, which was also called NewEntry.

Step Four: Prunella creates a pull request in GitHub

Now that her new branch has been pushed up to GitHub, Prunella can submit a pull request to ask that her changes are merged with the master branch.  She does this by going to the GitHub repository and clicking the “New Pull Request” button.  This takes her to the “Compare” page.  She makes sure that the left side of the comparison is the master branch, and then she chooses the NewEntry branch from the branch dropdown.  She can see how the guestBook.txt file has changed; the new line she added is highlighted in green, illustrating the difference between the two files.  (If she had deleted a line, the line she removed would be highlighted in red.)  Finally, she clicks the “Create Pull Request” button.

Step Five: Prunella’s pull request is approved and merged

The final step in the file change process is that the owner of the repository (or any teammates who have approval permissions) will review the change, approve it, and merge it.  Now if Prunella changes to the master branch by doing git checkout master, pulls down the changes by doing git pull origin master, and takes a look at guestBook.txt file, she will see that her entry has been added:

Kristin Jackvony was here on May 11, 2019
Prunella Prunewhip was here on May 13, 2019

And that’s all there is to it!  In my next post, I’ll add a few more Git tips and tricks, but these steps should be enough to get you started with committing your own code to your team’s repository.

10 thoughts on “A Gentle Introduction to Git

Comments are closed.