Introduction to GIT

Betty
Geek Culture
Published in
6 min readJan 5, 2022

What is GIT, why it is there, what GIT can do and how do we use it.

Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

What is GIT ?

Git is a free and open source Version Control System which can be used for any types of plain text files (not only source code files like .java or .py).

Why do we need Git?

We need Git because we want to:
- keep track of the modifications made to files over time — we can see what has changed, who made changes and when those changes took place;
- be able to revert back to the previous version — what if you made hundreds of changes to different files which turned out to be a completely useless modifications? Isn’t it great that we can switch back to the previous, stable version within seconds?
- compare the differences between versions,
- use branching capabilities — feature branches establish a special environment for each modification we make to our code. A developer creates a new branch whenever he wants to start working on something new, no matter how big or tiny. This ensures that production-quality code is always present in the main branch. This way you are always ready to deploy new version to production — your master (main) branch is untouched.

File lifecycle in GIT

It’s important to remember that each file in your working directory can be tracked or untracked. Files in the latest snapshot, as well as any newly staged files, are tracked. In a nutshell, tracked files are those that Git is aware of.
Everything else is untracked files, which are any files in your working directory that Git does not know about.

Files can be unmodified, modified, staged or committed.

When we modify a file and we are done modifing it (file content has changed), we can save it to our local Git database — in this case, this file becomes a staged file.

We choose the files that need to be tracked by Git while we’re in the working directory. Why are we doing this? Why don’t we keep track of everything that happens in the project? Because various project files, such as class files, or some temporary data files, are dynamically generated and it’s pointless to keep track of these files’ versions. The business logic of the program is contained in the source code files, data files, configuration files, and other project artifacts. These files must be added to the staging area in order to be tracked by Git.

Staging files opens the doors to committing them.
We add a commit message, explaining what is our change about. Together with the files, our login is recorded as an author of the commit and the time is of course registered as well. Now, a snapshot of the files in the commit is recorded by Git.

If we want to send our changes to the remote repository, we do the action called pushing a commit to a Remote Repository. From this moment on, other users can see our changes.

States of a Git project

a) Working Directory — the project is in this state if the files has not yet been staged.

b) Staging Area (Index) — here you find your modified files after staging them.

c) Repository (.git directory) — last state, you have committed and recorded your files.

Installing GIT

Download link for:

Configuring GIT

After downloading and installing a package, you can enter the following phrase to verify your installation:

git --version

This should print the version of Git that you have installed.

Next, you can set your email and username (this is important because every Git commit uses this information) using the following commands:

git config --global user.name “Betty ABC”

git config --global user.email “betty.abc@gmail.com”

You may wonder about the ‘global’ parameter. As name suggests, adding this parameter will make sure that your configuration will apply to all projects (write to global ~/.gitconfig file rather than the repository .git/config). You may want not to have this global settings but specific for a project instead. In this case the following commands would come handier:

git config user.name “Betty ABC”

git config user.email “betty.abc@gmail.com”

How to check your current configuration in GIT

If you want to check your configuration settings, you can use the git config --list command to list all the settings Git has.

Example output:

$ git config --list
user.name=Betty ABC
user.email=betty.abc@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...

Code Hosting Provider

When you want to start sharing your code — even if it’s just with yourself on another machine — hosting it becomes a critical consideration.

Using Code Hosting Provider like GitHub or Bitbucket means your repository is no longer only on your local device — it gives you ability to access your code from other devices and makes the door open to collaboration.

Creatting an account on GitHub is just as easy as creating email account. Click here if you want to make one for yourself: https://github.com/join

How to create new repository on GitHub

Once you have your account verified, you can log in at GitHub and open the plus menu like on my screenshot. Select “New repository”.

This is what you will see after the first step:

Choose the repository name, add description, you can choose between public and private repository and there are some options for initializing your project as well.
Once done, click ‘Create repository’.
You’re all set!

You will se the following page after creating your first repository:

The default option to connect to your repository is HTTP but SSH is also possible.
There are ready commands listed for the following purposes:

  • creating new repository on the command line,
  • pushing a repository that you already have on your local machine, from the command line,
  • importing the code from another repository.

Let’s explore some commonly happening actions while working with Git.

How to save changes with a commit message

Assuming you’ve edited some information in the current branch mystory.txt file and are willing to commit it to the project history. You must first use git add to stage the file, after which you can commit the staged snapshot.

$ git add mystory.txt# git status is used to inspect the status of your action.
$ git status
On branch main
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: mystory.txt
$ git commit
# This will open a prompt message asking you for a commit message

How to push commit to the remote repository

Pushing is the process of sending commits from a local repository to a remote repository. You can achieve that using the following command:

$ git push origin master

How to update from the remote repository

If you want to get the changes from a remote repository and update your current repository use the following command:

$ git pull

(The git pull command is actually a combination of two commands: git fetchfollowed by git merge)

How to view commit history

In order to see commit logs, execute the following command:

$ git log

How to revert/undo your last commit

Maybe you have introduced a buggy commit and you want to apply inverse of a commit instead of manually coming back to the previous state?
There is a command for it:

$ git revert HEAD

This revert command works in a specific way. Once executed, it will figure a way to get your repository to the previous state but without loosing a history, which is very important in order to do collaboration in a reliable manner.

If you do want to remove all traces from the last commit and never see it again you can do the following:

$ git reset --hard HEAD~1
# Because of using --hard, files are reset to their state at previous commit and changes are not kept.
$ git reset --soft HEAD^
#Because of using --soft, we will keep our changes

If you want to keep on editing files but just revert command commit:

git reset HEAD~1

That’s it! I hope my article was helpful!

--

--