AN INTRODUCTION TO VERSION CONTROL
Whether you are working alone or with a team, there is need for you to track the changes you make on a project. This is commonly referred to as version control or source control.
We are going to use Git — for obvious reasons as our case study to learn some version control concepts.
You need to install git on your machine. To do so, you can head to the official website to download the client for your operating system. Windows, Linux based operating systems and Mac OS are all supported.
Before setting out to use git, you need to configure it on your machine. There are 2 important attributes that you need to set — username and email. To configure your system, you can issue the following commands.
git config --global user.name '<username>'git config --global user.email '<email>'
Cloning a repository
Cloning a repository is the process of pulling a remote copy of a repository into your local machine. By a remote copy, we mean a repository that is hosted in code repositories like Github or Bitbucket.
To clone a repository, you need to pass the command shown below on your CLI.
git clone https://github.com/<username>/<repository_name>.git
Note that you can only clone your own repositories and those that are marked as public.
Creating a Repository
A repository contains all your project files and keeps track of all the changes made on the files.
The first step is to create a repository on Github — the remote version.
Click the green ‘New’ button and follow the steps to create a repository.
We also need to create a repository on our local machine. You can do so by issuing the command below on your CLI
The above command initializes an empty repository on your local machine. A folder called .git is created. We shall discuss more about this folder in another blog.
Staging a file means that you are preparing it for a commit. To stage a file, use the command below.
git add filename.filename-extension
This will stage on the specified file. However, if you want to stage all files in the current directory, you can issue the command below:
git add .
In the spirit of version control, it is important to tell what happened in a certain stage. This is achieved by committing the staged files. Your commit message needs to be descriptive.
To commit your changes, you can type in the following command.
git commit -m "a descriptive commit message goes here"
Add Origin — Optional
To this point, we are working on our repository locally. If we want to push our code to a remote repository, the local copy needs to know where to push to. This is what is referred to as an origin.
This step is only necessary if an origin has not been added yet. You can have many origins to push to — in which case you will have to specify where you are pushing to.
To add an origin, key in the following command
git remote add origin https://github.com/<username>/<reponame>.git
Push Code To Remote Repository
The final step is to push your code to the remote repository. To do so, key in the following command.
However, for the first push, you might need to be explicit. You might do something like this:
git push -u origin <branchname>
Mostly, your main branch could be tagged as main or master. We will cover more about branches in another tutorial.
By following these steps, your local repository will be synchronized with your remote repository.
We will cover more advanced version control concepts like branching in the next blogs in this series.