Day #10 : Advance Git & GitHub for DevOps Engineers.

Shefali MishraShefali Mishra
3 min read

Introduction

Git is a powerful version control system used by developers to manage and track changes in their code. One of the key features of Git is branching, which allows developers to work on different features, fixes, or experiments without affecting the main codebase. In this article, we will explore the concept of Git branching and some common Git commands like revert, reset, rebase, and merge.

Git Branching

Creating a New Branch

A branch in Git is essentially a lightweight pointer to a specific commit, and it allows developers to isolate their development work from other branches. The default branch in a Git repository is usually the "master" branch, but you can create multiple other branches as needed.

To create a new branch in Git, you can use the git checkout -b <branch_name> command.

For example, if you want to create a new branch called "dev" based on the "master" branch, you can use the following command:

git checkout -b dev

This command will switch you to the "dev" branch.

Making Commits in a Branch

Once you are on a branch, you can make changes to your code and commit them. For instance, if you add a text file called "version01.txt" with the content "This is the first feature of our application," you can use the following commands:

echo "This is the first feature of our application" > Devops/Git/version01.txt 
git add Devops/Git/version01.txt 
git commit -m "Added new feature"

Pushing Changes to Remote Repository

After committing your changes in the local repository, you can push them to the remote repository to make them available for review. You can use the git push command to do this:

git push origin dev

This command will push the changes in the "dev" branch to the remote repository.

Reverting Changes

If you need to revert changes in a branch to a previous state, you can use the git revert or git reset commands. These commands allow you to remove or edit changes made in previous commits.

For example, to revert changes in the "dev" branch to a previous version, you can use the git revert command:

git log # Note the commit hash you want to revert to git revert <commit_hash>

Git Rebase and Merge

Git Rebase

Git rebase is a command that lets you integrate changes from one branch into another. One of the key differences between rebase and merge is that rebase modifies the commit history. It's designed to overcome some of the shortcomings of merging, especially regarding commit logs.

Git Merge

Git merge, on the other hand, is a command that allows you to combine the changes from one branch into another. The significant advantage of merge is that it preserves the original commit logs of the branches being merged.

Demonstrating Branches

Let's now demonstrate the concept of branches with screenshots:

  1. Create two branches, "dev" and "feature," and add some changes to the "dev" branch.

  2. Merge the "dev" branch into the "master" branch.

  3. Experiment with the git rebase command to see the difference it makes in the commit history.

Task 2:

For Task 2, you can demonstrate the concept of branches as follows:

Create a new branch from 'master' and make some changes in the 'dev' branch.

git checkout -b dev2 
# Make changes to files in the 'dev2' branch.

Merge the 'dev2' branch into 'master.'

git checkout master git merge dev2

In conclusion, Git branching is a fundamental concept in version control that allows developers to work on separate features or experiments without affecting the main codebase. Common Git commands like revert, reset, rebase, and merge provide the flexibility needed to manage code changes effectively. Understanding these concepts and commands is essential for effective code collaboration and version control in software development.

0
Subscribe to my newsletter

Read articles from Shefali Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shefali Mishra
Shefali Mishra