# Day-13 of #90DaysOfDevOps

# Advance Git & GitHub for DevOps Engineers

## Git Branching

Branches are a core concept in Git that allow you to isolate development work without affecting other parts of your repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request.

Branches let you develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

## Git Revert and Reset

Git reset and git revert are two commonly used commands that allow you to remove or edit changes you’ve made in the code in previous commits. Both commands can be very useful in different scenarios.

## Git Rebase and Merge

### What Is Git Rebase?

Git rebase is a command that lets users integrate changes from one branch to another, and the commit history is modified once the action is complete. Git rebase helps keep a clean project history.

### What Is Git Merge?

Git merge is a command that allows developers to merge Git branches while keeping the logs of commits on branches intact. Even though merging and rebasing do similar things, they handle commit logs differently.

For a better understanding of Git Rebase and Merge, check out this [article](https://www.simplilearn.com/git-rebase-vs-merge-article).

## Tasks

### Task 1: Feature Development with Branches

#### Create a Branch and Add a Feature

1. Add a text file called `version01.txt` inside the `Devops/Git/` directory with “This is the first feature of our application” written inside.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734412910692/45d47a3e-914d-4575-ab17-d445b997b15d.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734412960161/4a8d7bfd-be2a-4f15-873d-074a6f9432b9.png align="center")
    
2. Create a new branch from `master`:
    
    ```bash
    git checkout -b dev
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413037916/4fc272ee-d74f-4b31-aea7-dde098daaa4c.png align="center")
    
3. Commit your changes with a descriptive message:
    
    ```bash
    git add Devops/Git/version01.txt
    git commit -m "Added new feature"
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413064209/0519f7a7-1960-4d3a-9272-916a1b6d8777.png align="center")
    

#### Push Changes to GitHub

1. Push your local commits to the GitHub repository:
    
    ```bash
    git push origin dev
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413096546/f997af0b-8fd8-4f6b-80f1-5561c4780bfa.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413104522/b8695420-0fc7-41cf-8b71-d01f9f66a466.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413111299/5047e679-9582-4ada-8544-a0fa293d4c03.png align="center")
    

#### Add More Features with Separate Commits

1. Update `version01.txt` with additional lines, committing after each change:
    
    * 1st line: `This is the bug fix in development branch`
        
        ```bash
        echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
        git commit -am "Added feature2 in development branch"
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413129030/b3840fa7-070c-4fed-a1b8-fe779c7b6e60.png align="center")
        
    * 2nd line: `This is gadbad code`
        
        ```bash
        echo "This is gadbad code" >> Devops/Git/version01.txt
        git commit -am "Added feature3 in development branch"
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413139236/023cfa79-c9c1-44bc-ab5e-fa57c37475bf.png align="center")
        
    * 3rd line: `This feature will gadbad everything from now`
        
        ```bash
        echo "This feature will gadbad everything from now" >> Devops/Git/version01.txt
        git commit -am "Added feature4 in development branch"
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413155834/8a8b288d-6877-48e5-8c89-8a0ed51366e9.png align="center")
        
2. Verify the content of `version01.txt` after the updates.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413174321/2d1c90af-8c4e-423f-88af-292c08b57916.png align="center")
    

#### Restore the File to a Previous Version

1. Revert the file to where the content should be “This is the bug fix in development branch”:
    
    ```bash
    git revert HEAD~2
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413200265/7d30465f-1191-488b-9932-36af3b7187ab.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413209037/7be79894-bc10-46f4-8add-d592bcffc780.png align="center")
    

### Task 2: Working with Branches

#### Demonstrate Branches

1. Create 2 or more branches and take screenshots to show the branch structure.  
    **Image Placeholder:** Screenshot showing the branches.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413223852/6bc98e72-40bf-4e8b-ab7a-ea4d9a870016.png align="center")
    

#### Merge Changes into Master

1. Make changes to the `dev` branch and merge it into `master`:
    
    ```bash
    git checkout master
    git merge dev
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413241746/bb97926f-9d8f-4c6b-abdb-71154a793759.png align="center")
    

#### Practice Rebase

1. Rebase changes and observe the differences:
    
    ```bash
    git rebase master
    ```
    
    * Make changes in the `master` branch and commit them.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413262199/88b04690-f088-4734-bc4e-f9af57668c43.png align="center")
        
    * Switch to the `dev` branch and check file content before rebasing.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413275932/59157aa6-d0cb-42f4-a431-933af75e095f.png align="center")
        
    * Perform the rebase and verify the updated file content in `dev`.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734413290362/6a7fe5f2-214b-4de8-82fb-93e740a6280d.png align="center")
        

## Best Practices

Following best practices for branching is crucial. Check out these [best practices](https://www.flagship.io/git-branching-strategies/) commonly followed in the industry.

### References

* Simple Reference on branching: [video](https://youtu.be/NzjK9beT_CY)
    
* Advanced Reference on branching: [video](https://youtu.be/7xhkEQS3dXw)
    

---

Connect with me on [LinkedIn](https://linkedin.com/in/amitabh-devops) to stay updated on my DevOps journey!
