Git and Version Control Basics

In this article, I’m going to talk about the basics of Git and version control using GitHub. This is going to be for anybody who has never used git or GitHub before.

What are the prerequisites?

The only prerequisites that are required are just your laptop, a working internet and a basic understanding of command line. Don’t get scared if you have never used command line that before, all you need to know to get going is just knowing two commands: a command to navigate through your directories (folders) in the terminal and a command to list items in a directory (folders).

With the following examples, you should be able to understand these commands.

C:\Users\pc>
C:\Users\pc>cd desktop
C:\Users\pc\Desktop>cd coding
C:\Users\pc\Desktop\Coding>

The cd “folder name” helps us to navigate through our folders. In this case, I’m in the directory (folder) “Coding”, which is my working folder. To go one step backwards, we use cd .. as in the example below.

C:\Users\pc\Desktop\Coding>cd ..
C:\Users\pc\Desktop>cd ..
C:\Users\pc>

As you can see, we have gone back to the directory “pc”.

Using the “dir” command lists all the files that is in that particular folder for you. In some other terminal like VS code, using “ls” command also works fine.

The last bonus command to know is “cls”, cls command lets you clear your cmd line screen. With this three command line commands, we can go ahead with git and GitHub.

Some terminologies to know

What is git? What is GitHub?

Git is a version control system that is used for: tracking code changes, tracking who made the changes and also code collaboration.

GitHub is just a platform. It’s used for hosting codes and projects for your versions control and collaboration purposes from anywhere in the world.

What is Version Control (VC)?

This is also known as Source control. It’s the practice of tracking and managing changes to files.

What is a Version Control System (VCS)?

VCS are software tools that help someone or teams to manage changes to source codes over time. Examples are Git, Mercurial, GitLab etc. Git being the most popular version control system is use.

To get started with git…

Step 1. We need to go to https://git-scm.com/downloads to download git and install git.

Step 2. After installing git, we can check if git is properly installed by running this command git --version If it’s properly installed, it will display the git version that’s installed on your PC.

Step 3. Let’s configure git. It’s just kind of letting git know who you are. It will help to track who made changes whenever there’s any change. We do this using the code below.

git config --global user.name "SochiCodes"
git config --glabal user.email youremail@gmail.com

Step 4. To make sure that everything is fine, we run this code git config --global –list which will tell us that everything is fine by displaying our username and email we just configured. This username and email always show or goes with every commit message that we make.

Now that we have configured git, let’s go ahead and look at how we can use git to track our code base or project.

Step 1. Let’s create a folder, then create a html file inside it. Example, index.html

Step 2. Navigate to the directory (folder) in your text editor or just open the folder, right click and select “open in VS code” or whatever text editor you are using.

Step 3. Let’s initialize the folder by typing the command git init. What does this do? This converts the folder into a new git repository. This makes it trackable unlike the rest of the folders on our computer.

When you do “ls” you will notice that there’s a new file called “.gitignore” that has been added to the folder.

Some basic useful git commands and their functions

1. git status – This command tells us the status of git at every point in time. It tells us which files that are being tracked and not been tracked. Tracked files are those files in the git folder that git monitors what happens to them. For instance, if the file was modified, committed or even deleted. Untracked files are files that are in the git folder but are not monitored.

2. git add “file name”– This command adds file that git will track its changes. It’s called staging. If you make any changes to a file or creates new file in the folder, you still need to run this command again to stage it. Assuming we have quite large number of files we modified; it’s going to be very tedious to start staging them one after the other. To stage all files at once, we use the command git add . This stages all the files at once.

3. git commit -m “your commit message here”– This command simply saves our progress so far. After adding or staging our files, we can save the progress with git commit. We also accompany it with a commit message, for instance if we change the header of a html file, we can save our progress or do a git commit as follows git commit -m “changed the header”. This is very important because since git is a version control system, we can go back to any of our commit in the feature. Assuming we want to undo the modification we did on the header, we can simply go back to the commit.

4. git branch “branch name” – This command helps to create a new branch in git. Branching is import because it helps us not to mess up the main or master source code. For instance, we designed a landing page with a video background in the hero section, we can create a new branch in git and create another landing page with just a background image, this way, our main design is not messed up with. We can also use this command git checkout -b “branch name” to create a branch. This difference with the former is that this creates the branch and takes you to it immediately.

5. git branch – This list all the branches

6. git checkout “branch name” – This command lets you switch between branches listed.

Since we have known basic git commands, let’s go ahead and see how to combine git and GitHub. This is basically pushing our code to GitHub and store it there and we can also see all the changes and modifications we do with git in real time.

We can do more on GitHub, like creating branches like we did with git, writing commit messages, collaborating, forking, creating pull requests and even merging codes.

Step 1. Go to https://github.com, register to have an account if you are new.

Step 2. Click on “New” button by the left corner to create a new repository. Enter your repo name, example, “my-portfolio”. Leave every other field at their default settings and click on the “create repository” button.

The next thing is to push our existing repository we have on our PC to the one we created on GitHub from the command line.

Step 1. Go back to your working repo, and run these git commands

  • git remote add origin “Your Repo GitHub Link” –This command basically links the repository we created on GitHub to the one we have been working with on our local computer.
  • git remote -v – this shows you all the remote links that are available. This command is necessary because you can set up more than one remote links. For instance, you can have another remote link from Bitbucket or GitLab etc.

  • git branch -M main – this renames your default master branch name from master to main. When you initialize a git, the default branch name it comes with is ‘master’. To avoid conflicts with GitHub, GitHub wants it changes to ‘main’.

  • git push -u origin main – This command pushes your code from your local system to GitHub to make your code hosted there. After pushing your codes, go back to your GitHub account, you will notice the changes there and your codes and files are visible.

Git Pull

Git pull command is used to fetch and download content from a remote repository and immediately update the local repository. What this means is that if there are any changes on the repository on GitHub, running git pull origin main will download the new changes and update our local repo on our PC.

You can also pull to a particular branch with the command git pull origin “branch name”

Pull Request

Pull request is basically requesting for your code to be merged with another specific branch online of the same repository you are working on. You are requesting for your code to pulled into another branch or the same repository.

Git Cloning

This is just having or creating a copy of a repository or another repository or a specific branch on your system. For instance, you are working with someone on project and you are not the one that created the repository, in other to have that repo, you need to have a copy of it – cloning.

To clone, we use this command git clone “GitHub Repo Link Here”. This will create a copy of all the repo on your local system. A cloned repo does not require initialization.

Git/GitHub Good Practice

It’s always good not to work directly from the main branch to avoid messing up with it. The best is to create a branch and merge later with the main branch. The main branch should always be clean and deployable.

Conclusion

This article did not cover everything about git and GitHub. As a matter of fact, there are many important git commands that were not mentioned here which are worthy of knowing. It's required that one should further his or her knowledge to mastering git and GitHub to a deeper level. But the good news is that this basic introduction is enough to get you started.