Advance Git & GitHub for DevOps Engineers

Advance Git & GitHub for DevOps Engineers

Introduction📜

As a DevOps engineer, mastering version control is a crucial skill for maintaining efficient workflows and collaborating effectively with your team. Git and GitHub are powerful tools that enable you to manage your codebase and work together seamlessly. In this blog, we'll dive into some advanced Git and GitHub concepts that every DevOps engineer should be familiar with: Git Branching, Git Revert and Reset, Git Rebase, and Git Merge.

Git Branching🌿

Imagine you're working on a software project, and you want to develop a new feature without affecting the main codebase. Git branching allows you to do just that. A branch is like a parallel universe of your code, where you can make changes independently. This ensures that your main code (often on the master branch) remains stable.

Some common commands

git branch List all the branches in the repository

git branch <branch> Create a new branch called <branch>

git checkout -b <branch> To change the current branch

git branch -d <branch> Deletes a branch. If there are unmerged changes, Git does not allow you to delete it.

git branch -D <branch> Forces delete the branch, even if there are unmerged changes. Execute this command when you are sure to delete it permanently.

git branch -m <branch> Moves or renames the current branch to <branch>.

git branch -a Lists all the remote branches.

Git Revert and Reset🔀

In the world of software development, mistakes happen. Git provides ways to undo changes without causing chaos.

Revert: It creates a new commit that undoes a previous commit's changes. This preserves the commit history.

Reset: It erases commits, moving the branch pointer to a previous commit. Be cautious with this as it can alter history.

A. git reset is considered a more powerful and potentially dangerous command because it can lead to the loss of commit history.

B. git reset --soft moves the branch pointer to the specified commit while preserving the changes in the working directory and staging area.

C. git reset --hard moves the branch pointer to the specified commit and discards all changes in the working directory and staging area that came after the reset point.

What Is Git Rebase?🏗️

Rebase is like taking your changes and placing them at the end of the latest codebase. It creates a linear history by incorporating the changes from one branch into another.

git rebase <source-branch>

What Is Git Merge?🔃

Merge combines the changes from different branches into one. It creates a new commit that has two parent commits, showing the branching history clearly.

Check out the main branch

 git checkout main

then merge the function_1 branch into the main branch

git merge function_1

Tasks:

1. Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master, [hint try git checkout -b dev], swithch to dev branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]

version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]

Add new commit in dev branch after adding 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 development branch

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

  • 2nd line>> This is gadbad code

  • Commit this with message “ Added feature3 in development branch

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

  • Commit with message “ Added feature4 in development branch

Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]

  • Created the file version01.txt and committed the first change locally

  • pushed the changes , now dev branch updated on git

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

    Commit this with message “ Added feature2 in development branch”

  • 2nd line>> This is gadbad code

    Commit this with message “ Added feature3 in development branch

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

    Commit with message “ Added feature4 in development branch

  • Restore the file to a previous version

    git revert <commit id>

  • now, checking git log

2. Add some changes to dev branch and merge that branch in master

  • Check the branch you are currently working on

    git branch

  • Add some changes in the dev branch and commit them

  • switched to the "main" branch

  • merge the dev branch into main branch

Conclusion📚

As a DevOps engineer, Git and GitHub are your canvas and palette. By understanding advanced Git concepts like branching, reverting, resetting, rebasing, and merging, you'll wield these tools with finesse. Just as a painter grows with each canvas, your proficiency in Git will evolve with each project. Remember, the key is practice, experimentation, and embracing the collaborative spirit of modern development. So go forth, create, collaborate, and let Git be your artistic expression in the world of software.

Mastering these advanced Git techniques will undoubtedly elevate your DevOps game. Through controlled branching, skillful undoing, and harmonious merging, you'll navigate the intricate landscapes of software development with confidence. So, whether you're crafting code or crafting a masterpiece, let Git and GitHub be your creative companions. Happy coding!