A Practical Guide to Using Git

Photo by Yancy Min on Unsplash

This post will show you how to use Git using some practical examples. Git is a very commonly used tool, and you should know how to use it. Don’t worry; Git is super easy to use!

This post will highlight a few practical examples when using Git. But first, let’s get into the basics of Git before we look into the practical examples. By the way! Don’t confuse GitHub with git. GitHub is a website for hosting projects that use git. While git is a version control system.

What is Git?

Git is a Distributed Version Control System (VCS) that allows you to track changes on code, supports many developers working on the same code at the same time and enables code to be merged easily.

You have what is called a “local” and “remote” repository. The “local” repository (or repo for short), has all the information that the remote repository has. Also, you can sync your local repository with the remote repository when you want, and vice versa. Furthermore, there can be many local repositories, one for each developer that is working on the same code.

Git has three stages that you need to know about:

  1. Working directory
  2. Staging area
  3. Committed files

The untracked (new) and modified files will be in the working directory area. We run our Git commands in the working directory.

The staging area is where we organize what we want to be committed to our repository. The reason for this area is so that we pick and choose what we want to be committed.

See the basic diagram below to visualize the stages and repositories working together:

Note that there is much more to Git than is shown in the diagram.

Download & Install Git

Visit https://git-scm.com/downloads to see how you can download and install Git on Windows or Linux/Unix or Mac OS X. The list below only show the instructions for Ubuntu, Fedora and Alpine, but Git supports many more Operating Systems.

For Ubuntu

The latest stable version for your release of Debian/Ubuntu:

apt-get install git

For Fedora

Up to Fedora 21:

yum install git

Fedora 22 and later:

dnf install git

For Alpine Linux

apk add git

After installation, you can check the version number by running the following command:

git --version

Configure Git

We have to set some global configuration variables after we have installed Git. These variables are important because they help identify you as the developer. So this is a way to add your name to all the changes you have made.

The syntax for these variables is:

git config --global user.name "joe soap"
git config --global user.email joe@soap.com

Instead of using joe soap, you can use your name and instead of using my email, you have to use your email. All other keywords are part of these commands and will be written as it is.

After putting in those values we can check those values by:

git config --list

Result:

user.name=joe soap
user.email=joe@soap.com

How to get help for Git Commands?

Now, we will see how we can get help for any command. You can use two ways to get help. Let’s get the help for the Git config command:

git help config

Or

git config --help

Instead of the config command, you can also get help about any other command like:

git add --help

First scenario – Existing project on your local machine

Now, you will see the first scenario in which you can initialize a repository from an existing code. In this case, you have a local code base that you want to start tracking using Git.

Now, go to the root folder of your project and run the following command to check the files and folders you want to track using Git:

ls -la

Result:

drwxr-xr-x 3 root root 5 Sep 19 15:31 .
drwxr-xr-x 3 root root 3 Sep 19 15:30 ..
-rw-r--r-- 1 root root 0 Sep 19 15:31 linux.txt
-rw-r--r-- 1 root root 0 Sep 19 15:31 personal_info.txt
drwxr-xr-x 2 root root 2 Sep 19 15:31 project

As you can see, the project that we want to track exists in the folder named as the git_project. This folder contains two text files which are linux.txt and personal_info.txt. It also contains a folder named as the project. Now, let us start tracking this project using the following command:

git init

This command will initialize an empty Git repository which can be checked by running the following command:

ls -la

As you run this command, you will see the .git directory. Moreover, you will also see all the files and directories in this folder that you are going to track.

Now, we are using Git in our directory, but we haven’t committed anything yet. So before we commit anything, run the following command:

git status

This command will show all the untracked files in your current working directory. Let’s see the working of the above commands in the terminal:

root@local:/home/user/git_project# git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	linux.txt
	personal_info.txt

nothing added to commit but untracked files present (use "git add" to track)

What is the .gitignore file?

Now, before we move on, let’s see what the .gitignore file is. There are some files in our project that we do not want other people to see or keep track of these files.

These files might be your files about the project that you do not want to share with the developers involved in this project. So, we would like to ignore these files, and to do that first we will create a .gitignore file

touch .gitignore

The .gitignore file is a simple text file where we can add files that we want git to ignore. Let’s say we do not want other developers to see the personal_info.txt file. So we will add the name of this file to the .gitignore file.

Now, open up the .gitignore file and add the following filename:

personal_info.txt

You can also ignore files and folders using patterns such as:

  • *.log – Match anything except a slash
  • docs.* – Match any file or folder whose name begins with docs

Note: Git will not ignore a file until you un-track it with the following command:

git rm --cached name-of-the-file

You can un-track many files at once using the following command. Just remember to commit everything you’ve changed before you do this.

git rm -rf --cached .

Now, that we have added the personal_info.txt file to the .gitignore file, let’s check its status:

git status

When you run this command you will not see the personal_info.txt file:

root@local:/home/user/git_project# git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.gitignore
	linux.txt

nothing added to commit but untracked files present (use "git add" to track)

As you can see, it did not show the personal_info.txt file because we have ignored it. It will only show the .gitignore file and other files that we want to track.

How to Add to Git?

Now, let’s add files to the staging area. So, if we want to add all the files that are currently untracked or that we made changes to, to the staging area, then we can use the following command:

git add -A

We can also add them individually by a command like this:

git add filename

Now, we can run the Git status command to check if the files have been added to the staging area.

root@local:/home/user/git_project# git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   .gitignore
	new file:   linux.txt

As you can see, it gives a message like: “Changes to be committed”. This means that we are in the staging area. Running the Git status command in this area will also show any untracked files if there are any.

Now, if we run the following command, then it would commit these changes to our repository:

git commit -m "my first git commit command"

Results:

root@local:/home/user/git_project# git commit -m "my first git commit command"
[master (root-commit) c52ebba] my first git commit command
 2 files changed, 1 insertion(+)
 create mode 100644 .gitignore
 create mode 100644 linux.txt

Here, the option m stands for the message, and it saves our changes with the given commit message.

How to remove from the staging area?

Let’s create a text file and add it to the staging area.

root@local:/home/user/git_project# touch file.txt
root@local:/home/user/git_project# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	file.txt

nothing added to commit but untracked files present (use "git add" to track)
root@local:/home/user/git_project# git add file.txt
root@local:/home/user/git_project# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   file.txt

As you can see, we have added file.txt to the staging area using the ‘git add’ command.

Now, to remove files from the staging area, we can use the following command:

git reset file.txt

Results:

root@local:/home/user/git_project# git reset file.txt
root@local:/home/user/git_project# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	file.txt

nothing added to commit but untracked files present (use "git add" to track)

So, as you can see, we have removed file.txt from the staging area to the working directory.

If you want to remove everything from the staging area, then you can use the following command:

git reset

Another useful command is the log command:

git log

This command shows all the commits we have made with the hash number of the commits. All those hashes will be unique. It will also show the author, date, and commit messages.

Moreover, you can also stop tracking your project. All you have to do is remove the git directory just like you remove any other directory:

rm -rf .git

This way, you are no longer tracking this project.

Second scenario – A project that exists remotely

Now, let’s see a scenario where we want to track an existing remote project.

Let’s say your company has a remote repository and you want to clone that remote repository and begin developing on top of it. The syntax for cloning the repository is:

git clone URL

Let’s clone a GitHub repo by following this syntax:

git clone https://github.com/CoreyMSchafer/Sample-Repo.git

After cloning this remote repository, let’s see the contents of this repository.

ls -la Sample-Repo
root@local:/home/user/git_project# ls -la Sample-Repo
total 13
drwxr-xr-x 3 root root  5 Sep 20 07:35 .
drwxr-xr-x 3 root root  3 Sep 20 07:35 ..
drwxr-xr-x 8 root root 13 Sep 20 07:35 .git
-rw-r--r-- 1 root root 12 Sep 20 07:35 .gitignore
-rw-r--r-- 1 root root 90 Sep 20 07:35 script.py

Now, let’s see how we can view information about that remote repository using:

root@local:/home/user/git_project# cd Sample-Repo/
root@local:/home/user/git_project/Sample-Repo# git remote -v
origin	https://github.com/CoreyMSchafer/Sample-Repo.git (fetch)
origin	https://github.com/CoreyMSchafer/Sample-Repo.git (push)

This command shows the location of the repository on the internet.

By running the following command you can show all of the branches in the repository, not only locally but remotely as well.

git branch -a

Result:

root@local:/home/user/git_project/Sample-Repo# git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

How to see what changes were made?

Now, we make changes to script.py file in this repository. Now, we will see those changes through  a command:

git diff

Result:

root@local:/home/user/git_project/Sample-Repo# git diff
diff --git a/script.py b/script.py
index 1a9ec4e..9a9cb08 100644
--- a/script.py
+++ b/script.py
@@ -1,5 +1,5 @@
 import requests
 
-r = requests.get("https://coreyms.com")
+r = requests.get("https://anto.online")
 print(r.status_code)
 print(r.ok)

The ‘git diff’ command shows the changes that we have made to the code. While the plus (+) symbol means we have added something while the negative () symbol means we have removed something.

If we run the following command,  it will show us which files have been modified. We’re currently in the working directory.

git status

Now, like before, we can add all the changes to the staging area by the following command:

git add -A

After adding the changes to the staging area, you can run the ‘git status’ command to see the changes that are ready to be committed.

root@local:/home/user/git_project/Sample-Repo# git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   script.py

You can commit these changes by the following command:

git commit -m "added a line to script.py file"
[master 53f0ead] added a line to script.py file
 1 file changed, 1 insertion(+), 1 deletion(-)

You can give any appropriate message for the changes you have made. Now, by running the above command, we have committed the changes locally.

How to push changes to the remote repository?

Now, we want to push the changes we have made to the remote repository, so other developers have access to those.

To do this, we will need to run two commands:

git pull origin master

We run this command because we’re working on a project that could potentially have multiple developers. And people have been pushing code to that remote repository.

While we have been working on our features. So what the ‘git pull’ does is that it will pull any changes that have been made since the last time we pulled from that repository.

root@local:/home/user/git_project/Sample-Repo# git pull origin master
From https://github.com/CoreyMSchafer/Sample-Repo
 * branch            master     -> FETCH_HEAD
Already up to date.

And, as you can see it says we are Already up-to-date because there have been no changes to that remote repository since the last time we pulled from it. And now you can run the following command to push those changes:

git push origin master

origin here is the name of our remote repository, and master is the branch that we want to push to.

So, in short, two scenarios have been discussed in this article, one in which you have your own project, and you start tracking it using Git. And, another scenario is the one in which you start tracking the remote project.



About the Authors

Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic!

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



Leave a Reply

Your email address will not be published. Required fields are marked *