🚀Advance Git & GitHub for DevOps Engineers | Git Branching | Rebase/Merge | Revert/Reset | #Day10 #90daysofdevops🚀

🚀Advance Git & GitHub for DevOps Engineers | Git Branching | Rebase/Merge | Revert/Reset | #Day10 #90daysofdevops🚀

Welcome fellow DevOps enthusiasts to this blog, where we'll demystify some essential Git operations and commands! Let's dive right in and explore Git Branching, Git Revert, Git Reset, Git Rebase, and Git Merge.

🌿 Git Branching 🌿

Git branching is a fundamental feature that allows us to work on multiple tasks simultaneously without disturbing the main development line. Imagine branches as separate copies of your project where you can implement new features, fix bugs, or experiment safely. The primary branch is usually the "master" or "main" branch, and you create new branches from there.

Important Commands:

  • Create a new branch: git branch <branch_name>

  • Switch to a branch: git checkout <branch_name>

  • Create and switch to a branch: git checkout -b <branch_name>

  • List branches: git branch

  • Delete a branch: git branch -d <branch_name>

Here's a step-by-step process to create a Git branch:

Step 1: Make sure you have Git installed on your system. If you don't, download and install Git from the official website (https://git-scm.com/).

Step 2: Open your terminal or command prompt.

Step 3: Navigate to the directory of your Git repository using the cd command. For example:

cd /path/to/your/git/repository

Step 4: Ensure you are on the main development branch (e.g., "master" or "main") by running the following command:

git checkout main

Replace "main" with the name of your primary branch if it's different.

Step 5: Pull the latest changes from the remote repository to ensure you have the most up-to-date version of the main branch:

git pull

Step 6: Now, it's time to create your new branch. You can name the branch according to the feature or task you're working on. To create the branch, use the following command:

git branch <branch_name>

Replace <branch_name> with the name you want for your new branch.

Step 7: Switch to the newly created branch using the checkout command:

git checkout <branch_name>

Again, replace <branch_name> with the name you chose in Step 6.

Step 8: Congratulations! You have now successfully created and switched to your new branch. You can start working on your changes and commit them as usual.

Optional Step: If you want to combine the branch creation and checkout steps into one, you can use the -b option with the checkout command:

git checkout -b <branch_name>

💾 Git Revert and Reset 💾

Sometimes, mistakes happen, or we need to undo changes in Git. The two primary ways to do this are through Git Revert and Git Reset.

Git Revert allows you to undo a specific commit while keeping a record of the undo in your history. It creates a new commit with the inverse changes.

Important Commands:

  • Revert a commit: git revert <commit_hash>

  • Revert the last commit: git revert HEAD

🔙 Stepwise process to Git Revert 🔙

Git Revert is a safe way to undo changes in a Git repository while preserving a record of the undo operation in the commit history. It creates a new commit that undoes the changes introduced by a specific commit. Follow these steps to perform a Git revert:

  1. Identify the Commit to Revert: First, you need to identify the commit you want to revert. You can find the commit hash using git log or any Git GUI tool.

  2. Perform the Revert: Once you have the commit hash, use the git revert command to create a new commit that undoes the changes in the target commit. For example, if the commit hash is abcdef, use the following command:

     git revert abcdef
    

    Git will open your default text editor to add a commit message for the revert. Save and close the editor after writing the message.

  3. Resolve Conflicts (if any): In some cases, Git may encounter conflicts while attempting to revert the changes. Conflicts occur when the changes in the commit you want to revert interfere with other changes in the repository. Git will prompt you to resolve these conflicts manually.

    Use git status to identify the files with conflicts. Open each conflicting file, resolve the conflicting lines, and save the changes. After resolving all conflicts, stage the modified files using git add and continue the revert process:

     git revert --continue
    
  4. Finish the Revert: Once all conflicts are resolved (if any), Git will create a new commit that undoes the changes from the target commit. The revert process is now complete.

  5. Push the Revert to Remote (if necessary): If you're working with a remote repository and want to share the revert with your team, use the git push command to push the changes to the remote repository:

git push origin <branch_name>

Replace <branch_name> with the name of the branch you are working on.

🔄 Process to Git Reset 🔄

Git reset is a powerful command that allows you to move the current branch to a specific commit, effectively undoing changes or altering your project's history. It comes with different options that determine the scope of the reset. Here's a step-by-step process to perform a Git reset:

Step 1: Check Branch and Status: Ensure you are on the correct branch by running git branch and check the status of your working directory with git status. This helps avoid any accidental changes.

Step 2: Identify the Commit to Reset: Find the commit hash of the commit you want to reset to. You can obtain this hash by using git log to see the commit history.

Step 3: Choose the Appropriate Reset Option: There are three primary reset options: --soft, --mixed, and --hard. Select the option that suits your needs:

a. --soft: Keeps changes in the working directory. Commits will be removed, but changes will remain unstaged. b. --mixed: This is the default option. It unstaged changes but keeps them in the working directory. Commits will be removed. c. --hard: This is a powerful option. It resets both the commits and working directory to the specified commit, discarding all changes.

Step 4: Perform the Reset: Run the git reset command followed by the reset option and the commit hash you identified in Step 2.

Step 5: Confirm the Reset: After performing the reset, use git log again to confirm that the branch has been reset to the desired commit. Double-check the working directory status with git status to ensure that the changes are as expected.

Important Commands:

  • Soft reset (keep changes in the working directory): git reset --soft <commit_hash>

  • Mixed reset (unstage changes): git reset <commit_hash>

  • Hard reset (discard changes): git reset --hard <commit_hash>

⚠️ Caution ⚠️ Be cautious when using git reset --hard, as it irreversibly removes all changes after the specified commit. This command should be used with care, especially when collaborating with others or working with remote repositories.

⚙️ Git Rebase ⚙️

Git Rebase is a powerful tool to combine the changes from one branch onto another, giving your history a cleaner and linear look. It replays the commits from one branch onto another, making it appear as if the work was done sequentially.

Important Commands:

  • Rebase a branch onto another: git rebase <base_branch>

  • Resolve conflicts during rebase: Manually edit the conflicting files and git add them. Then, run git rebase --continue.

  • Abort a rebase: git rebase --abort

let's walk through the process of performing a Git rebase step-by-step:

  1. Create a Backup: Before starting any critical operation like a rebase, it's always a good idea to create a backup or make sure your repository is in a clean state. Commit any pending changes or stash them to ensure you can revert to the original state if needed.

  2. Select the Branch to Rebase: Decide which branch you want to rebase. Typically, you'll rebase a feature branch onto the latest changes in the main branch (e.g., "master" or "main").

  3. Fetch and Pull: Ensure your local repository is up to date with the remote repository by running the following commands:

git fetch origin
git pull origin <branch_name>
  1. Checkout the Branch to Rebase: Switch to the branch you want to rebase (e.g., a feature branch).
git checkout <branch_name>
  1. Initiate the Rebase: Use the git rebase command followed by the target branch (e.g., "master") to start the rebase process.
git rebase master
  1. Resolve Conflicts (if any): During the rebase, if Git encounters any conflicting changes between the target branch and your feature branch, it will pause the rebase. You'll need to resolve these conflicts manually.

Use git status to identify the files with conflicts, open them in your preferred text editor, and resolve the conflicts. After resolving each conflict, use git add <file_path> to stage the changes.

  1. Continue the Rebase: Once all conflicts are resolved, continue the rebase process using:
git rebase --continue

If you want to abort the rebase at any point, use:

git rebase --abort
  1. Push Changes: After successfully rebasing, you'll need to force-push your updated branch to the remote repository since the commit history has changed.
git push origin <branch_name> --force

🔀 What is Git Merge? 🔀

Git Merge is the process of combining changes from one branch into another. It creates a new commit that includes the changes from the merged branch. Merging is commonly used to integrate features or bug fixes into the main branch.

Important Commands:

  • Merge a branch into the current branch: git merge <branch_name>

  • Merge and fast-forward (when possible): git merge --ff-only <branch_name>

  • Merge and create a merge commit: git merge --no-ff <branch_name>

  • Abort a merge: git merge --abort

Here's a step-by-step guide to merge a branch in Git:

  1. Ensure you're on the Target Branch: Before merging, make sure you are on the branch into which you want to merge the changes. This is typically the main branch. You can use the following command to switch to the target branch:
  •     git checkout main
    
  • Pull the Latest Changes: It's essential to have the latest changes from the remote repository before performing a merge. To pull the latest changes from the main branch, run:

      git pull origin main
    
  • Start the Merge: Now, you can start the merge process. To merge another branch (let's call it "feature-branch") into the main branch, use the following command:

      git merge feature-branch
    

    If there are no conflicts between the two branches, Git will perform a "fast-forward" merge, which means it will move the main branch pointer to the same commit as the "feature-branch" pointer.

  • Resolve Conflicts (If Any): If there are conflicting changes between the two branches (i.e., changes in the same lines of code), Git will notify you of conflicts. You'll need to manually resolve these conflicts by editing the conflicting files, removing the conflict markers (<<<<<<<, =======, >>>>>>>), and then committing the resolved changes.

  • Commit the Merge: After resolving any conflicts, add the modified files to the staging area and commit the merge:

      git add .
      git commit -m "Merge 'feature-branch' into 'main'"
    
  • Push the Changes: Finally, push the merged changes to the remote repository:

      git push origin main
    
  • Optional: Delete the Merged Branch (If Needed): If the "feature-branch" is no longer needed, you can delete it with the following command:

git branch -d feature-branch

#Day 10 Tasks:

On day 10 of 90daysofdevops, let's do the hands-on of following tasks on our machine:

Task 1:

  1. Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside..

  2. Switch to the dev branch with this command git checkout -b dev

  3. version01.txt should reflect at the local repo first followed by the Remote repo for review.

  4. Your commit message should be “Added new feature”.
    git commit -m "Added new feature”

  5. We will use git push in order to reflect the local changes to the remote repository.

Add a new commit in dev branch after adding the below-mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines

  • 1st line>> This is the bug fix in the development branch

  • Commit this with the message “ Added feature2 in development branch”

  • 2nd line>> This is gadbad code

  • Commit this with the message “ Added feature3 in the development branch

  • 3rd line>> This feature will gadbad everything from now.

  • Commit with the message “ Added feature4 in the development branch

Restore the file to a previous version where the content should be “This is the bug fix in the development branch”

Task 2:

  • Demonstrate the concept of branches with 2 or more branches with screenshot.

  • add some changes to dev branch and merge that branch in master

🎉 Wrapping Up 🎉

We have seen Git Branching, Revert, Reset, Rebase, and Merge, along with their important commands in this blog.

Git is an incredible version control system that empowers developers to collaborate effectively and manage code efficiently.

Remember, Git provides an impressive arsenal of commands, so take time to explore and experiment with caution. Happy coding! 😊👍

📚 Resources:

Thanks!

#trainwithshubham #Devops #devopsjourney #git #github #devopscommunity #90daysofdevops