Git – Git Outline and How To Use Git

Tram Ho

Original article: An introduction to Git: what it is, and how to use it

In this article, we will learn the most basic knowledge about Git: What is Git? How is Git used?

What is Git?

Git is the Open Source Distributed Version Control System. There are many ways to define Git, here I will break it down and explain the semantics in detail.

  • Control System: Basically, this means that Git is a content tracker, so Git can be used for content storage and mostly used for source hosting. The code is based on the features it offers.
  • Version Control System: The source code stored in Git will change as more new source code is added. Furthermore, many developers can add source code in parallel. The Version Control System helps to deal with this problem by maintaining a history of changes that have occurred. Additionally, Git also provides features such as branching and merging, which will be discussed below.
  • Distributed Version Control System: Git has a remote repository hosted on a server and a local repository stored on each developer’s computer. This means that not only is the source code stored on a central server, but a full copy of that source code is also stored on all developer computers. The remote repository concept and local repository I will explain later in this article.

So, why is it necessary to have a Version Control System like Git?

In fact, projects often have many developers working in parallel. Therefore, a version control system such as Git is necessary to ensure there is no source code conflict among developers.

In addition, the requirement in the project changed frequently. Therefore, the version control system allows developers to revert to older versions of source code.

Finally, it is sometimes also the case that multiple projects running in parallel involve a codebase. In such cases, the concept of branching in Git is extremely important.

Next, let’s learn about how to use Git.

Instead of covering all the concepts at once, I will in turn explain the concepts of Git through concrete examples.

How to use Git

Download Git

The link below describes in detail how to install Git in different operating systems.

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Verify whether Git is installed or not using the following command in the command prompt.

Create a local Git repository

On your computer, create a directory for your project, for example the project directory is simple-git-demo .

Then go to the project directory and add the local Git repository using the following command:

The git init command will add a local Git repository to the project.

Add source code

Next, let’s add a small source code snippet. Here, for example, I create a file called demo.txt in the project directory and add the following text to the file.

Here I am demo with only plain text instead of the real source code, since as mentioned, the main focus of this article is Git, not any specific programming language.

Staging and Committing

Commit is the process in which the source code is added to the local repository. Before committing, the source code must be in the area prepared for the commit.

This section keeps track of all files that will be committed, files not added to this area will not be committed – This gives developers control over which files to commit.

Staging

Use the following command to sort the file into the area prepared for the commit.

In case you want to add more files, you can use the following command.

Alternatively, if you want to add all the files in the project directory to this staging area, use the following command.

Be careful when using this command, because it will add all the files and folders in your project to the Staging area.

Committing

Use the following command to commit the file.

“Initial Commit” here is the message, you can enter a corresponding message to know what source code changes were made in a certain commit.

Git Status and Git Log

Now, modify the demo.txt file and add the following code:

Status

Use git status to see which files have been modified, which files are in the area prepared for the commit – there’s more to it that I will leave out in this article.

Use the following command to view the status:

The status shows that demo.txt has been modified and is not in the area prepared for the commit.

Now, let’s add demo.txt to the Staging area and execute the commit using the following command:

Log

Use git log to show all commits made so far using the following command.

In it, the author of each commit, the commit datetime, the commit message will also be displayed.

Branches

So far, we have not created a branch in Git, and so by default commit goes into the master branch (main branch).

What is a branch?

Branches exactly a pointer to the latest commit in the Git repository. So now our master branch points to the second commit, demo.txt file is modified .

Why need more branches?

The answer is: It takes multiple branches to support multiple parallel developments. For detailed operation of branches refer to the following figure.

First, commit 1 and commit 2 are executed in the Master branch. After commit 2, a new branch called “Test” is created, commit 3 and commit 4 are added to this branch Test.

At the same time, commit 3 and another commit 4 are added to the Master branch. Here we can see that after commit 2, two parallel developments are being performed concurrently in 2 separate branches.

The Test branch and the Master branch separate here, containing different source code – the source code from the Test branch can be merged into the Master branch using the git merge, which I will cover later.

Create a new branch locally

Create a new branch called “Test” using the following command:

Test branch is created. We’re still on the master branch; To switch to the test branch, use the following command:

And now we are in the test branch.

You can list all local branches with the following command:

Do some commits on the new branch

Modify demo.txt by adding the following code:

Now, we will perform staging and commit using the following command:

This commit is executed in the test branch, and the test branch is now 1 commit more than the master branch – because the test branch also includes 2 commits from the main branch.

You can verify the commit history in the test branch as follows:

Merging

Currently, the test branch is 1 commit more than the master branch. Now we want to bring all the source code in the test branch back to the master branch.

In this case, the Git merge will be extremely helpful.

To consolidate the code from the test branch into the primary branch, follow these steps:

First, we will go back to the main branch.

Then run the following merge command:

After running these two commands, branch merging – in theory – should be successful. As shown in the example, no conflicts have occurred.

However, in real world projects, there will be a conflict occurring while merging the branch. Dealing with these conflicts takes a lot of experience, so as you work harder with Git, you will know exactly what to do to handle the conflicts.

Run git log right now, you will see that master also has 3 commits.

Remote Git repository

For now, we are only working on the local repository. Each developer will work with their personal repository but in the end, they have to push the source code into the remote repository.

Once the source code is in the remote repository, other developers can view and modify the source code.

GitHub

We will be using GitHub for the remote repository.

Go to https://github.com/ and create an account.

After registering on the GitHub homepage, click “Start a Project” to create a new Git repository. Name the repository ( git-blog-demo for example) and click “Create Repository”.

This will create a remote repository in GitHub, and when you open the repository, a page like below will display.

The repository URL is the one highlighted in the image above: https://github.com/aditya-sridhar/git-blog-demo.git

To point your local repository to the remote repository, use the following command:

Git Push

To push all the source code from the local repository into the remote repository, we would use the following command:

Then, the source code from the master branch in the local repository is pushed to the master branch in the remote repository.

Additional commands

Git Pull

git pull is used to pull the latest changes from the remote repository into the local repository.

The source code in the remote repository is constantly updated by many different developers, so Git Pull is essential.

Git Clone

git clone is used to clone a remote archive to your computer. The command for this is:

~ The end ~

Share the news now

Source : Viblo