Advance Git & GitHub for DevOps Engineers: Part-1

Advance Git & GitHub for DevOps Engineers: Part-1

What is Git Branching?

Git branching is a feature in the version control system Git that allows developers to create independent lines of development, known as branches, from the main codebase.

Each branch contains a copy of the codebase, and developers can make changes to the code without affecting the main branch. This allows multiple developers to work on different features or bug fixes simultaneously without interfering with each other's work.

Once the changes on a branch are complete, developers can merge the changes back into the main branch. Git makes it easy to switch between branches and track changes made to each branch.

In addition to creating branches for new features, developers may also create branches for bug fixes or experimentation. Git branching is a powerful feature that enables developers to work more efficiently and collaboratively on software projects.

What is Git Revert and Reset?

Git Revert and Reset are two different ways to undo changes made to a Git repository.

Git Revert is used to undo changes made to a commit by creating a new commit that undoes the changes. It leaves a history of the original commit and the revert commit. This means that the changes made in the original commit are still in the Git history, but they are not present in the current version of the code. This can be useful when the changes made in a commit need to be undone, but the commit history needs to be preserved.

Git Reset, on the other hand, is used to undo changes made to a branch by resetting the branch to a previous commit. This removes the changes made in the commits after the specified commit and can be used to discard changes that were made in error or that are no longer needed. This is a more aggressive way to undo changes as it completely removes the changes from the Git history, and it can lead to data loss if not used carefully.

In general, Git Revert should be used when the commit history needs to be preserved, and Git Reset should be used when the changes need to be completely removed from the Git history. It's important to use these commands with care and to make sure that any changes being made are fully understood before executing them, as they can have significant consequences on the Git repository.

Git Rebase and Merge

What Is Git Rebase?

Git rebase is a Git command that allows developers to integrate changes from one branch into another by applying the changes of a branch onto the base of another branch.

The rebase command works by moving the changes made in a branch to a new base commit, which is usually the head of another branch. This makes it look like the changes were made on top of the other branch all along, creating a linear history of commits.

Rebasing can be used to keep the Git history clean and organized, by avoiding the creation of unnecessary merge commits. It also makes it easier to see the progression of changes over time, since the commits are displayed in a linear order.

However, it's important to note that rebasing can be a destructive operation since it rewrites the Git history. This can cause conflicts if multiple developers are working on the same branch and trying to rebase their changes. It's important to use rebasing carefully and to communicate with other developers before using it to avoid conflicts.

What is Git Merge?

Git merge is a command that allows developers to combine changes from multiple branches into a single branch.

The merge command works by taking the changes from one branch and applying them to another branch. If there are any conflicts between the changes made in the two branches, Git will prompt the user to resolve the conflicts before completing the merge.

Merging can be used to combine different branches that have been created for different purposes, such as features, bug fixes, or experimentation, into a single branch that contains all the changes. It's a useful way to keep the Git history organized and to ensure that all changes are applied to the final version of the code.

One downside of merging is that it creates a new commit that records the merge itself. This can clutter the Git history and make it harder to follow the progression of changes over time. To avoid this, developers can use Git rebase instead of merge, which creates a linear history of commits.

In summary, Git merge is a command that allows developers to combine changes from multiple branches into a single branch. It's a useful way to keep the Git history organized, but it can create clutter in the history by creating merge commits.

Tasks:

Make a branch dev from main, using git checkout -b dev, switch to dev branch.

Add a text file called version01.txt inside the repository "DevOps" in dev branch and write “This is the first feature of our application”

Commit the change and push it to your remote repository.

Now, Let's commit our code a couple of times after a couple of changes:

2nd line>> This is the bug fix development branch.

Commit with a message “ Added feature2 in development branch”

3rd line>> This is a code with added mistakes.

Commit with a message “ Added feature3 in development branch

4th line>> Mistakes, Mistakes and Mistakes.

Commit with a message “ Added feature4 in development branch

git log --oneline

As the last 2 commits had mistakes in their code, let's restore our application to a previous version which was running without mistakes.

We will use git revert command to move back to a previous safe version which had no bugs or mistakes.

git revert -m "message" <desired_commit_ID>

Merge dev branch in main

Thank you for reading. I hope you found value from this blog.
Happy Learning!