This article has all the version control basics to get you up and running.
The version control software we will use is called Git. For an introduction to version control and Git please see the following article Git: Setup. In this article, we will learn the following commands: git init
, git status
, git add
, git commit
, git log
and git diff
.
Lets use a simple example to demonstrate the basics of Git. First create a directory called "testDirectory". cd
into testDirectory.
Initialise your empty git repository using git init
.
$ git init --> Initializes empty Git repository in mycomp/testDirectory/.git/ $
We can see the hidden directory ".git" by using the ls -a
command. Also we can use the git status
command to check the status of our repository at any time.
$ git init --> Initializes empty Git repository in mycomp/testDirectory/.git/ $ git status On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) $
This tells us that we are on the master branch (we will look at moving to other branches in a later article) and that we currently don't have anything to commit.
Let's add an empty text file "story.txt" to our current directory. Now if we run git status we get a different message.
$ touch story.txt $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) story.txt nothing added to commit but untracked files present (use "git add" to track) $
The status message tells us that story.txt is untracked. In order to track a file we need to add it to the staging area using git add filename.txt
. If we wanted to add all untracked files to the staging area then we can use git add .
.
$ git add story.txt $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached..." to unstage) new file: story.txt $
Adding story.txt to the staging area has not yet saved a version of our file. We also need to commit this change using git commit -m "your commit message here"
. It will become clearer why we have this two stage process when looking at more complex operations. For now lets commit our change.
$ git commit -m "adding a text file" [master (root-commit) f5dc1b4] adding a text file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 story.txt $ git status On branch master nothing to commit, working tree clean $
Great! We have now committed our text file and have a saved snapshot of our directory at this point in time. If I now deleted story.txt I would be able to go to my commit and reinstate the file.
Lets make a change to the file and create a new commit. This is as simple as adding the changed file to the staging area and committing as above. Add the words "Once upon a time" to your story.txt file using a text editor and then do the following.
$ git add story.txt $ git commit -m "added the beginning of the story" [master 88f905c] added the beginning of the story 1 file changed, 1 insertion(+) $
Now we will look at two very handy git commands.
git log
lists all commits in the current git repository.
$ git log commit verylonggitcommitidA (HEAD -> master) Author: YourName YourEmailAddress Date: Tue Jul 24 15:55:51 2018 +0100 added the beginning of the story commit verylonggitcommitidB Author: YourName YourEmailAddress Date: Tue Jul 24 15:50:43 2018 +0100 adding a text file $
For an easier to read git log, we can add these useful arguments: git log --pretty=oneline --abbrev-commit
.
$ git log --pretty=oneline --abbrev-commit shortCommitIdA (HEAD -> master) added the beginning of the story shortCommitIdB adding a text file $
git diff
will show us the difference between our current working directory and the latest git commit. Say we added another line to the text file: "there was a big bad wolf".
$ git diff diff --git a/story.txt b/story.txt index 6cdd5fc..3bfcebb 100644 --- a/story.txt +++ b/story.txt @@ -1 +1,2 @@ -Once upon a time \ No newline at end of file +Once upon a time +there was a big bad wolf \ No newline at end of file $
This tells us that we have removed the line "Once upon a time" and added the lines "Once upon a time" and "there was a big bad wolf". How come Git thinks we removed and readded the first line? This is because I didn't add a newline character at the end of "Once upon a time" in the original commit. You may have done this in yours.