Let's get started on using Git
Now that I have introduced you to the concepts of git, it is time for us to have a look at examples of how to use git in practice, we will start by downloading and installing git
The link below has details on how to install Git in multiple operating systems:
Git is a command line tool, I will be testing it in my terminal, an app on a mac for running commands, all example codes below are to be pasted in your terminal command line.
Verify if Git is installed by with the following command in your terminal:
Create a project directory on your desktop called git-demo and initialize git within that directory
# change directory into your desktop
# make directory called git-demo
# change into git-demo directory
# initialize git
When we run git init, it initializes a repository within the git-demo directory called .git this is where git will save all snapshots along with other information git requires
Let's Add Code To Commit
Save the code below into git-demo directory as index.html
git status tells us we have a new file inde.html which we have not yet commited, now we can go ahead and commit that work.
Before we create a snapshot of commit we first need to stage the work that we want to commit, example below shows how we stage the index.html file
If we had multiple files to stage we could do it in two different ways, one would be to list the files we want to stage as follows:
Another option for staging multiple files would be to use a (.) notation, this will go ahead and commit every file you have changed.
With our work staged ready for commit, we can run the status command again to see that our work is staged
Now we are ready to commit our work, this will create the snapshot, making it possible for us to revert back to this snapshot at any point in the future.
git commit -m'added an index.html'
The above command will create our commit, it also adds a commit message 'added an index.html' this makes it possible for us to track this commit
With git log we can inspect a list of changes that have been commited
By default git work is done on a branch called Master Branch, that is where our commit from above was recorded, to view list of branches you type:
Let's go ahead and create a new branch, we will then commit some changes against our new branch:
The above command has created a new branch called about-page, we can go ahead and view the list of branches with the git branch command, notice there is a star? that indicates which branch we are currently pointing to.
Now what we want to do is point our currently working directory to our newly created branch, to do that we use the following command:
Another check on our branch list should reveal that we are now pointing to our new branch
From now on, what ever new code we write and commit, will be recorded against our new branch, let's see that in action, below is the code for creating an about-us.html file
Now let's go ahead and stage this work
The next phase is to go ahead and commit this work
With the work now commited, we have created a new snapshot in about-us branch, if we checkout back into our master branch, you will notice that the about-us.html file will be missing. Let's see how we checkout back into master branch:
The above command has changed out working directory to point back to our master branch, what ever files were commited to the about-us branch will not be visible here
Now that we have checkedout our master ( meaning that we are now pointing to master branch) we can merge in our changes from the about-us page:
The above command will take all the commits from about-us branch and replay them on the master branch, now the about-us.html file has been added to our working directory
If we go ahead and check the branches we will notice that we still have the about-us branch
Deleting a Git Branch
When a branch is merged into other branch or no longer needed, you can delete it using the following command:
git branch -d about-us
A further check of our branch lists with the command below should now reveal that we only have our master branch
If we check our commit log we will notice that we now have both commits, one was created in the master branch and the other when we were working from the about-us branch.
An added bonus, you can also view your commits as single lines, this comes in handy when you have lots of commits to inspect:
git log --oneline
You have just gone through some of the processes of how to use git, version controlling your work, keeping a history of changes, creating branches and learnt examples of how to change from one branch to another.
Git is a fantastic tool to add to your skill set and this article has barely scratched the surface, I advice you to visit the git documention to learn even more as well as see some of the video courses I have created on the subject.