DEV Community

On-cloud7
On-cloud7

Posted on

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

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
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 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
Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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:

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

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

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.
Enter fullscreen mode Exit fullscreen mode

Merge the 'dev2' branch into 'master.'

git checkout master git merge dev2

Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)