Advance Git & GitHub for DevOps Engineers

In the world of software development, Git and GitHub have become essential tools for version control and collaborative coding. In this introduction, we will explore some advanced concepts in Git and GitHub, including branching, merging, reverting, resetting, and rebasing.

and also you can check my previous Git and Github blogs: -

link1: - https://gopalgtm.hashnode.dev/day8-basic-git-github-for-devops-engineers

link2: - https://gopalgtm.hashnode.dev/deep-dive-in-git-github-for-devops-engineers

🔹Git Branching

Branching is a fundamental concept in Git that enables developers to isolate their development work without impacting other branches in the repository. When you create a new branch, it essentially acts as a separate workspace where you can work on new features, bug fixes, or experiment with new ideas without affecting the main codebase.

Each Git repository has one default branch, often named 'main' or 'master', which serves as the primary branch. Additionally, you can create multiple other branches that diverge from the default branch to work on specific tasks or changes.

Branching is particularly useful when you want to work on a new feature or fix a bug without disrupting the stable version of your project. It allows multiple team members to collaborate concurrently on different aspects of the project without interfering with each other's work.

Once you have made the necessary changes and completed your work in a branch, you can then merge it back into the main branch using a pull request. A pull request is a request to merge the changes from one branch into another. This process enables code review and ensures that the changes are well-tested and ready to be incorporated into the main codebase.

In summary, branches in Git provide a structured and organized approach to development, allowing you to work on different tasks in separate environments while maintaining a clean and manageable version history. By effectively utilizing branches, you can streamline your development workflow and collaborate efficiently with your team.

🔹Git Revert and Reset

Git Revert and Reset are two important commands in Git that allow you to undo changes in your repository. Let's explain them in a non-technical way:

  1. Git Revert: Think of Git Revert as a way to undo a specific commit, without removing it entirely from the commit history. It's like using an "undo" button for a specific change. When you use Git Revert, Git creates a new commit that undoes the changes introduced by the specified commit, effectively canceling out its effects.

Imagine you have a project with a series of commits, and you realize that one of the commits introduced a bug. Instead of deleting the entire commit and potentially losing other changes, you can use Git Revert to create a new commit that negates the changes made by the buggy commit.

Git Revert Command:

git revert <commit>
  1. Git Reset: Git Reset, on the other hand, is more like a "time machine" that allows you to move the current branch to a specific commit in the past. When you use Git Reset, you're essentially resetting the current branch's pointer to the specified commit, discarding any commits that come after it. This can be a powerful tool, but it should be used with caution as it can lead to data loss.

Imagine you made a series of commits, but later realized that the last few commits were unnecessary and caused issues. You can use Git Reset to move the branch back to a previous commit, effectively removing the unwanted commits from the branch history.

Git Reset Command:

git reset <commit>

🔹Git Rebase and Merge

Git Rebase: -Imagine you are working on a team project and you have your own branch to develop a new feature. Meanwhile, other team members are also working on their branches to add new features or fix bugs. As time goes on, your branch might become outdated because the main branch (let's call it "main") has received new changes from other team members.

Here's where Git Rebase comes in. It allows you to update your branch with the latest changes from the main branch in a more organized way compared to traditional merging.

Example:

  1. You create a branch called "my-feature" and start working on your changes.

  2. Meanwhile, your teammate makes changes to the main branch and pushes them to the remote repository.

  3. To update your branch with the latest changes, you can use Git Rebase.

git checkout my-feature   # Switch to your branch
git rebase main           # Update your branch with changes from main

Git Rebase will take all the changes you made in your "my-feature" branch and apply them on top of the latest changes from the "main" branch. It replays your commits one by one, making it appear as if you developed your feature directly on top of the latest main branch.

The advantage of Git Rebase is that it keeps your commit history clean and linear, making it easier to understand the development timeline. On the other hand, traditional merging can create multiple merge commits, making the commit history more cluttered.

However, it's essential to use Git Rebase carefully, especially when collaborating with other team members. If you rebase a branch that others are also working on, it can cause conflicts that need to be resolved.

In summary, Git Rebase is a useful way to integrate changes from one branch to another while keeping a clean and organized commit history. It's a powerful tool that allows developers to collaborate effectively and maintain a clear record of changes in their project.

Git Merge: - Git Merge is a command in Git that allows you to combine changes from one branch into another branch. It is commonly used to integrate new features or bug fixes developed in separate branches back into the main branch of a project.

Imagine you and your friend are working on a team project, and you both have your own branches to work on different parts of the project. Your friend's branch is called "friend-feature," and yours is called "my-feature."

Once you and your friend have completed your work, you may want to combine the changes from both branches to have a complete and up-to-date version of the project. Here's where Git Merge comes in.

Example:

  1. Your friend has finished working on their "friend-feature" branch and pushed their changes to the remote repository.

  2. You want to integrate your changes from the "my-feature" branch into the main branch.

git checkout main         # Switch to the main branch
git merge my-feature      # Merge changes from my-feature into main

Git Merge will take all the changes you made in your "my-feature" branch and combine them with the latest changes from the main branch. It creates a new commit that represents the merge of both branches.

After the merge, your main branch will now have the changes from both your "my-feature" branch and your friend's "friend-feature" branch. This allows you to have a single, unified version of the project with all the new features and bug fixes.

Git Merge is useful when multiple developers are working on different aspects of a project simultaneously. It allows them to bring their changes together and create a cohesive final product.

However, it's essential to resolve any conflicts that may arise during the merge. Conflicts happen when Git cannot automatically combine changes from different branches and requires manual intervention to decide which changes to keep.

Example:

Creating the repository: -

clone the project: -

git clone <project-url>

Create a text file named "version01.txt" inside the "Devops/Git/" directory and add the content "This is the first feature of our application":

mkdir -p DevOps/Git
echo "This is the first feature of our application" > Devops/Git/version01.txt

Commit the changes with the message "Added new feature":

git add Devops/Git/version01.txt
git commit -m "Added new feature"

Create a new branch "dev" from the "master" branch and switch to it:

git checkout -b dev

Push the changes to the remote repository for review:

git push origin dev

Add new content to "version01.txt":

echo "1st line>> This is the bug fix in development branch" >> Devops/Git/version01.txt
echo "2nd line>> This is gadbad code" >> Devops/Git/version01.txt
echo "3rd line>> This feature will gadbad everything from now." >> Devops/Git/version01.txt

Commit each change with appropriate messages:

git add Devops/Git/version01.txt
git commit -m "Added feature2 in development branch"
git commit -m "Added feature3 in development branch"
git commit -m "Added feature4 in development branch"

To restore the file to a previous version where the content is "This is the bug fix in development branch", you can use git reset:

git reset --hard <commit>

here are examples of merging and rebasing two branches, main and dev, in Git:

Example for Merging: -

Step 1: Merging the dev branch into main

git checkout main 
echo "This is a commit in the main branch" >> sample.txt
git add .
git commit -m "create sample.txt file"

Step 2: switch to the dev branch

git checkout dev
echo "hey guys" >> demo.txt
git add demo.txt
git commit -m "create demo.txt file"

Step 3: Merge the dev branch into main

git switch main
git merge dev

Example for Rebasing: -

Step 1: Merging the dev branch into main

git checkout main 
echo "this is sample.txt file" >> sample.txt
git add .
git commit -m "some changes in sample.txt"

Step 2: switch to the dev branch

git checkout dev
echo "hey" >> demo.txt
git add demo.txt
git commit -m "some changes in demo.txt"

Step 3: Merge the dev branch into main

git switch main
git rebase dev