Day 10 : Git and GitHub Advance

Day 10 : Git and GitHub Advance

Another step towards 'Git' and 'GitHub' (Part 1)

ยท

4 min read

โญIntroduction

In the previous blog, we learned a lot about Git and Github๐Ÿ“๐Ÿ’ผ. We went through the topics like Git Types of Branches ๐ŸŒฟ and the Importance of Git ๐Ÿ’ญ. This blog will go over advanced Git topics like Git Rebase, Merge, Revert and Reset ๐Ÿ”.

Git Branching๐ŸŒฟ

Think of branches as parallel universes ๐ŸŒณ๐ŸŒŒ within your project. Each branch represents a separate timeline ๐Ÿ“†๐Ÿ”ง, allowing you to work on different ideas without affecting the main project.

This is how you create a branch:

git checkout -b <BRANCH_NAME>

Git Revert ๐Ÿ”™

Think of Git Revert as an "undo" button ๐Ÿ“๐Ÿ”™ for your project. It helps you reverse the changes made in a specific commit, like stepping back in time โช๐Ÿ”ง, while keeping a clear history.

When to Use Revert?

Use Git Revert๐Ÿ”™ when you want to undo a specific commit without altering the history. It's like selectively removing a change without affecting the rest of the project.

Hands-On Git Revert ๐Ÿš€โช๐Ÿ”ง:

Let's say you have a commit with the ID ABC123 that introduced a bug.

  1. Identify and Revert: Use git revert to undo the changes from that commit:

     git revert ABC123
    
  2. Resolve Conflicts: If there are conflicts, resolve them just like in a regular merge.

  3. Commit the Reversion: Once resolved, Git creates a new commit that undoes the problematic changes.

Git Reset ๐Ÿ”„

Git Reset is like a time machine ๐Ÿ”งโช that lets you go back to a previous state of your project. It erases commits and takes you to an earlier version, but be cautious as it can discard some history.

When to Use Reset?

Use Git Reset๐Ÿ”„ when you need to "rewind" your project to a specific point in time, erasing commits and altering the history. However, be cautious as it can be a powerful tool.

Difference Between Reset๐Ÿ”„ and Revert๐Ÿ”™.

Git Revert and Reset give you the ability to control your project's history โช๐Ÿ”„๐Ÿ“. Revert selectively undoes changes, while Reset takes you back to a specific state.

Git Merge ๐Ÿ”€๐Ÿค

Imagine Git Merge as a master builder ๐ŸŒ‰๐Ÿ‘ทโ€โ™‚๏ธ that brings different branches together. It takes changes from one branch and integrates them into another, creating a unified version with all the improvements.

๐Ÿค Git Merge is your bridge to collaboration. When you're ready to combine your work with others' changes, Git Merge ensures that all progress is effectively integrated, making the project complete and cohesive.

You can merge your code using:

git checkout master
git merge <BRANCH_NAME>

Git Rebase ๐Ÿ“‘

Git Rebase is a powerful tool that enables you to reshape your commit history in a branch. It's like rearranging puzzle pieces to create a more organized and coherent picture.

Checkout the branch you want to rebase. For example, if you're working on a feature branch, switch to that branch.

git checkout feature

Use the command git rebase <base-branch> where <base-branch> is the branch you want to rebase onto. This could be the main branch or another relevant branch.

git rebase master

Tasks๐Ÿ“š:

1.) Branch, Commit & Reset

First, create an Empty repo and Add 'devops/git/version01.txt'. Then add the following

This is the bug fix in development branch

Then, create a branch named dev and Push the changes into dev with the commit message "Added feature 2 in development branch".

git checkout -b dev
#After making changes
git add .
git commit -m "Added feature 2 in development branch"
git push origin dev

After reviewing the changes, add the remaining features.

2nd Feature: "This is gadbad code"

3rd Feature: "This feature will gadbad everything from now."

The next task is to revert the file to its previous state with the content โ€œThis is the bug fix in the development branch.โ€

First, use the log command to get the commit id then use the respective id to reset it back.

git log --oneline
git reset <COMMIT_ID>

2.) Merge and Rebase

In this task, we will merge the dev and the main branch using the git merge command.

Make sure you are on the main branch then execute this command.

git merge <BRANCH_NAME>

Resolve conflicts if any, then check logs.

git log --oneline

Now, let's try again with git rebase.

Switch to main branch.

git checkout main

Then, execute this command.

git rebase dev

During the rebase, the commits from the โ€œdevโ€ branch are moved on top of the existing commits in the โ€œmainโ€ branch, resulting in an updated, linear commit history on the โ€œmainโ€ branch. This process demonstrates how branches interact, and how Gitโ€™s merge and rebase commands can be used to manage code development seamlessly.


โœ‰Endcard:

๐ŸŽ‰ Thank you for joining me on this insightful journey into the world of DevOps!

โค If you found this blog helpful and informative, don't forget to give it a like!

๐Ÿ”„ Share this valuable knowledge with your friends and colleagues, so they can also benefit from understanding the power of DevOps!

๐Ÿ‘‰ Stay updated with my latest posts and never miss out on exciting content! Click that Follow button to join and stay in the loop!

Follow me on Linkedin --> abdallah-qamar ๐Ÿ‘”

Stay tuned for Day 11...๐Ÿ‘‹

ย