Day 13 Advanced Git & GitHub for DevOps Engineers

FauzeyaFauzeya
3 min read

Git is a powerful version control system that helps developers manage their code effectively. This guide will walk you through key concepts like branching, reverting, resetting, merging, and rebasing in Git. Each concept will be explained, followed by practical tasks to help you apply what you've learned.

1. What is Git Branching?

Branches in Git are used to develop features, fix bugs, or experiment with new ideas without affecting the main codebase.

  • Main Branch: The default branch where the stable code resides, often named main or master.

  • Feature Branch: A new branch created for a specific feature or fix.

How to Create a Branch and Add a Feature

  1. Create a New Branch: Start by switching to the main branch.

     git checkout main
    
  2. Create a New Branch: Make a new branch for your feature.

     git checkout -b dev
    
    • This creates and switches you to the dev branch.
  3. Add a Feature: Create a new text file with some content.

     echo "This is the first feature of our application" > Devops/Git/version01.txt
    
  4. Stage Your Changes: Add the file to the staging area.

     git add Devops/Git/version01.txt
    
  5. Commit Your Changes: Save your changes with a descriptive message.

     git commit -m "Added new feature"
    
  6. Push Your Changes to GitHub: Send your changes to the remote repository.

     git push origin dev
    

2. Adding More Features with Separate Commits

You can continue to add features or make changes by following these steps:

  1. Update Your File: Add a line about a bug fix.

     echo "This is the bug fix in the development branch" >> Devops/Git/version01.txt
     git commit -am "Added feature2 in development branch"
    
  2. Add More Lines:

    • Add another line (gadbad code):

        echo "This is gadbad code" >> Devops/Git/version01.txt
        git commit -am "Added feature3 in development branch"
      
    • Add one more line (this feature will gadbad everything):

        echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
        git commit -am "Added feature4 in development branch"
      

3. Restoring the File to a Previous Version

If you want to revert changes made in the last few commits:

  1. Revert Commits: This will remove the last two commits from the history.

     git revert HEAD~2
    
    • The file will now contain only the first feature and the bug fix.

4. Working with Branches

To manage multiple features or fixes:

  1. Create Additional Branches:

    • Create another branch for a different feature:
    git checkout -b feature2
  1. Visualize Your Branch Structure:

    • To see your branches and their structure, use:
    git branch

5. Merging Changes into Master

To merge your changes back into the main branch:

  1. Switch to the Main Branch:

     git checkout main
    
  2. Merge the Development Branch:

     git merge dev
    

6. Understanding Git Revert vs. Git Reset

  • Git Revert: Creates a new commit that undoes changes from previous commits without altering commit history.

  • Git Reset: Moves the HEAD pointer to a specified commit and can affect the working directory.

7. Understanding Git Merge vs. Git Rebase

  • Git Merge: Combines changes from one branch into another, preserving the history of both branches.

  • Git Rebase: Moves or combines commits from one branch onto another branch, resulting in a linear commit history.

8. Practicing Rebase

To practice rebasing:

  1. Rebase Your Development Branch onto Master:

     git checkout dev
     git rebase main
    
    • This replays the commits from the dev branch on top of the main branch.

Conclusion

Git's branching, reverting, resetting, merging, and rebasing features provide powerful tools for managing your code. By following this guide and practicing the tasks, you can enhance your Git skills and collaborate more effectively with others.

2
Subscribe to my newsletter

Read articles from Fauzeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fauzeya
Fauzeya

Hi there! I'm Fauzeya 👩‍💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀