#Day10: Advance Git & GitHub for DevOps Engineers.

#Day10: Advance Git & GitHub for DevOps Engineers.

ยท

6 min read

๐ŸŒฟ All About Git Branching ๐ŸŒฟ

Hey there! ๐Ÿ‘‹ Today, we're going to explore the magical world of Git Branching! ๐ŸŒณ If you're a developer or a tech-savvy person, chances are you've heard about Git, the super cool version control system that helps us manage our code changes. ๐Ÿ™ But what's this "branching" thingy? Let's dive in and find out! ๐ŸŠโ€โ™‚๏ธ

๐ŸŒŸ Git Branching: What's It All About?

Imagine you're working on a project ๐Ÿ—๏ธ, and you want to add a new feature or fix a pesky bug. Instead of changing the main code directly and potentially breaking things for others, Git branching lets you create a separate "branch" ๐ŸŒณ where you can work on your changes safely. It's like having your own coding playground! ๐ŸŽ‰

๐Ÿ’ก Why Branching is Awesome?

Here's the beauty of it โ€“ while you're doing your thing on your branch, the main code (often called "master" or "main" branch) remains untouched. ๐Ÿš€ That means others can continue working on the project, and when you're ready, you can merge your changes back into the main code like a pro! ๐Ÿค

๐Ÿ”™ Git Revert and Reset: The Magic Undo Buttons ๐Ÿ”™

Okay, sometimes things go a little haywire, and we make mistakes โ€“ it happens to the best of us! ๐Ÿ˜… That's where Git's "revert" and "reset" come to the rescue! ๐Ÿš‘

๐Ÿ”„ Git Revert:

Picture this: you've committed some code changes, but later you realize they're causing trouble ๐Ÿž, and you want to undo those changes. The "git revert" command is your superhero! ๐Ÿฆธโ€โ™‚๏ธ It creates a new commit that undoes the specific changes you want to get rid of. Your code is saved, and the day is saved! ๐ŸŒŸ

โš ๏ธ Caution: Revert creates a new commit, so your commit history stays intact. Just be clear about what you're reverting! ๐Ÿ˜‰

๐Ÿ”™ Git Reset:

Now, let's say you want to go back in time ๐Ÿ•ฐ๏ธ, eliminating one or more commits entirely. Git "reset" is here to make it happen! It's like a time machine that can take you to any commit you choose. ๐Ÿ˜ฎ

โš ๏ธ Caution: Reset is a bit more dangerous โ€“ it erases commits permanently! So, be extra careful and use it wisely! ๐Ÿง

๐Ÿ”„ What Is Git Rebase? ๐Ÿ”„

Oh boy, get ready for some advanced Git magic! ๐Ÿง™โ€โ™‚๏ธ Git rebase is like giving your branch a makeover! ๐Ÿ’… Imagine you've been working on your branch for a while, and in the meantime, some changes have been made to the main branch. ๐Ÿ”„

Instead of merging your branch with the main one right away, "git rebase" lets you move your changes to the tip of the main branch. ๐Ÿš€ It's like you're reapplying your changes on top of the latest and greatest stuff! ๐Ÿ˜Ž This can keep your commit history tidy and linear. ๐Ÿงน

๐Ÿ’ก Keep in mind: Rebase can be fantastic, but it can also cause confusion if used carelessly. Make sure you're aware of its implications, especially if you're collaborating with other developers. ๐Ÿค

๐Ÿค What Is Git Merge? ๐Ÿค

Ah, the classic Git merge! ๐Ÿคœ๐Ÿค› It's like bringing all the cool kids together at the same table! ๐Ÿ•บ๐Ÿ’ƒ When you're done working on your branch and you're satisfied with your changes, it's time to merge it with the main branch. ๐ŸŽ‰

Git merge takes the code from your branch and combines it with the main branch, creating a new "merge commit" that incorporates both sets of changes. ๐ŸŒˆ The result? Your changes are now part of the main codebase for everyone to enjoy! ๐ŸŽŠ

๐ŸŒŸ Pro Tip: Keep in mind that sometimes merge conflicts may pop up when Git tries to combine the changes. Don't worry, though โ€“ you've got the skills to resolve them like a pro! ๐Ÿ’ช

That's a wrap on our Git adventure! ๐ŸŽฌ We've explored branching, the magic undo buttons (revert and reset), the transformative rebase, and the inclusive merge! ๐ŸŒŸ Git is an amazing tool that makes version control feel like a breeze! ๐ŸŒฌ๏ธ So go ahead, try it out, and unleash your coding powers! ๐Ÿš€๐Ÿ’ป

Keep branching and keep coding! ๐ŸŒณ Happy Git-ing! ๐Ÿ˜„โœจ

Task 1:

Adding Features and Restoring Previous Versions

In this task, we'll learn how to create a new branch, add a file, and commit changes. We'll also make some additional commits and then restore the file to a previous version.

Step 1: Create a new branch and add a file

We start by creating a new branch called "dev" using the following command:

git checkout -b dev

Next, we create a text file called version01.txt inside the Devops/Git/ directory with the content "This is the first feature of our application."

Step 2: Commit changes and push to the remote repository

We add and commit the changes made to the new branch with the commit message "Added new feature."

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

To make these changes visible in the remote repository, we push the branch to the remote.

git push origin dev

Step 3: Add more commits to the dev branch

Now, we add new content to the version01.txt file as follows:

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

Commit this change with the message "Added feature2 in development branch."

Then, add the second line to the file:

2nd line>> This is gadbad code

Commit this change with the message "Added feature3 in development branch."

Lastly, add the third line to the file:

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

Commit this change with the message "Added feature4 in development branch."

Step 4: Restore the file to a previous version

Suppose we want to revert the file to a previous version where the content was "This is the bug fix in development branch." To do this, we can use the git reset command:

Find the commit hash of the version you want to revert to
git log

# Reset to the commit hash of the version you want to revert to
git reset --hard <commit_hash>

Task 2:

In this task, we'll explore the concept of branches with multiple branches, make changes to the dev branch, and then merge it into the master branch. Additionally, we'll try out Git rebase and observe the differences.

Step 1: Create and switch to the dev branch

We start by creating a new branch called "feature-branch" and switch to it.

git checkout -b feature-branch

Step 2: Make changes and commit in the dev branch

Here, we can make any desired changes to our files. For example, add new features or fix bugs.

Make changes and add files as necessary
git add .
git commit -m "Added new features in feature-branch"

Step 3: Merge the dev branch into the master branch

To incorporate the changes made in the dev branch into the master branch, we switch back to the master and merge the feature-branch.

git checkout master
git merge feature-branch

Step 4: Try Git rebase

Rebasing is another way to incorporate changes from one branch into another. It moves the entire feature-branch to begin on the tip of the master branch, resulting in a linear commit history.

# Assuming you are on the feature-branch
git rebase master

Conclusion ๐Ÿ:

In this blog, we covered two tasks that helped us understand the fundamental concepts of branching in Git. We learned how to create branches, commit changes, push to remote repositories, and perform basic operations like merging and reverting to previous versions. Git's branching and version control capabilities significantly enhance collaboration and productivity in software development, making it an essential tool for every developer. Remember, practice is the key to mastering Git, so don't hesitate to experiment and explore more about this powerful version control system. Happy coding! ๐ŸŽ‰๐Ÿš€

ย