Found 1383 Articles for Open Source

How to undo a faulty merge with reset command in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 08:22:06

2K+ Views

Let us say we performed a merge commit and later found out that the code is not compiling or our application is not working. This happens if we make mistakes while merging. In situations like these we need to undo the merge using either −reset command; ORrevert commandThe git reset command can be used to undo local changes to the state of a Git repository. A git reset moves the HEAD pointer to a given commit and updates the index to match that commit. This command rewrites the commit history. However, if we have shared our commits with other team ... Read More

Explain soft reset with an example in Git

Kannan Sudhakaran
Updated on 30-Apr-2021 08:18:54

729 Views

Soft reset will move the HEAD pointer to the commit specified. This will not reset the staging area or the working directory.ExampleThe diagram shows a file named File1.txt within the git repository. A, B, C and D represent lines that are added to the file. The diagram indicates that a commit is performed after adding each line A, B and C. c1 is the commit performed after adding line A, c2 is the commit after adding line B and C3 represents the commit after adding line C.Now add line D. This change is available in the working directory and this ... Read More

Explain mixed reset with an example in Git

Kannan Sudhakaran
Updated on 30-Apr-2021 08:15:25

204 Views

Mixed reset will move the HEAD pointer to the commit specified. This is the default reset option in git. It also copies content of the commit snapshot to the staging area and not to the working directory. This will lead to overwriting the staging area. The working directory contents will be safe in this case. Let us understand this through an example.ExampleThe diagram shows a file named File1.txt within the git repository. A, B, C and D represent lines that are added to the file. The diagram indicates that a commit is performed after adding each line A, B and ... Read More

Explain hard reset with an example in Git

Kannan Sudhakaran
Updated on 29-Apr-2021 11:19:45

140 Views

The diagram shows a file named File1.txt within the git repository. A, B, C and D represent lines that are added to the file. The diagram indicates that a commit is performed after adding each line A, B and C. c1 is the commit performed after adding line A, c2 is the commit after adding line B and C3 represents the commit after adding line C. Now add line D. This change is available in the working directory and this change is staged but yet to be committed.Now if we perform a hard reset to move the HEAD pointer of ... Read More

Explain how reset command works in Git

Kannan Sudhakaran
Updated on 30-Apr-2021 08:11:58

814 Views

The commit command moves the HEAD of a branch implicitly. The below diagram shows that initially HEAD was pointing to commit c1. After each commit operation the HEAD pointer moves ahead to the new commit. We can perform a reset using the HEAD pointer or commit hash.The git reset command will explicitly or forcibly move the HEAD of the branch to a specific commit.When resetting the HEAD pointer, we have 3 options −SoftMixedHardHard ResetWhen performing a hard reset, git will copy the commit snapshot into the working area as well as the staging area. Due to this any changes done ... Read More

How to abort a merge conflict in Git?

Kannan Sudhakaran
Updated on 30-Apr-2021 08:03:28

2K+ Views

When doing a merge, you may run into one or more conflicts. Now what if we are not quite ready to handle this conflict yet. Perhaps you have several conflicts and you don't have enough time to spend on resolving these conflicts. In situations like this we can easily go back to the state before we started the merge.To abort the merge, we can use the following command$ git merge --abortNote that if we execute this command after resolving some conflicts and staging the changes, then these changes would not be saved. Once the command is fired, we are back ... Read More

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

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

762 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

7K+ 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

2K+ 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

Advertisements