Skip to main content

Command Palette

Search for a command to run...

Day 13: Advanced Git & GitHub for DevOps Engineers

Published
4 min read
Day 13: Advanced Git & GitHub for DevOps Engineers
P

Greetings! 👋 I'm Priyadarshi Ranjan, a dedicated DevOps Engineer embarking on an enriching journey. Join me as I delve into the dynamic realms of cloud computing and DevOps through insightful blogs and updates. 🛠️ My focus? Harnessing AWS services, optimizing CI/CD pipelines, and mastering infrastructure as code. Whether you're peers, interns, or curious learners, let's thrive together in the vibrant DevOps ecosystem. 🌐 Connect with me for engaging discussions, shared insights, and mutual growth opportunities. Let's embrace the learning curve and excel in the dynamic realm of AWS and DevOps technology!

Git Branching

Branches are a core concept in Git, enabling isolated development work without affecting the main codebase. Each repository has a default branch (often main or master), but you can create multiple branches for different tasks. This way, you can work on new features, bug fixes, or experimental changes without disrupting the main project.

  • Creating a Branch: Use the git branch command to create a new branch. For instance, to create a branch named feature, you would run:

      git branch feature
    
  • Switching to a Branch: Use git checkout to switch between branches:

      git checkout feature
    
  • Creating and Switching: Combine these actions with the -b flag:

      git checkout -b feature
    
  • Merging a Branch: When you are done with your changes, you can merge your branch back into the main branch. First, switch to the main branch:

      git checkout master
    

    Then, merge the feature branch:

      git merge feature
    

Branches are useful for organizing work on different aspects of your project, making it easier to track changes and collaborate with others.

Git Revert and Reset

Both git revert and git reset are commands used to undo changes, but they function differently:

  • Git reset: This command moves the HEAD pointer to a specified commit, effectively changing the commit history. It has three modes:

    • --soft: Keeps changes in the working directory and staging area.

    • --mixed: Keeps changes in the working directory but clears the staging area.

    • --hard: Discards all changes, both in the working directory and the staging area.

Example of a hard reset:

    git reset --hard <commit_hash>
  • Git revert: This command creates a new commit that undoes the changes of a specified commit, preserving the commit history. It’s safer for collaborative environments since it doesn’t rewrite history.

    Example:

      git revert <commit_hash>
    

Git Rebase and Merge

Git Rebase and Git Merge are used to integrate changes from one branch to another:

  • Git Rebase: Rebase rewrites the commit history by moving the entire branch to a new base commit. It creates a linear history, which is cleaner and easier to understand.

      git checkout feature
      git rebase master
    

    This command applies the changes from the feature branch on top of the master branch, making the history linear.

  • Git Merge: Merge combines the changes from one branch into another by creating a new merge commit. It preserves the commit history of both branches.

      git checkout master
      git merge feature
    

    While rebase results in a cleaner history, merge is better for preserving the context of feature development.

Tasks

Task 1: Feature Development with Branches

Create a Branch and Add a Feature:

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

  2. Create a new branch from master:

     git checkout -b dev
    
  3. Add and commit the changes:

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

Push Changes to GitHub:

  1. Push the local commits to the repository on GitHub:

     git push origin dev
    

Add More Features with Separate Commits:

  1. Update version01.txt with new lines and commit after each change:

     echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
     git commit -am "Added feature2 in development branch"
    
     echo "This is gadbad code" >> Devops/Git/version01.txt
     git commit -am "Added feature3 in development branch"
    
     echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
     git commit -am "Added feature4 in development branch"
    

Restore the File to a Previous Version:

  1. Use git revert to undo the last two commits:

     git revert HEAD~2
    

Task 2: Working with Branches

Demonstrate Branches:

  1. Create multiple branches:

     git branch feature1
     git branch feature2
    
  2. Take screenshots of the branch structure:

     git branch -a
    

Merge Changes into Master:

  1. Make changes to the dev branch and merge into master:

     git checkout dev
     echo "More changes" >> Devops/Git/version01.txt
     git commit -am "Added more changes in dev branch"
    
     git checkout master
     git merge dev
    

Practice Rebase:

  1. Try rebasing dev onto master and observe the changes:

     git checkout dev
     git rebase master
    

Conclusion

Mastering advanced Git and GitHub techniques is essential for effective DevOps practices. By leveraging branches, reverts, resets, rebases, and merges, you can manage code changes efficiently, collaborate seamlessly, and maintain a clean and organized project history. These skills are vital for any DevOps engineer aiming to streamline development workflows and ensure code quality.

Reference

For a visual guide, you can watch this YouTube video by Shubham Londhe Sir.

Connect and Follow Me on Socials

LINKDIN | GITHUB |TWITTER

More from this blog

Priyadarshi Ranjan

71 posts

As a DevOps engineer, I leverage automation and continuous integration to streamline development workflows, ensuring robust and scalable deployments.