At one point, as a developer, you would have heard of Git. If you write codes or build solutions, either on a small or big scale, Git is a useful tool.
What is GIT?
GIT is an Open Source Distributed Version Control System that tracks changes in a set of files. Let me break down this definition.
- It is Open Source because it is accessible to everyone – anyone can see, modify, or distribute the code.
- Git is a Control System as you can use it for source code management. It tracks the changes made to the contents of your files.
- A Version Control System because it provides a record of what has changed in your source code. It stores each change as a version and makes it easy to switch from one version to another.
- As Distributed Systems, the codes are not stored in one place. There’s a remote version on a server and a local copy on your systems. When working with teams, each developer has a personal copy of the code locally, and the combined project will be on the remote repository server.
WHY GIT?
Backup: This is the first advantage that comes to mind. When your codes are on a server, they can be easily retrieved in case of data loss or a system change. Imagine something goes wrong with your laptop, or you got a new one. Instead of copying the files from one system to another, you can easily pull your code from Git and continue working.
Multiple Development Ease: When more than one developer is working on a project, Git eases the development process. Each developer has his codes on his local system and can push to the git repository. It shows you who pushed what and at what time.
Version Control: Each time you push to git, a different version of your app is stored. You can always revert files to a previous state. You can review new changes made by another developer, create issues (a kind of to-do list for your project), and see who wrote this and that.
Create Code Bases: Using Git’s branches, you can create several code bases. You can branch out of an existing project and create a new app.
How to Use GIT
You can use Git via different Hosting Services. The most popular are GitHub, GitLab, Bitbucket. You can also combine two or more services in one project. For this lesson, I will be using GitHub. Whatever service you use, the git commands remain the same.
Step One: Installation
You need to install git to use it. Git comes preinstalled on most Mac and Linux systems. To confirm Git is installed, fire up your terminal/command prompt and run:
$ git --version
git version 2.17.1
As seen, this command prints the version of my installed git. What if you don’t have git installed? If you’re on Mac, this command will start the installation process automatically. If you are running Linux, run the code below on your terminal.
$ sudo apt install git-all
You will be prompted on the amount of space that will be used during installation – make sure to enter ‘Y’ to continue the installation. If you are on windows, download the latest version of git on GitforWindows. You can run git –version again after installation to see the version installed.
For more on installing Git, visit GitHub’s Install Git Guide.
Step One and a Half: Create a GitHub Account
If you do not have a GitHub account, create one here.
Step Two: Set your Global Variables
This step tells git who you are. You would add your username and email address to git’s global configuration. Still on your terminal, add the following.
$ git config --global user.name "your_username"
$ git config --global user.email "your_email@email.com"
$ # The username and email address should correlate with your github account details
$ #To confirm the values supplied, run
$ git config --global --list
user.name=your_username
user.email=your_email@email.com
Step Three: Get your project on git
When you want to push your project to Git, there are two possible scenarios. It could be:
- A totally new project: You haven’t written any line of codes, or
- An existing local project: You have existing codes that should be pushed.
In both cases, the steps are quite similar.
A New Project
Step A: Create a repository on GitHub. You can do that here.
You can add a project description and a README file while creating the new repo. When your repo has been created, take note of the repo link; this will be used later.
Step B: Initialize Git
Now, you need to create a local repository. You can simply create a folder in your preferred directory. Open this folder in your terminal.
$ mkdir project_git #creates a new folder called project_git
$ cd project_git #changes the directory to the newly created one
~/project_git$
The git init command initializes an empty local git repository
$ git init
Initialized empty Git repository in /home/maryam/project_git/.git/
Step C: Adding a remote origin
git init added an empty local git repository. We need to add the remote repository to the path as well. To do that, we use the git remote add origin command. The remote repository URL can be copied directly from GitHub.
$ #git remote add origin <remote_repository_URL>
$ git remote add origin https://github.com/maryam-olabisi/git_simplified.git
To confirm the remote has been added, run:
$ git remote -v
The command lists all the remote URL that are linked to that path. Note that this step can also come after steps D & E.
Step D: Add Files to Staging Area
Now you can start creating your code files in the folder. For the sake of this tutorial, let’s add one or two files. If you didn’t add a README file in Step A, you could do that now. We can also add another file app.py
After creating and editing these files, we need to add them to the staging area. To do that, we use the git add command.
$ git add README.md #adds the specified file
$ git add . #adds all the files
Before we go to the next step, let us check the status of the added files.
$ git status #lists all the new and staged files to be committed
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: app.py
The first line says you’re on the master branch – which is the default branch for all git repos. I will explain branches better later. The second line says you haven’t made a commit yet. The next line shows the files that you’ve staged that are about to be committed.
Step E: Commit your staged files
When you use the git commit command, you need to provide a commit message. This message is intended to give an explanation of what you did.
$ git commit -m "My first commit"
[master (root-commit) f77a777] My first commit
2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 app.py
The response gives a summary of what you committed: 2 files (README.md and app.py), two insertions (because I added only one line each to the files – two lines in total).
Step F: Push Changes
This step updates you remote repository with the changes you committed. To do this, run the git push command.
$ git push origin master #master is the main branch as explained in Step D
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 295 bytes | 295.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/maryam-olabisi/git_simplified.git
* [new branch] master -> master
The terminal will ask you to provide your GitHub password before pushing. Type in your password and hit Enter. After the successful push, your GitHub repo is updated. On refresh, you have:
The added files have the commit messages “My first commit” as added in the previous step. When you make further changes to your local repository, all you need to do is a git add ., git commit -m “message”, and git push origin master. That simple.
Existing Code on Local Machine
Now, what if you have existing code on your local machine that has not been pushed? The steps are almost the same. Do Step A – create a repository on GitHub. Then, skip B and continue with C. Open the folder that contains your code in the terminal and add the remote URL of your new repo.
$ git remote add origin https://github.com/maryam-olabisi/git_simplified.git
Then continue as in Steps D, E & F. git add ., git commit -m “message”, git push origin master. As in the first case, your files would be available on GitHub after a successful push.
And that’s it! This article covers getting familiar with Git. In part 2 of this series, I will cover git clone, branches and merges.