Git & GitHub 02 - day 13

Git & GitHub 02 - day 13

Advanced Git & GitHub for DevOps Engineers

Today, I took a deeper dive into some advanced Git and GitHub features that are crucial for DevOps engineers. These tools not only help us manage code but also keep our development process organized and efficient. Let’s walk through some of the key concepts I learned and the tasks I worked on today.

Git Branching: Keeping Your Work Isolated

Branches in Git are like different pathways where you can make changes without affecting the main project. This is super useful when developing new features, fixing bugs, or experimenting with new ideas. You can always merge these branches back into the main codebase when you're ready.

Each project starts with a default branch (often called main or master), and you can create as many other branches as you need. Once your feature is complete, you can combine it with the default branch through a pull request. This way, you’re not messing up any existing code while you work.

Git Revert and Reset: Undoing Changes When Things Go Wrong

At some point, we all make mistakes in our code—it happens! But Git makes it easy to backtrack. Two commands that help with this are git revert and git reset. Both allow you to undo or adjust previous changes, but they work differently.

  • git reset lets you remove a commit and erase it from history as if it never happened.

  • git revert creates a new commit that undoes the changes made by a previous commit. It’s a bit safer because it preserves the history of everything, even the mistakes.

Knowing when to use each is key when managing a large project!

Git Rebase vs. Merge: What's the Difference?

Git provides two ways to bring changes from one branch into another: rebase and merge.

  • Rebase: This command helps you move or combine a sequence of commits to another branch. After rebasing, the commit history is cleaned up, making it look like the changes were made in a linear order.

  • Merge: When you merge one branch into another, Git keeps the full history of both branches intact. Merging is simpler because it preserves every little detail of what happened in each branch.

Both are useful depending on whether you want a neat, linear history (rebase) or a complete picture of all the work that’s been done (merge).

For a deeper understanding, you might want to read more about Git Rebase vs. Git Merge.


Tasks: Putting Git Concepts into Action

Task 1: Feature Development with Branches

  1. Create a Branch: First, I created a new branch from master to work on a new feature:

     git checkout -b dev
    
  2. Add a Feature: Inside the Devops/Git/ directory, I added a file called version01.txt and included the text:
    "This is the first feature of our application."

  3. Commit the Changes: After making the change, I committed it with:

     git add Devops/Git/version01.txt
     git commit -m "Added new feature"
    
  4. Push to GitHub: Then, I pushed these changes to my GitHub repository:

     git push origin dev
    

Task 2: Add More Features in Separate Commits

Next, I made multiple updates to version01.txt, committing after each change:

  • First Update: I added a bug fix and committed:

      echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
      git commit -am "Added feature2 in development branch"
    
  • Second Update: I intentionally added some bad code to demonstrate a revert later:

      echo "This is gadbad code" >> Devops/Git/version01.txt
      git commit -am "Added feature3 in development branch"
    
  • Third Update: More "gadbad" code for good measure:

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

Task 3: Revert to an Earlier Version

After playing around with some bad code, I restored the file to its previous state using git revert. This brought the file back to where it said "This is the bug fix in development branch", undoing the last two commits:

git revert HEAD~2

Task 4: Working with Branches

  1. Create Multiple Branches: I created two more branches and took screenshots to visualize the structure of my project. Each branch allows me to work on separate features or fixes in isolation.

  2. Merge Changes to Master: Once I finished making changes to the dev branch, I merged them into the master branch:

     git checkout master
     git merge dev
    
  3. Rebase Practice: I also practiced using git rebase to see how it affects commit history. Rebase rewrites the commit history, unlike merging, which keeps a full record of every commit.

     git rebase master
    

Final- - -

Understanding advanced Git concepts like branching, merging, resetting, and rebasing is critical for managing complex projects in DevOps. These tools help keep our work organized, track changes efficiently, and collaborate smoothly with our teams.