Day 10 of #90DaysOfDevOps

Day 10 of #90DaysOfDevOps

Git Branching

In Git, a branch is a new/separate version of the main repository. Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. You can even switch between branches and work on different projects without them interfering with each other. Branching in Git is very lightweight and fast!

git branch <new-branch> - creates a branch name <new-branch>

git checkout <new-branch> - Switches branch to <new-branch>

git checkout -b <new-branch> - Creates a new branch named <new-branch> and switches branch to <new-branch>

Git Revert and Reset

revert is the command we use when we want to take a previous commit and add it as a new commit, keeping the log intact.

git revert <commit-ID> - It reverts back to the previous commit having commit ID <commit-ID> with a new commit ID.

reset is the command we use when we want to move the repository back to a previous commit, discarding any changes made after that commit.

git reset <commit-ID> - It reverts back to the previous commit having commit ID <commit-ID>. It removes all the changes made after the specified commit-ID.

But, we can undo the git reset if we have the latest commit-ID before running the reset at first place.

What Is Git Rebase?

Rebasing is the process of combining or moving a sequence of commits on top of a new base commit.

Git rebase is a command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.

Lightbox

What Is Git Merge?

Git marge will help to combine the changes from two or more branches into a single branch. Developers will work on different branches to improve code or to develop the code after completion we can merge them into a single version of the code.

git merge <name of the branch to be merged>

1. Fast-Forward Merging

Fast forward merge happens when the tip of the current branch (“dev” in our case) is a direct ancestor of the target branch (“main” in our case). Here instead of actually merging the two branches git simply moves the current branch tip up to the target branch tip. Fast-forward merge is not possible if the branches have diverged. Then we need a 3-way merge which uses a dedicated commit to merge two histories or you can say branches.

Lightbox

2. Three-Way Merging

When the base branch has changed since the branch was first created, this kind of merging takes place. Git in this situation generates a fresh merging commit that incorporates the modifications from both branches. Git compares the modifications made to both branches with those made to the base branch using a three-way merge process. Following that, it integrates both sets of changes into a single new commit.

Lightbox

create a branch named "dev" and create a file and commit.

Push the commit to remote repo.

Create pull request and review & merge the PR.

Add multiple commit in dev branch as shown below

Git reset to hash(10ae626) and observe the behavior of reset.

Undo the reset.

Demonstration of the concept of branches with 2 branches with screenshot.

git merge command

git rebase command