📁Day 10 - Advance Git & GitHub for DevOps Engineers: Part - I

📁Day 10 - Advance Git & GitHub for DevOps Engineers: Part - I

🟠 Git Branching

Git branching is like creating different versions of your project. It allows you to work on new features or fix issues without affecting the main or stable version of your code. To create a new branch in Git, you can use the git branch command.

Overview of how branching works in Git with an example:

  1. Creating a Branch: To create a new branch, you use the git branch command followed by the name of the new branch. For example:

      git branch new-feature
    
  2. Switching Branches: You can switch between branches using the git checkout command followed by the name of the branch you want to switch to. For example:

      git checkout new-feature
    
  3. Adding Commits: Once you're on the new branch, you can make changes to your code as usual and commit them using git add and git commit.

  4. Merging Branches: After you've finished working on your feature or fix, you can merge the changes back into the main branch (often master or main) using the git merge command. For example:

      git checkout main
      git merge new-feature
    
  5. Resolving Conflicts: Sometimes, when you merge branches, Git may encounter conflicts if changes were made to the same part of a file in both branches. In such cases, you'll need to manually resolve the conflicts before completing the merge.

  6. Deleting Branches: Once you've merged a branch and no longer need it, you can delete it using the -d option with the git branch command. For example:

      git branch -d new-feature
    

🟠 Git Revert and Reset

The git revert and git reset commands are both used to undo changes in Git. However, there are some key differences between the two commands.

git revert is used to create a new commit that undoes the changes made in a previous commit. git revert is the safer option to use, as it does not rewrite the commit history.

You can use git revert:

  1. Identify the Commit to Revert: You need to find the commit hash of the commit you want to revert. You can do this using git log.

  2. Revert the Commit: Use git revert followed by the commit hash. For example:

      git revert xyz90897
    

    This will create a new commit that undoes the changes introduced by the specified commit.

git reset is used to reset the current branch to a specific commit or undo changes. git reset should only be used if you are sure that you want to rewrite the commit history.

you can use git reset:

  1. Identify the Commit or Reference: You need to specify the commit hash, branch name, or any other reference to indicate the state you want to reset to.

  2. Choose Reset Mode:

    • Soft Reset (--soft): Moves the HEAD to the specified commit but keeps changes in the index and working directory.

    • Mixed Reset (--mixed): Default mode. Resets the index to the specified commit but keeps changes in the working directory.

    • Hard Reset (--hard): Resets both the index and the working directory to the specified commit. This is the most aggressive mode and can result in the loss of uncommitted changes.

Here are examples of each reset mode:

  • Soft Reset:

        git reset --soft xyz2389
    
  • Mixed Reset:

        git reset --mixed pqr9089
    
  • Hard Reset:

        git reset --hard qwer0098
    

🟠 What is Git Rebase?

Git rebase is a command that allows you to move or combine commits on a branch. This can be useful for cleaning up your commit history or for integrating changes from another branch.

git rebase <base_branch> #basic syntax for Git Rebase

Here's a simple example of how git rebase works:

Let's say you have two branches: main and feature. Both branches have diverged, meaning there are commits on both branches since they were last in sync.

  1. Initial Commit History:

      codemain:    P---Q---R
                      \
      feature:         S---T
    
  2. Starting a Rebase: You're on the feature branch and want to incorporate changes from main into feature. You initiate the rebase:

      git checkout feature
      git rebase main
    
  3. Reapplying Commits: Git will pause the rebase process if it encounters any conflicts that need to be resolved. Assuming there are no conflicts, Git will automatically reapply each commit from feature onto the tip of main.

  4. Final Commit History: After the rebase is complete, the commit history might look like this:

      codemain:    P---Q---R
                           \
      feature:             S'---T'
    

    Here, D' and E' are new commits, which are the same changes as D and E but with potentially different commit hashes due to the rebase operation. The feature branch now contains the changes from main as well, but in a linear fashion.

  5. Pushing Changes: Finally, you can push the rebased feature branch to the remote repository:

      git push origin feature
    

And that's a simple example of how git rebase works.

🟠 What is Git Merge?

Git merge is a command used to combine changes from two or more branches into a single branch. It is a powerful tool that allows developers to work on different features or bug fixes in parallel, and then merge their changes together when they are ready.

git merge <source_branch> #basic syntax for Git Merge

Example 1: Merge Feature Branch into Main Branch

Suppose you have a main branch and a feature branch. You've completed work on the feature branch and want to merge those changes into main.

  1. Checkout the Main Branch: First, ensure you're on the main branch:

      git checkout main
    
  2. Merge Feature Branch: Now, you merge the feature branch into main:

      git merge feature
    

    This command integrates the changes from the feature branch into the main branch.

  3. Resolve Conflicts (if any): If there are conflicts during the merge process (e.g., if changes were made to the same part of a file in both branches), Git will pause and prompt you to resolve them. You can use git status, git diff, and git add to resolve conflicts.

  4. Commit the Merge: After resolving conflicts, commit the merge:

      git commit
    
  5. Push the Merged Changes: Finally, push the merged changes to the remote repository:

      git push origin main
    

✸Conclusion:

In conclusion, mastering advanced Git and GitHub techniques empowers DevOps engineers to streamline development workflows, improve collaboration, and accelerate software delivery, making them indispensable assets in modern software development environments.

Happy Learning : )