One of the easiest ways to ruin your interview as a developer is not being able to give a basic explanation of what Git is to your interviewer or know how to use it. Huge companies, as well as solo programmers, need Git for various purposes nowadays, and this article is going to be an introductory guide.
What is Git?
The homepage of Git’s official website defines it as a free and open-source distributed version control system designed to handle everything from small to massive projects with speed and efficiency. Git, in simpler terms, is a tool/system used to monitor and control new versions of your files; it allows you to track changes in an application, single file, or folder over time.
For instance, when you create a new file and put it under version control using Git, you can create versions of the file at various savepoints. If there is an error in the document you created further down the line, and you’re finding it hard to resolve or debug the issue and would rather roll back to a previous version, you can do that using Git.
While you can think of Git as a “Ctrl + Z” for your files and folders, it does more than undoing an action can achieve in Microsoft Word. Unlike pressing the “undo” button in Microsoft Word, Git allows you to choose a specific version of the file that you want to roll back to, and you can do this after restarting your system or even on a different computer.
An essential aspect of programming is the need for collaboration because you would hardly work on and maintain many worthwhile projects on your own. Git makes it possible for multiple developers to work on a single project, track each contributor’s changes, and merge all approved changes into one file.
Local And Remote Use Of Git
Git can be used either locally (offline) or remotely (online); let’s look at how to use Git in each instance:
How To Use Git Locally
A basic understanding of using Bash commands is necessary to use Git locally (except you want to use GUI tools). To explain the processes below more practically, I’ll be working with a directory on my PC known as C:/Users/Clouddevelop/Desktop/using-git. There will be three files in this folder– file-1.txt, file-2.txt and file-3.txt.
Setup Git Locally
To set Git up for local use, you first need to install Git on the computer by going to https://git-scm.com/ and clicking on the download button. The website will automatically detect your Operating System and download the latest version of Git for you. Open the installation file and accept the default conditions to install Git on your system and restart your computer.
The Bash shell is the easiest means of using Git; it is the default shell used on Unix-based systems like Linux and MacOS; however, if you’re a Windows user, likely, you don’t have the Bash shell installed. Click here for a complete guide on setting up the Bash shell for Windows before continuing.
To confirm if you’ve successfully installed Git on your computer, run the following command:
If you have Git installed, you will see the version of Git installed as a response on your CLI.
Create A Local Git Repository
To track the changes made to files in a particular folder on your system, you must create a repository in that location. The steps to do that are as follows:
- On the command line, open the folder you want to track by using the command cd location name; for instance:
- Next, you will need to initialize a git repository in that location by using the following command:
You will see a response that says
Initialized empty Git repository in C:/Users/Clouddevelop/Desktop/using-git
You won’t see this git repository on your file explorer; however, if you use the command
ls -a, you will see the hidden .git folder among the list of folders in the directory. This folder in which you’ve initialized Git is known as the Working Directory.
- To track the changes made to any of the files in the working directory, you need to add it to the Staging Area. The staging area is an intermediate place where you can pick which files you want to commit from your working directory.
To know the files in your working directory that aren’t being tracked, run the command
git status; the untracked files will be marked in red. To start tracking it, you should run the command
git add filename; for example:
git add file-1.txt
If you want to add all the files in your working directory at once, use the
git add . command.
- Next, we need to commit the staged file by using the command below:
git commit -m "A message describing what changes were made on this commit"
The message put in after the -m flag is essential, as it helps you and others working on the project know the critical changes made to this version of the file. The best practice with commit messages is to write them in the present tense, not past tense; for instance, “update navbar”, not “updated navbar”.
If you want to check the commits you made in that directory, use the
git log command.
Revert File Changes To A Previous Version Locally
To reverse a file to a previous version, you will use the git checkout filename command. Below is an example:
git checkout file-1.txt
The above command rolls the file back to its last committed version.
Sometimes you might want to compare the difference between various commits of a file; you can do that by using the
git diff filename command. An example is:
git diff file-1.txt
Both versions of the file will appear, highlighting the differences between each version.
How To Use Git Remotely
While using Git locally is great, using it remotely comes with added benefits, such as:
- The ability to access your files on any computer connected to the internet.
- Sharing files with other users around the world.
- Giving teams the ability to work simultaneously on a particular project.
- Open-source projects can be accessed and updated by developers around the world.
- You can track file changes more efficiently using a Graphical User Interface.
Setup Git Remotely
GitHub is the name of the online service that gives users the ability to save their repositories online. Below are the steps to setup GitHub for remote repositories:
- First, you need to open a new account on GitHub by clicking the Sign Up button on the taskbar and filling in your correct information.
- Next, you need to verify your account by checking your email and following the instructions. Following these steps gives you access to a free GitHub account.
Create A Remote Repository On GitHub
After creating an account, you need to create a remote repository to track changes to your files; the steps to do that are as follows:
- First, you will need to create the repository locally, as explained above.
- Once you’re signed up and signed in, you can create your first remote repository by clicking the + on the taskbar and selecting New Repository on the dropdown.
- Give the repository a name and description, and select if you want your repository to be Public or Private.
By default, GitHub makes every new repository public, meaning it can be accessed, viewed, and copied by other GitHub users. You can only create private repositories if you use a paid version of GitHub.
- If you have a README, you can add it or leave the checkbox unticked.
- Click the Create repository button.
- Next, you need to push the existing local repository to GitHub by first copying the repository URL and staging it on the remote server using the
git remote addcommand. You will see the repository URL in an input box at the top of the screen; an example of the command is:
git remote add origin https://github.com/nabsteve/clouddevelop.git
- Finally, you would need to transfer or push the staged files into the remote repository using the following commands:
git branch -M main git push -u origin main
The word “main” stands for the particular branch we’re pushing the files to in the above command. A single repository can have multiple branches, but it is best practice to make your initial commit to the main or master branch of the repository. So, the first command switched to the main branch, and the second pushed the files into that main branch.
- Pushing the files to the remote repository will take time, depending on how large the files you’re pushing to the remote repository are. Adding the
-uflag as we did above allows you to link your local repository to the remote. Because the remote and local repositories are now connected, we only need to push whatever new changes we make to the remote repository.
Revert File Changes To A Previous Version Remotely
If you want to revert the changes made to a file using a version that you committed on GitHub, you need to follow the steps below:
- Open the remote repository URL on your web browser and locate the file whose changes you want to revert.
- Next, you need to copy the file’s location; ensure you start after the first forward slash, as seen in the screenshot below.
- Click on History, and you will see the various versions of the file. Copy the commit ID of the version to which you want to revert.
- Run the
git checkoutcommand adding the two details you just copied, like this–
git checkout commitID -- file location. For example:
git checkout a005b87e1de8aa4b305c23eef6ff4e69d96a439f -- index.js
- Finally, you need to commit the change you just made; for example:
git commit -m reverted to previous version