How git works?

Before you jump into a git you should know how in general it works. Let's take a practical scenario so that you better understand.

You work for a company where they work on multiple projects. They need a server to store this projects or a space where they can put all of their projects right.

A git is a space or a repo where you can store your source code for a project. A central place on a remote server from where all the developer can access the code.

What is a remote server?

A remote server is nothing but a remote computer. For example, bitbucket server. Bitbucket provides a way to manage git repositories.

Let say that your company uses a bit bucket server to manage their projects. Now the question is how do you create a repository.

You can either use git command line tool to create or manage git repositories or use any web interface or a gui tool to manage git related operations.

How to manage git repository?

You can manage a git repo through different medium followings are some of the examples:

  • using command line
  • using a web app like bitbucket
  • using a gui tool like sourcetree

How to create a new git repository command line?

Let say that you are hired as a new developer you are using a git for very first time. You installed a git command line tool on your mac, linux or windows machine.

If you do not know how to install git on your machine follow the link below:

Install git on Mac/Windows/Linux

You are given a new project by your company. You are told to save your source code on a remote server using a git repository but you don't know how it works yet.

Followings are some of the steps that you do when working with a git:

  • You create a new project folder
  • You initialize a git so that it tracks your project folder changes
  • You add some files to your project and add them to git using a command
  • You are done with your changes and you now have to push your changes to remote server
  • You tell git which remote server and credentials to use for your user
  • Git knows about a remote server
  • You commit all of your work and push your changes to remote server

How to create local git repository?

First of all let's create a new project folder and initialize git locally. Follow the steps below:

# create a new project directory
mkdir new-project

# go to your new project folder
cd new-project

# tell git that this will be our
# project root directory locally
git init

# let's add some of the test files
# add 5 test text files
touch file{1..5}.txt

How to check status of git files?

Alright you created a new project you initialized a new git. Now, your project folder become a local git repository. We added some test files.

Now, we need to know if these files are actually added to our git repository they sit in our project directory however they may not be in our local git repository.

How do we check if they are not in a git repo? To verify this we use git status command. Let's run following git command to check the status of newly created files in our project folder:

git status

Once you run above command you will see following output on your terminal window:

You will see that our files are listed in a red colour under Untracked files: title. It clearly says that these files sits in a project folder however they are not added to our local git repository yet.

How to add files to git repository?

To add these untacked files to our local git repo we need to run following command:

git add *.txt

Git add command adds files to git repo. Now, we have to chek the status again for these files to see if they are actually added to our git repo or not. Let's check the status of these files using below command:

git status

Above command will output something like below:

Now, you will see that all the files are shown in green color under Changes to be committed: title. Git is telling us that our changes have been added to a repo however they are not yet committed.

How to commit git changes?

Now, our files are added however they are not committed which mean they really dont exists in our repo yet until they are fully committed.

Once you commit your changes your files will be added to git repo. To commit a change you need to run following git command:

# git commit take -m option
# to add a message related to your
# current commit. It is a good practise
# to always add a message to describe your changes
git commit -m "my first git changes"

When you run above command you will see following output:

Now, if you check the status of our git repo you will see no changes. Try following command to check the status of files

git status

Above command will now output something like below:

It gives you a message like nothing to commit, working directory clean

At this point, you have added all of your project files to local git repository. You committed all of your changes to a git repository.

What did you learn today?

You may not know but you gain some skills regarding git by following our tutorial. Some of the skills are below:

  • you now know what is git
  • you know that git can be local or remote repository
  • you now know how to work with local git repository
  • you now know how to add files to git local repo
  • you now know how to check the status of a git local repo
  • you now know how to commit changes to git local repo

In next tutorial, we will learn about how to add our local git repo to remote server.

Related Posts