# Day 9 - Deep Dive in Git & GitHub for DevOps Engineers

### Branching in Git

In Git, branching is a powerful feature that allows developers to work on multiple versions of a project simultaneously. Each repository starts with a default branch, typically named `main` or `master`. This default branch represents the main line of development.

### Creating and Switching Branches

To create a new branch in Git and switch to it, you use the following commands:

```bash
git checkout -b <branch_name>
```

This command creates a new branch named `<branch_name>` and switches your working directory to that branch. If you want to switch to an existing branch, you simply use:

```bash
git checkout <branch_name>
```

To see a list of all branches in your repository, including remote branches, use:

```bash
git branch -a
```

You don't need to clone the repository again to create branches; these commands work directly within your cloned repository.

### Creating Branches on GitHub

On GitHub, you can also create branches directly through the web interface:

1. Click on **"View all branches"** in your repository.
    
2. Click on **"New branch"**.
    
3. Provide a branch name and create the new branch.
    

Once you have your branch set up, you can add new changes to files, commit them, and push them to the remote repository from your branch.

### Merging Branches

To merge changes from your branch back into the stable branch (e.g., `main`), you typically create a **pull request** (PR):

1. Create a pull request on GitHub, where you can review the changes and request reviews from other developers.
    
2. After approval, the **merge** button becomes available, allowing you to merge your changes into the stable branch.
    

You can also merge branches using the `git merge` command:

```bash
git checkout main
git merge <your_branch_name>
```

### Handling Merge Conflicts

Merge conflicts occur when Git is unable to automatically merge changes from different branches. This usually happens when multiple developers modify the same line of a file. To resolve merge conflicts:

1. Git will mark the conflicting lines in your files.
    
2. Manually edit the files to resolve conflicts.
    
3. After resolving conflicts, add the resolved files (`git add`) and commit the changes.
    

### Reverting Changes

To revert changes to a specific commit, use the `git revert` command:

```bash
git revert <commit_id>
```

This command creates a new commit that undoes the changes introduced by the specified commit.

### Git Reset

Git reset allows you to move the `HEAD` pointer to a specific commit and optionally modify the index and working directory. There are three common modes of reset:

* **Soft Reset**: Moves `HEAD` to the specified commit, keeps changes in the index.
    
* **Mixed Reset**: Moves `HEAD` to the specified commit, keeps changes in the working directory.
    
* **Hard Reset**: Moves `HEAD` to the specified commit, discards all changes in the index and working directory.
    

Here's how you can use these reset options:

```bash
git reset --soft <commit_id>
git reset --mixed <commit_id>
git reset --hard <commit_id>
```

### Conclusion

Understanding branching in Git is crucial for managing complex development workflows. Branches allow teams to collaborate effectively, experiment with new features, and maintain stable codebases. By mastering Git branching strategies and commands like merge, revert, and reset, developers can navigate projects with confidence and efficiency.

~ Tushar Ranjan🙂
