Git for Absolute Beginners
If you’re new to the programming world, then learning Git should be something on top of your priority list.
Git is one such tool which you will encounter on a day-to-day basis as part of your job.
What you can expect in this post
In this post, I’ll provide an overview of Git and how to get started with it.
- What is Git?
- Terminologies associated with Git
- Interacting with Git using the command line
I promise to explain the topics in the most simplified way I can.
So let’s begin by understanding, what is Git?
Git is a Version Control System.
Now, what the heck is a Version Control System (VCS)?
A VCS monitors and keeps track of all the changes done to the files that are being monitored by it.
It also allows multiple developers to share and work collaboratively on the same set of files, without conflicting with each other’s work.
It not only keeps track of what files were changed, but it also keeps tracks of
- What changes were done?
- Who did these changes?
- When these changes were done.
For you to share and work collaboratively with other developers, you require access to a Git-based hosted service.
Some of the popular Git hosted service providers are:
All of them offer a similar sort of functionality.
What is a Repository in Git?
A repository is a folder whose contents are tracked by Git. It is also known as a repo, in simple terms.
A repo may have multiple files and sub folders present within it. Usually, the files that are present within the repository contain source code.
Within each repo, there is a .git folder. This folder contains all the files and folders required by Git to keep track of all the changes done to the files within this repo.
If we delete this .git folder, Git will not identify this folder as a repo nor track its contents.
The repo present on the local computer is referred to as a local repository, and the repository located on a hosted Git platform is referred to as a remote repository.
Download and Install Git
Downloading and Installing Git is a fairly straightforward process.
You can download Git from here.
Once Git has been downloaded, you can refer to this guide on how to have it installed.
Initializing a Git Repository
Before we go ahead and start tracking our files using Git, we need to initialize Git for the folder which we want Git to monitor.
In simple terms, Git converts a folder into a repository so that its contents can be tracked by it.
In order to initialize a folder into a Git repository:
On a Windows-based system, we need to right-click on the folder (we would like to be tracked by Git), and then click on “Git Bash Here”. This opens up a command prompt like a window, which allows us to interact with Git using the Git commands.
Note: Whenever we would like to interact with Git, we will be interacting using Git commands through this Git Bash window. Also note, the Git commands do not differ for Windows and Unix based systems.
Within the Git Bash window, we need to type the command:
This command initializes the folder. Basically, it converts this folder into a Git repository.
As part of this initialization process, it also creates a .git folder (which is a hidden folder) within this repository. This contains all the files required by Git to track all the changes done to this repository.
But this is just a normal folder like other folders we have on the system. In Git terminology, we still refer to this as a repository or a local repository, to be more precise.
On a Unix-based system, we just navigate to the directory (which you would like to be tracked by Git), and run the git init command, that’s it. This converts this directory into a Git repository.
At any point in time if we want to see what is being tracked by Git within a repository, we can do that by typing the command below:
We will be looking at this command in more detail at some point later in the post.
For now just remember, if we want to see what is being tracked within a repository by Git, we can do that using that command.
Tracking a Repository
Even though we have initialized the folder as a Git repository, its contents won’t get tracked automatically. We need to instruct Git to monitor its contents.
In order to do that, we make use of the git add command. The syntax for this command is as shown below:
git add file [file] [file..]
Note: Anything enclosed within the square brackets  is optional. This applies to all the Git commands listed within this post.
We can either specify a single file or multiple files to be tracked by Git.
If we want Git to monitor specific files present with the repository, we can do so by specifying the individual filename of each file we would like to track.
In case we want to track files belonging to a specific file type, we can do that by specifying its file extension, as shown below. This tracks all the files ending with the .txt extension.
$ git add *.txt
If we want Git to track all the files present with the respository, the syntax is as shown below.
$ git add .
Let’s say we have the following files present within our repository:
As you can see even the .git folder has been created as part of the initialization process. Originally this folder was hidden — I had to change the folder properties to make it visible (just to show it to you all).
This is how a .git folder looks, immediately after the git init command is executed.
This is how the contents of the .git folder look after a few transactions have been done to the repository.
To check what all files are currently being tracked by Git, we can make use of the git status command:
$ git status On branch master No commits yet Untracked files: (use “git add <file>…” to include in what will be committed) HelloWorld.html Notes.txt README.md nothing added to commit but untracked files present (use “git add” to track)
Looking at the output of the git status command, it indicates that none of the files are currently being tracked by Git.
Let’s go ahead and add these files so that they get tracked by Git.
The command for adding these files is as shown below:
$ git add HelloWorld.html Notes.txt
Now, let’s execute the git status command and check its output.
$ git status On branch master No commits yet Changes to be committed: (use “git rm — cached <file>…” to unstage) new file: HelloWorld.html new file: Notes.txt Untracked files: (use “git add <file>…” to include in what will be committed) README.md
As we can see, we have the
HelloWorld.txt and the
Notes.txt files present within the staging area that are waiting to be committed.
README.md file isn’t being tracked at all, as we didn’t include this file within the git add command which we executed earlier.
When we executed the git add command, Git staged all the files which were specified as part of the input to this command.
Until we commit these files, Git won’t start tracking these files.
Committing Staged Files
Let’s commit these staged files by typing the command shown below.
$ git commit -m ‘Initial Commit’
git commit is the command which is used to commit any staged files, -m is used to specify the comments for this commit operation.
If we would like to view all the commit operations that have been performed, we can do it by typing the git log command, as shown below.
$ git log commit 8525b32ffcb92c731f5d04de7fe285a2d0ebe901 (HEAD -> master) Author: shahzan <firstname.lastname@example.org> Date: Sun Apr 28 01:12:20 2019 +0100 Initial Commit
Whenever any change is done to a file which is being tracked by Git, we need to re-stage those files and re-commit them again. Until those files are not re-staged and re-committed, they will be tracked by Git.
I have done some minor changes to the Notes.txt file, let’s see what Git has got to say about these changes by executing the git status command.
$ git status On branch master Changes not staged for commit: (use “git add <file>…” to update what will be committed) (use “git checkout — <file>…” to discard changes in working directory) modified: Notes.txt Untracked files: (use “git add <file>…” to include in what will be committed) README.md no changes added to commit (use “git add” and/or “git commit -a”)
Looking at the above output block, it is clear that the file
Notes.txt has been modified and the changes are not staged for commit.
We make use of the same git add command to re-stage the file.
$ git add Notes.txt Shahzan@BlackBox MINGW64 /d/Medium Post Pics/Git/Source Code (master) $ git status On branch master Changes to be committed: (use “git reset HEAD <file>…” to unstage) modified: Notes.txt Untracked files: (use “git add <file>…” to include in what will be committed) README.md
As you can notice from the above output block, the file has been staged and is waiting to be committed.
Again, we make use of the same git commit command to re-commit the staged file.
$ git commit -m ‘Notes.txt file updated’ [master 184fcad] Notes.txt file updated 1 file changed, 3 insertions(+), 1 deletion(-)
Let’s execute the git log command and see if the commit has been successful.
$ git log commit 184fcad4185296103cd9dba0da83520399a11383 (HEAD -> master) Author: shahzans <email@example.com> Date: Sun Apr 28 01:15:38 2019 +0100 Notes.txt file updated commit 8525b32ffcb92c731f5d04de7fe285a2d0ebe901 Author: shahzans <firstname.lastname@example.org> Date: Sun Apr 28 01:12:20 2019 +0100 Initial Commit
As you may notice in the above output block, both the commit operations are being displayed.
Within the repository, there may be files that hold sensitive data or log data, which we don’t want to be tracked by Git under any circumstances.
.gitignore is the file within which we can specify all the files we don’t want Git to keep track of.
$ touch .gitignore
The syntax to create this file is as shown above.
Let’s say that I do not want Git to track any file ending with the .md extension.
Before adding *.md to the .gitignore file, have a look at the output of the git status command as shown in the output block below.
$ git status On branch master Untracked files: (use “git add <file>…” to include in what will be committed) .gitignore README.md nothing added to commit but untracked files present (use “git add” to track)
As you may notice, we have
README.md being shown as untracked files.
After adding *.md to the .gitignore file, the git status is as shown in the output block below.
As you may notice, we now just have .gitignore being shown as an untracked file.
$ git status On branch master Untracked files: (use “git add <file>…” to include in what will be committed) .gitignore nothing added to commit but untracked files present (use “git add” to track)
You can either specify an individual filename or a wildcard entry within the .gitignore file.
Git is a very powerful tool and there is a lot more you could do with it, such as branching, merging, pull and push request and much more.
Just in case you’re interested in learning more about Git, here is a course that I would recommend you to enroll (affiliate link).
Before you say goodbye…
Let’s keep in touch, click here to enter your email address (Use this link if the above widget doesn’t show up on your screen).
Thank you so much for taking your precious time to read this post.