💠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 a fundamental concept in version control that allows developers to work on multiple independent lines of development within a single repository. Branches are essentially pointers to specific commits, enabling developers to isolate work on new features, bug fixes, or experiments without affecting the main codebase.

Commands:

  1. List Branches:

     git branch
    

    This command lists all branches in the repository, highlighting the currently active branch.

  2. Create a New Branch:

     git branch <branch_name>
    

    Use this command to create a new branch with the specified name. The new branch will be based on the commit you are currently on.

  3. Switch to a Branch:

     git checkout <branch_name>
    

    To switch to an existing branch, use this command. It updates the working directory to match the selected branch's state.

  4. Create and Switch to a New Branch (Shortcut):

     git checkout -b <new_branch_name>
    

    This shortcut command creates a new branch and switches to it in a single step.

  5. Merge Branches:

     git merge <branch_name>
    

    Merging integrates changes from the specified branch into the currently active branch. It creates a new merge commit if there are conflicting changes.

  6. Delete a Branch:

     git branch -d <branch_name>
    

    Use this command to delete the specified branch. It prevents deletion if the branch contains unmerged changes.

  7. Force Delete a Branch:

     git branch -D <branch_name>
    

    If you're sure you want to delete the branch, including unmerged changes, use this command.

Usage Example:

Let's say we want to create a new branch named "feature-1" to work on a new feature:

  1. Create the branch:

     git branch feature-1
    
  2. Switch to the new branch:

     git checkout feature-1
    
  3. Make changes, add commits, and push the branch to the remote repository if needed.

  4. Once the feature is complete, merge it into the main branch:

     git checkout main
     git merge feature-1
    

☞Git Revert and Reset

Git revert and reset are essential commands for managing changes in a Git repository.

Commands:

  1. Git Revert:

     git revert <commit_hash>
    

    Revert undoes a specific commit by creating a new commit that reverses the changes introduced by the original commit. It's useful for undoing changes without altering the repository's history.

  2. Git Reset:

     git reset [--soft | --mixed | --hard] <commit_hash>
    

    Reset moves the current branch's HEAD to a specified commit. The --soft option keeps staged changes, --mixed resets the staging area, and --hard discards all changes.

Usage Example:

Let's say you want to revert the last commit and reset the changes made in the working directory:

  1. Revert the last commit:

     git revert HEAD
    

    This creates a new commit that undoes the changes introduced by the previous commit.

  2. Reset changes in the working directory:

     git reset --hard HEAD^
    

    This moves the HEAD to the parent of the current commit, effectively discarding any changes made in the working directory.

☞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.

COPY

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:

    COPY

      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:

    COPY

      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:

    COPY

      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:

    COPY

      git push origin feature
    

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

☞What is Git Merge?

Git merge is a fundamental command used to integrate changes from one branch into another branch. When you merge branches in Git, it combines the changes made in the source branch with the target branch, creating a new merge commit that incorporates the changes from both branches.

Usage Example:

Suppose you have a feature branch (feature) where you've been working on a new feature, and you want to merge it into the main branch (main) once the feature is complete.

  1. Switch to the main branch:

     bashCopy codegit checkout main
    
  2. Start the merge process:

     bashCopy codegit merge feature
    
  3. Resolve any merge conflicts that may arise during the merge process.

  4. Once conflicts are resolved, Git creates a new merge commit that combines the changes from both feature and main, incorporating the feature into the main branch.

✤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:

    COPY

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

    COPY

      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:

    COPY

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

    COPY

      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.

***I'm confident that this blog will prove to be valuable, helping you discover new insights and learn something enriching .***🙏

😊Happy Learning : )