What is Git?

What is Git?

- Simple Explanation

Git is a version control system.

That is a correct definition but I want to share my experience as a beginner and my understanding of Git. At the end of this post I'll give my own definition so read on.

I first started using Git from the command line(after installing Git), on my first try I was super amazed how Git made having multiple versions of a project super easy. I learned it with the analogy of a rocket and a staging area.

To use git, open the Command Line and navigate to your project folder. To initialize Git run the command

git init

Which creates an empty repository for your project.

Then add files to the repository by first placing them on a staging area, like placing the files you want Git to take a snapshot of in a rocket that has not been launched.

git add .

which adds all your project files to the staging area. Make sure not to run this command in the root directory of your system as this would all your system files to the staging area and well... you don't want to ever do that. You can also use

git add filename.text

to add specific files to the staging area.

Then finally send the files in the staging area to the repository with

git commit -m ' Commit message '

where 'Commit message' is the label or human-readable identifier of what changes were made to the files you are adding to the repository. Each change/snapshot you add to the repository is called a commit At this point in our analogy, the rocket has just been sent to space.

One wonderful part is that you can see a lot of all your commits and the time you made them unlike the good old way of CTRL S. The command to view the log of your commits is

git log

which shows you a list of your commits displayed as the commit messages and the commit ID which is an alphanumeric code(h7h377sh773752) that is generated for each commit.

Git stores and references each commit with an ID which is used to differentiate each commit. Each commit also stores a reference to its parent and that is how it is listed in the log.

Git can help differentiate changes in different commits using their ID's

git diff h7h377sh773752 y5e673782h653

If you made a terrible mistake, you can revert to the most recent stable commit using

git checkout h7h377sh773752

where h7h377sh773752 is the commit ID of the most recent stable version

With Git you can create branches of your project in the repository. Branches are usually created to work on a different aspect of the code while keeping the main stable code untouched.

By default when Git is initialized there is only one branch called 'master' which is the branch that your first commit was added to. To view available branches use

git branch

Branches are created using

git branch branch-name

where "branch-name" is the name of the new branch.

You can move from one branch to another by running the command

git checkout branch-name

which to would change your codebase from your current branch to branch "branch-name"

After work is finished on a certain branch you can add the changes to the stable branch or any other by merging them. First checkout the branch you want to merge into then run the command

git merge  branch-name

where "branch-name" is the branch you want to merge into your current branch. This command adds all the commits from the 'branch-name' into your current branch.

You can get definitions of these commands and much more by using

git help

Here is my definition of Git:

Git is a version control system that helps developers work on the same codebase and keep track of the state of the codebase at any point in development. Git takes snapshots of the project and allows you to revert to any saved state at any time.