Found 56 Articles for Git

What is merge conflict in Git? How to handle merge conflicts?

Kannan Sudhakaran
Updated on 30-Apr-2021 08:01:14

714 Views

In real world, when we merge branches, we will run into conflicts quite often. Conflict happens because of the following reasons −When the same line of code is changed in different ways in two branches.A given file is changed in one branch but deleted in another branch.Same file is added twice in two different branches but the content of the file is different.In these cases, git will stop the merge process as it cannot figure out how to merge the changes. In such scenarios we need to intervene manually and instruct how to proceed with the merging process.The diagram given ... Read More

How to view merged and unmerged branches in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 07:48:08

6K+ Views

When we have multiple branches in a git repository, we would need to bring the changes from all the branches to the main line of work that is the master branch. So, if we are currently in master branch and need to see which branches need to be merged, we can use the following commands.$git branch --no-mergedWe would also need to verify which branches are already merged, so that we can delete the unused branches$git branch --mergedExampleLet us create an example to understand how to view the branches that are merged and unmerged. The following diagram shows that there are ... Read More

How to disable fast forward merges? What are its pros and cons in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 07:46:06

1K+ Views

Fast forward merges can be disabled −At the time of mergingFor the entire repositoryFor all repositories.Disabling fast forward merges has both pros and cons.When we disable fast forward merge, git will perform a merge commit to merge the changes from both the branches. The drawback of merge commit is that it becomes hard to read and understand the commit history, especially when we have many branches. If your team prefers to keep a linear history of branches, then one should go for fast forward merge. Disabling fast forward merge will create merge commits, which pollutes the commit history.The benefits of ... Read More

What is 3-way merge or merge commit in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 07:43:00

5K+ Views

Let us look at an example of a 3-way merge. In this example, the Feature branch is two commits ahead of the Master branch.Diagram 1Before we merge it with Master, let us say we have added an additional commit to the Master as shown in the below diagram.Diagram 2Due to the commit performed on the Master branch, both our branches Master and Feature are now diverged.This means we have some changes in the Master branch that is not present in the Feature branch. If we perform a merge in this case, Git cannot move the master pointer towards the Feature ... Read More

What is a fast-forward merge in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 07:34:00

24K+ Views

Fast forward merge can be performed when there is a direct linear path from the source branch to the target branch. In fast-forward merge, git simply moves the source branch pointer to the target branch pointer without creating an extra merge commit.Let us look at an example implementing fast-forward merge.We have a master branch with 3 commits.Next, we create a branch called feature branch. In git a branch is nothing but a pointer to a commit. At this point both feature and master are pointing to the same commit.Now let us switch to the feature branch and do a couple ... Read More

How can we diverge two branches in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 07:23:23

2K+ Views

A branch in git is a series of interrelated commits. If two branches follow a non-linear path then they diverge each other. The diagram shows two diverged branches master and feature.Consider the above diagram. Let us assume that both the branches were following a linear path before the initial commit. After the initial commit, the branch master has an independent commit c1 and the branch feature has its own commit c2. As the branches now follow a non-linear path, we can say that both the branches are diverged. Let us see this through an exampleStep 1 − Create an empty ... Read More

Explain BLOB object and tree object in Git.

kannan sudhakaran
Updated on 20-Feb-2021 09:04:17

1K+ Views

Git uses a series of BLOBs and trees to store content of the working directory of a project. Whenever we perform a commit operation, Git internally creates a series of trees and BLOBs, which is the binary representation of the project folder structure at that point in time of commit.What is BLOB?BLOB stands for Binary Large Object. Each version of a file in Git is represented as a BLOB. A BLOB holds a file’s data but doesn’t contain any metadata about the file or even its name.To understand a BLOB let us see an example.Create 3 files “file1.txt”, “file2.txt” and ... Read More

What is the short status in Git?

kannan sudhakaran
Updated on 20-Feb-2021 09:01:52

2K+ Views

The git status command returns the current state of the −Working areaStaging areaThis command returns tracked and untracked files and changes made to the repository. However, this command does not show any commit records or information. This command usually returns a status message denoting one of the following states −No commit historyUntracked filesChanges pending to be committedClean working treeModified filesDeleted filesThe syntax for using the git status command is given below −$ git statusA screenshot of the above command’s output is below. The output indicates that the file has been modified.dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git status On branch master ... Read More

How do we stash changes in Git?

kannan sudhakaran
Updated on 20-Feb-2021 08:59:46

636 Views

This question can be rephrased as "How to save work in progress (WIP) in Git and return to it later when convenient?"The problem − When we switch branches, Git resets our working directory to contain the snapshot stored in the last commit of the target branch. For example, if we switch from feature to the master branch, Git will replace contents in the working directory with the last commit of the master branch. But if we have local changes in our working directory that we haven't committed yet, these changes will be lost. In this situation, Git will not allow ... Read More

How to compare two branches in Git?

kannan sudhakaran
Updated on 20-Feb-2021 08:57:04

2K+ Views

Collaborators will use multiple branches in order to have clearly separated codebase. At some point in time, we may have to merge these branches in order to have the resulting work in the main branch. It is important that we compare the differences in the branches before merging to avoid any conflicts. We will see a couple of different ways to compare two branches −Listing commit differences − This method shows commits that are present in a branch but unavailable in the other branch.Listing file changes − This method compares branches and displays how exactly a certain file is different ... Read More

Advertisements