Data Smith

The Data Blog

Musings and ramblings on a variety of topics.

Git for Beginners

Basic git workflows, for git beginners.

Greg Smith

4 minute read

For the uninitiated and even the experienced git has a whole raft of confusing commands. The purpose of this article is to provide you with just enough git to do what we need. The purposes of this example I will be using a very simple private repo I created on my personal github account called “test-branching”. The basic workflow involves cloning a repository from https://github.com and then making some additions, deletions and modifications on your local laptop before eventually commiting those changes back to https://github.com. The is targetted at the single developer, who just wants to save what they have done in a structured way at https://github.com

Why github? github is the largest and most well known online git repository and with the introduction of free private and public repositories, I really don’t see any reason for a lone developer or small team to use anything else.

Cloning

on your local laptop find a directory and issue the following command:

git clone https://github.com/gregory-smith/test-branching

Cloning into 'test-branching'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

This will create a folder called “test-branching” containing the contents of the test-branching repository from github.com.

Track Changes

Modify the files in the test-branching directory freely, adding and modifying files and directories, all changes will be tracked. to see what has changed use the following command:

git status

On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

users1-folder/

no changes added to commit (use "git add" and/or "git commit -a")

Create a commit

Think of a commit has a chunk of changes that you want to group together. To create a commit, add or possibly remove (rm)) some files and directories that you want to group into a commit. e.g.

git add README.md
git add users1-folder/

See how the status command reflects that these have now been added to a commit. Also note that when we added the directory it included all the files in the directory.

git status


On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

modified:   README.md
new file:   users1-folder/junk.sh

Next we need to give the commit a suitable description. To keep things simple I usually just provide all description in the command line arguments.

git commit -m "updated README and added a script"

See how the status changes

git status

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Save the commit

Once we have a commited change the final step is to save this commit back to github.com. In git terminology this is known as a push. Looking at the feedback provided by git throughout this article you will see the words origin/master. Origin is the default remote destination that is created when we cloned the repository. Master is the default branch that is created when we create a repository. You can see this information more clearly if you use the command git remote -v, but for now think that we also work on the ‘master’ branch and our repository in github is always referenced as ‘origin’.

So let’s push a commit back to github:

git push origin master

Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 515 bytes | 73.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/gregory-smith/test-branching
   24bc528..44089aa  master -> master

And that is it, if you look at your github repository in your browser you will see the new commit. For reference the following commands are really all you need to start developing and using git.

git clone
git add
git rm
git status
git commit -m
git push origin master

Check my other articles for more advanced uses, like multiple users working on the same repository and an introduction to branching.

Recent posts

About

This is my personal blog space that I use to write about the many different things that interest me. To find out a little more about me click on the link