Day 10: Advance Git & GitHub for DevOps Engineers.

Day 10: Advance Git & GitHub for DevOps Engineers.

Git Branching

Use a branch to isolate development work without affecting other branches in the 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 allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.

Git Revert and Reset

Two commonly used tools that git users will encounter are those of git reset and git revert. The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits.

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 logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.

What Is Git Merge?

Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.

The merge wording can be confusing because we have two methods of merging branches and one of those ways is called “merge,” even though both procedures do essentially the same thing.

Task 1:

Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside. This should be in a branch coming from master, [hint try git checkout -b dev], switch to dev branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]

  • version01.txt should reflect at the local repo first followed by the Remote repo for review. [Hint Use your knowledge of Git push and git pull commands here]

Add a 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>> This is the bug fix in development branch

  • Commit this with the message “ Added feature2 in development branch”

  • 2nd line>> This is gadbad code

  • Commit this with the message “ Added feature3 in the development branch

  • 3rd line>> This feature will gadbad everything from now.

  • Commit with the message “ Added feature4 in the development branch

Restore the file to a previous version where the content should be “This is the bug fix in the development branch” [Hint use git revert or reset according to your knowledge]

Solution :

    1. Create a new directory called Devops and a subdirectory called Git:
    mkdir Devops
    cd Devops
    mkdir Git
  1. Create a file called version01.txt in the Devops/Git directory and add the content "This is the first feature of our application" to it:
    cd Git
    touch version01.txt
    echo "This is first feature of our application" > version01.txt
  1. Create a new branch called dev from the master branch:
    git checkout -b dev
  1. Commit the changes to the dev branch:
    git commit -m "Added new feature"
  1. Push the changes to the remote repository:
    git push origin dev
  1. Add the following lines to the version01.txt file:
    1st line>> This is the bug fix in development branch
    2nd line>> This is gadbad code
    3rd line>> This feature will gadbad everything from now.
  1. Commit the changes to the dev branch:
    git commit -m "Added feature2 in development branch"
  1. Commit the changes to the dev branch again:
    git commit -m "Added feature3 in development branch"
  1. Commit the changes to the dev branch again:
    git commit -m "Added feature4 in development branch"
  1. Restore the file to a previous version where the content should be "This is the bug fix in the development branch" using the following Git command:
    git reset --hard HEAD~2

The --hard flag tells Git to reset the working directory and the staging area to the specified commit. The HEAD~2 syntax tells Git to reset to commit two before the current HEAD commit.

Task 2:

  • Demonstrate the concept of branches with 2 or more branches with a screenshot.

In Git, branches are used to diverge from the main codebase and work on separate features or fixes without affecting the main code until you're ready to merge your changes. Here's how you can create and work with branches:

  1. Create a New Branch: You can create a new branch using the git branch command. For example, to create a new branch called "feature-branch," you would run:

     git branch feature-branch
    
  2. Switch to the New Branch: To switch to the newly created branch, use the git checkout command:

     git checkout feature-branch
    

    Now, you're on the "feature branch," and any changes you make will be isolated from the main codebase.

  3. Make Changes: You can make changes to the code in this branch. These changes won't affect the main branch until you merge them.

  4. Create Another Branch: You can create additional branches if needed. For example, let's create a second branch called "bug-fix" using the same git branch command.

     git branch bug-fix
    
  5. Switch to the Second Branch: You can switch to the "bug-fix" branch just like before:

     git checkout bug-fix
    

  • Add some changes to dev branch and merge that branch in master

Here are the steps on how to add some changes to the dev branch and merge that branch into the master branch:

  1. Switch to the dev branch:
git checkout dev
  1. Make some changes to the files in the dev branch.

  2. Commit the changes to the dev branch:

git commit -m "Added some changes to the dev branch"
  1. Merge the dev branch into the master branch:
git merge dev

This will merge the changes from the dev branch into the master branch.

Here is an example of how to add some changes to the dev branch and merge that branch into the master branch:

// Switch to the dev branch
git checkout dev

// Make some changes to the files in the dev branch
echo "This is a new line in the dev branch" >> version01.txt

// Commit the changes to the dev branch
git commit -m "Added some changes to the dev branch"

// Merge the dev branch into the master branch
git merge dev

This will merge the changes from the dev branch into the master branch. The version01.txt file will now have the new line "This is a new line in the dev branch".

  • As a practice try git rebase too, and see what difference you get.

The main difference between the two commands is that git merge creates a new commit that merges the changes from the two branches, while git rebase applies the changes from the first branch to the second branch, effectively rewriting history.

Here is an example of how to use the git merge and git rebase commands:

// Create a new branch called "dev" from the "master" branch
git checkout -b dev

// Make some changes to the files in the dev branch
echo "This is a new line in the dev branch" >> version01.txt

// Commit the changes to the dev branch
git commit -m "Added some changes to the dev branch"

// Switch back to the master branch
git checkout master

// Merge the dev branch into the master branch
git merge dev

This will merge the changes from the dev branch into the master branch, creating a new commit.

// Rewrite history by rebasing the dev branch onto the master branch
git rebase master dev

This will apply the changes from the dev branch to the master branch, effectively rewriting history. The version01.txt file will now have the new line "This is a new line in the dev branch".

The git rebase command can be useful for cleaning up history or for fixing merge conflicts. However, it is important to note that it can also introduce new merge conflicts.

Let me know if you have any other queries.

Thank You

If you liked this blog then click on ❤

and do follow for more interesting and helpful blogs.

Did you find this article valuable?

Support GCP by becoming a sponsor. Any amount is appreciated!