# Day 10 : Git and GitHub Advance

# ⭐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:

```bash
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:
    
    ```bash
    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:

```bash
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.

```bash
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.

```bash
git rebase master
```

# Tasks📚:

## 1.) Branch, Commit & Reset

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

```plaintext
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".*

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691824484843/37e6f2ee-f443-429d-8594-39e0a59ac990.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691824503786/7114825c-6ed5-474b-920a-4e912eb81a58.png align="center")

After reviewing the changes, add the remaining features.

2nd Feature: *"This is gadbad code"*

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691824752717/063ed642-1d27-43b3-b94e-b24f51028518.png align="center")

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.

```bash
git log --oneline
git reset <COMMIT_ID>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691825401936/ca0c907f-1e49-47c4-ac8b-6e207a180397.png align="center")

# 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.

```bash
git merge <BRANCH_NAME>
```

Resolve conflicts if any, then check logs.

```bash
git log --oneline
```

Now, let's try again with `git rebase`.

Switch to `main` branch.

```bash
git checkout main
```

Then, execute this command.

```bash
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 --&gt; [abdallah-qamar](https://www.linkedin.com/in/abdallah-qamar-632820235/) 👔

**Stay tuned for Day 11...👋**
