Merge conflicts and learn how to resolve them in git.

Merge conflicts

In this article, it gives knowledge about how merge conflicts happen and learn how to resolve merge conflicts using different methods and tools. To resolve merge conflicts we will be using tools and commands in git.

So, what is a Merge conflict? Merge Conflict is a warning when we try to merge branches if the same files are altered in both branches.
For instance, assume you’re working on an HTML file for web development. You use a master branch to have a mainline, and test branch to work without disturbing the master branch. Here, you have a file called index.html, you altered it in the test branch and commit it. You also altered the master branch’s index.html file. When you try to merge the test branch with master, it shows a conflicting message as git suggest you solve it to merge.
This is the same when two developers in a team try to alter files and merge it into the master branch. At, this time it shows merge conflict.

How to resolve a merge conflict

I have illustrated the images for reference please verify it.
Here, I have a directory called git-merge-conflict-demo with file content index.html.
Firstly initialize the git repository.

$ git init

Add the files to the git repository.

$ git add .

Commit the files to the git repository.

$ git commit -m "First commit"

git-merge-conflict-1

Then create the branch called test.

$ git branch test

Navigate to the test branch and change the contents using the nano text editor.

$ git checkout test
$ nano index.html

Then add the file and commit to the git repository.

$ git add . 
$ git commit -m "First test commit"

git-merge-conflict-2

Navigate to the master branch and now change the contents of the index.html file. Add it and commit it to the master branch.

$ git checkout master
$ nano index.html

Alter some contents in index.html, then add and commit it to the master branch.

$ git add . 
$ git commit -m "Altered commit in master"

Now try to merge the test branch with the master branch. It shows the conflict, auto-merge failed.

$ git merge test
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and the commit the result.

Try the status command to show unmerged files and changes. Now, try the diff command it shows the difference between the two files as the following image.
git-merge-conflict-3

How to resolve the merge conflict

1. The direct way to resolve is to clean-up the merge working tree by manual. Abort the merge option using the command

$ git merge --abort

2. Use the git mergetool such a vimdiff, araxis, vimdiff2. mergetool are the different graphical tools that can be used to point out the difference in two files and help to resolve it. The image shows the vimdiff2 tool

$ git mergetool --tool=vimdiff2

mergetool

3. Use the diff command to show the difference between the files in the MERGE_HEAD version.

$ git diff

diff-command

The contents above ======= are contents in our side or current branch. The contents below to it are on the other side. The text in red is the altered content. This is the same as in the mergetool option.

4. Use Status command to show the unmerged files and change contents.

$ git status

5. Use log -- merge -p the command to show log files and changed contents.

$ git log --merge -p

log-command

Use above all these methods use them as a guide to change the content of the files and then merge the files.

Learn how to branch in git -> Go.
Click here for Git archives.

To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.

 982 total views

Git Merging and learn how to merge in git

Git Merging

Though Git Branching is a powerful feature to branch the development work without changing the mainline, to add the features from one branch to another we need of this Git Merging, yet another powerful feature. Git Merging feature by merging the one development work of a branch to another branch it currently resides. This post help you to understand, how to git merge works and merge files in git from one branch to another.

Git Merge working

In Git Merging, we used the main command

  • $ git merge

The merge command will merge the given name branch to the current branch. When a merge command is given, a new snapshot is created in the current branch. Now, the pointer points to both previous snapshots of the current branch and another branch.

Here, we continue from the previous Git branching post(if not read it Git Branching). Assume, you’re currently in the ‘test’ branch. You made some improvements to the ‘test’ branch. Now, you want to add those features to the ‘master’ branches. First, navigate to the ‘master’ branch by the following command

$ git checkout master

Now, you moved to the ‘master’ branch. To merge the files to the ‘master’ branch from the ‘test’, type

$ git merge test

After this, it shows the following image

git-merge-work

The updating of checksums shows in the first line, the type of merge in the second line, the files merged and altered in next and changes in the last.

git-merging

The illustration describes that snapshot s6 from the ‘test’ branch is merged to the s5 ‘master’ branch as s7 snapshot. Now, the pointer heads to s5(master) and s6(test). This is how the basically merging works in git.

Problems with merging

When we try to merge a file, which is altered in both branches, it makes some conflicts with the merging and it shows git asks us to clear the conflict first. This looks like

merge-conflict

Here, I altered a Readme.md files both in master and test branches. When I try to merge it, it shows the ‘CONFLICT’ message.

Learn how to resolve this merge conflicts -> Go.

Learn how to branch in git -> Go.
Click here for Git archives.

To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.

 922 total views

Git Branching and learn how to branch

Git branching

Every version control system has a branching system. Branching is a powerful feature in which it does not disturb the main or actual pipeline. For instance, we can branch the development into main and test, in the main branch actual working content is developed, whereas in the test branch the features to be added can be developed and can be merged with the main branch. Git also supports this branching and merging feature.

Many version control systems make this feature in a heavy way by copying all content from one branch to another. This leads to a high load. But, git makes it in a lightweight process by snapshots(Already mentioned in the post – Git introduction please refer it). It snapshots the content using checksums. This makes git a lightweight and powerful version control system.

branch command

In git, a new branch is created using branch command. The default branch in all git repositories is main branch. The branch command simply creates a new branch only, do not navigate to the branch.

Here, we create a new branch called the ‘test’ branch by the following command.

$ git branch test

To view, the branches execute the log command that shows an arrow specified with the branch name. The log shows the name of the branch currently the pointer present and created.

$ git log

git-branch1

checkout command

To navigate to the branch, use the checkout command. This command takes control of another branch by taking a snapshot of the’master’ branch.

$ git checkout test

Now check out the log, the arrow changes to the test branch.

git-branch2

Now, you can able to work on the test branch freely, without disturbing the main content altering. If checkout is an extra process, use -b option with branch command. It will automatically navigate to target when a new branch is created.

The above picture illustrates how branching is done. Here, the ‘s’ stands for a snapshot. When the branch command is given with the name ‘test’, it creates a branch called ‘test’. Here s3 is the snapshot of the master and pointer points to master snapshot. When checkout command with ‘test’ given, it navigate to test branch. When some alteration made in the ‘test’ branch, commit it. This creates a new snapshot called s4 in the ‘test’ branch.

Edit and commit until the feature is ready. If your features are ready, to make the ‘test’ developments to the ‘master’ branch we must do merging of the branches, this will be available in the next tutorial.

Click here for Git archives.

To view official git documentation please visit git docs.

 662 total views

How to get remote repository and commit files in git

Git commands that are commonly used for managing project files such as ‘init’, ‘add’, ‘commit’, ‘status’, ‘log’.

 424 total views

Git commands

The last Git article illustrates how the git version control system works. This article will help you to learn how to get a remote git repository and commit files in git. You also get knowledge about git basic commands for remote repositories and commit files in git repository as local.

Getting a git repository

To get an existing git repository either from the local system or online repositories such as Github, bitbucket we can use the ‘clone’ command.
The format for getting a repository is as follows

$ git clone 

For example, consider the following syntax

$ git clone https://github.com/example.git

Initializing a repository

Initializing a repository refers to initialize the repository which is needed to be git i.e to be tracked. This is done using the ‘init’ command.To initialize a repository use the following command

$ git init

Go to the folder you need to be tracked and execute the ‘init’ command.
This creates a new file called .git the file that is where the git keeps of the files that are stored in it in the form of encryption.

Next, we need to stage the files that need to be tracked. This is done using ‘add’ command. The staging files refer to set the which files in the repositories are needed to be tracked in the git.
To stage a file

$ git add README.md

Instead of README.md replace with the file name needed to be staged.
To stage all files specify ‘.’ to add all files in the repository.

$ git add .

Next, we need to commit the files that was staged using the ‘commit’ command.

$ git commit

To commit the files we need to specify commit messages for all commits using ‘-m or –message’ specification in ‘commit’ command.

$ git commit -m 'Added new files'

Tracking a repository

To track the file status we can use ‘status’ in the git command.

$ git status

The status will show you the changes that are changed if any as follows.

On branch master
Changes not staged for commit:
    (use "git add ..." to update what will be committed)
    (use "git checkout -- ..." to discard changes in working directory)
        modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")

If no changes are made in the files it displays the message as follows

On branch master
nothing to commit, working tree clean

To view the log of the files committed to the git we can use the ‘log’ command.
This log command will give you all the logs about the commit done to the files in git.

$ git log

This will output the log as follows

commit 9222b1d10ed1741a2e7ddde50a264a729dc9c85d (HEAD -> master)
Author: AjeethT 
Date:   Thu Jul 16 15:48:54 2020 +0530

The first line gives the commit hash value next it specifies the branch as a master. Next are the author’s name and the timestamp of the commit.

The full flow of the git operation are as follows:

$ git init
$ git add README.md
$ git commit -m 'Initial commit'
$ git status

In the next git post, I will be sharing on different operations on the git repository.
Click here for Git archives.
To view official git documentation please visit git docs.

As I’m new to the blog, I trying to improve the contents daily. Please support it through comments.
If the article is helpful or any thought please comment on me.

 424 total views

Git and Version control system introduction

Introduction to the git version control tool and basic operations in the git tool.

 403 total views

Git

Git is an open-source version control system used by millions of users over the world. A version control system is used to track every change we do to a project. This system keeps track of every file changes done accordingly to the time. These changes can be rollbacked whenever needed from the repositories. Linus Torvalds, the person who developed the Linux kernel is also the reason to build this Git version control system.

There are three types of version control system

1. Local VCS – In the local version control system, version control is done within the local computer we use. It only saves the changes in the files over time as a version within the local computer. It saves the changes in the localhost.

2. Centralized version control system – Has the name specifies, there is a centralized hub that is used to store the files including the file changes. The central hub stores the file details and the snapshots over time. We can revert the files when needed from the central repository.

3. Distributed version control system – As in the form of distributed computers, we can have a distributed version control system in which the version control system is set on every computer connected over the network. The file changes are distributed over the servers and they can be retrieved at any time.

These repositories can be a local machine or server machine. An opensource distributed Git platform is called GitHub. By this, we can store and control our opensource projects in a distributed manner. This is also helpful in contributing millions of developers over the world for opensource projects successful. The other platforms are called Bitbucket, SourceForge, etc. Among them, Git is popular due to the following features:

  1. Open-source project.
  2. It tracks files like the snapshots in virtual machines.
  3. It gives security by means of checksum all the files using the SHA1 algorithm.
  4. It uses easy strategies to store the files in three stages as stages modified, staged, committed.
Git-working
Working of git

Git installation

Install git on Linux OS

All linux system comes with all the Linux kernel
To install git on Linux distros

Debian system – using apt

sudo apt-get update
sudo apt-get install git

Fedora system – using yum

sudo yum install git

Install git on Windows OS

Like Linux, windows don’t come with git
To install git on windows system

  1. Download the git installer from here.
  2. Select the installer and then give next and setup git first.
  3. Next, we need to configure it by using Command prompt.
  4. Run the following commands in the cmd and give your user_name and email_id

git config --global user.name "user_name"
git config --global user.email "email_id"

Install git on Mac OS

To install git on Mac OS we use brew package manager

Using the brew package manager

brew install git
After successful installation of git check it by checking version using below command
git --version
It will return the following output according to the version number
git version 2.9.2
To get help type
git --help

 403 total views