# Advance Git & GitHub for DevOps Engineers

Version control is an essential aspect of software development. Git, a distributed version control system, is a popular tool that helps developers manage their codebases effectively. Git provides several powerful features that allow developers to work collaboratively, keep track of changes, and revert to previous versions of their code. In this article, we'll explore some of Git's most powerful features, including branching, reverting and resetting, rebasing, and merging.

### Git Branching

Git branching is a powerful feature that allows developers to create multiple independent branches of their codebase. Branches are useful when developers want to work on a new feature or fix a bug without affecting the main codebase. Git allows developers to create branches easily and switch between them.

To create a new branch, use the `git branch` command followed by the name of the new branch. For example, to create a new branch called `feature-branch`, use the following command:

```plaintext
git branch feature-branch
```

To switch to a different branch, use the `git checkout` command followed by the name of the branch. For example, to switch to the `feature-branch`, use the following command:

```plaintext
git checkout feature-branch
```

Once you have made changes in your branch, you can merge it back into the main codebase using the `git merge` command. For example, to merge the `feature-branch` into the `master` branch, use the following command:

```plaintext
git checkout master
git merge feature-branch
```

### Git Revert and Reset

These two commands are used in Git to undo changes to a project code and history but in different ways.

### Git Revert

`Git revert` is similar to `git reset`, but the approach is slightly different. Instead of removing all the commits in its way, the revert ONLY undoes a single commit by taking you back to the staged files before the commit.

So, instead of removing a commit, `git revert` inverts the changes introduced by the original commit by creating a new commit with the underlying inverse content. This is a safe way to revoke a commit because it prevents you from losing your history.

It is important to know that the revert will have no effect unless the commit hash or reference is specified.

**This command helps you in reverting a commit, to a previous version**

```plaintext
git revert    <commit-id>
<commit-id>    can    be    obtained from the output of    git log
```

### **Git Reset**

The `git reset` command is used to undo the changes in your working directory and get back to a specific commit while discarding all the commits made after that one.

For instance, imagine you made ten commits. Using git reset on the first commit will remove all nine commits, taking you back to the first commit stage. Before using `git reset`, it is important to consider the type of changes you plan to make. You can use multiple options along with `git reset`, but these are the main ones. Each one is used depending on a specific situation: `git reset --soft`, `git reset --mixed`, and `git reset --hard`

**Command:-**

```plaintext
git reset <commit_ID>
```

![Use Git Reset - Manage Your Code Project With Git and GitHub -  OpenClassrooms](https://user.oc-static.com/upload/2022/01/04/16412698123429_image46.jpg align="left")

## **Git Rebase and Merge**

### Git Rebase

Git rebase is a feature that allows developers to change the history of their codebase by modifying the order of commits. Rebase is useful when multiple developers are working on the same codebase and want to ensure that their commits are applied in the correct order.

To rebase a branch, use the `git rebase` command followed by the name of the branch you want to rebase onto. For example, to rebase the `feature-branch` onto the `master` branch, use the following command:

```plaintext
git checkout feature-branch
git rebase master
```

### Git Merge

Git merge is a feature that allows developers to combine multiple branches into a single branch. Merge is useful when developers have multiple branches with different features or bug fixes that they want to combine into a single branch.

To merge a branch, use the `git merge` command followed by the name of the branch you want to merge. For example, to merge the `feature-branch` into the `master` branch, use the following command:

```plaintext
git checkout master
git merge feature-branch
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680113686782/ec599d11-90f4-47c1-ae3e-003ddd11a8db.jpeg?auto=compress,format&format=webp align="left")

## **Tasks:-**

**01\. Add a text file called version01.txt inside the DevOps/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from** `master`, swithch to `dev` branch ( Make sure your commit message will reflect as "Added new feature").

Here are the steps to add a text file called `version01.txt` inside the `Devops/Git/` directory with "This is first feature of our application" written inside. This should be in a branch coming from the `master` and you should switch to the `dev` branch with a commit message reflecting "Added new feature":

1. Open a terminal or command prompt.
    
2. Navigate to the directory where you want to create the Git repository.
    
3. Run the command `git init`. This will initialize a new Git repository in the current directory.
    
4. Run the command `git checkout -b dev`. This will create a new branch called `dev` and switch to it.
    
5. Create a new file called `version01.txt` in the `Devops/Git/` directory. You can do this by running the command `touch Devops/Git/version01.txt`.
    
6. Open the `version01.txt` file in a text editor.
    
7. Write “This is the first feature of our application” inside the file.
    
8. Save the file and close the text editor.
    
9. Add the file to the staging area by running the command `git add Devops/Git/version01.txt`.
    
10. Commit the changes to the repository with a commit message reflecting “Added new feature” by running the command `git commit -m "Added new feature"`.
    
11. Push the changes to the remote repository by running the command `git push -u origin dev`.
    

Now you have created a new branch called `dev` and added a new file called `version01.txt` with the message "This is the first feature of our application" inside it. The changes have been committed to the repository with a commit message reflecting "Added new feature”. The changes have also been pushed to the remote repository.

**Add new commit in** `dev` branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines

* 1st line&gt;&gt; This is the bug fix in the development branch
    
* Commit this with the message “ Added feature2 in development branch”
    

1. Run the command `git checkout dev`. This will switch to the `dev` branch.
    
2. Open the `version01.txt` file in a text editor.
    
3. Add the content “This is the bug fix in the development branch” as the first line in the file.
    
4. Save the file and close the text editor.
    
5. Add the changes to the staging area by running the command `git add Devops/Git/version01.txt`.
    
6. Commit the changes to the repository with a commit message reflecting “Added feature2 in development branch” by running the command `git commit -m "Added feature2 in development branch"`.
    
7. Push the changes to the remote repository by running the command `git push origin dev`.
    

* 2nd line&gt;&gt; This is gadbad code
    
* Commit this with the message “ Added feature3 in the development branch"
    
* 3rd line&gt;&gt; This feature will gadbad everything from now.
    
* Commit with the message “ Added feature4 in the development branch"
    

Do steps 03 and 06 with changes in the content of text and message while committing the changes.

**02\. Restore the file to a previous version where the content should be “This is the bug fix in the development branch”**

To restore the `version01.txt` file to a previous version where the content is "This is the bug fix in development branch", you can use the `git checkout` command followed by the commit hash or branch name of the version you want to restore. Here are the steps:

1. Open a terminal or command prompt.
    
2. Navigate to the directory where your Git repository is located.
    
3. Run the command `git log --oneline`. This will display a list of commits in the repository with their commit hashes and commit messages.
    
4. Identify the commit hash or branch name of the version you want to restore. Look for the commit message “Added feature2 in development branch” or any other identifying information.
    
5. Run the command `git checkout <commit hash or branch name> Devops/Git/version01.txt`. Replace `<commit hash or branch name>` with the hash or name of the version you want to restore.
    
6. Verify that the `version01.txt` file has been restored to the correct version by opening it in a text editor and checking that the content is "This is the bug fix in the development branch".
    

***Thank you for reading!!!***  
**~Bhaarat**
