Day 10 - Task: Advance Git & GitHub for DevOps Engineers (Dec 6, 2023)

Day 10 - Task: Advance Git & GitHub for DevOps Engineers (Dec 6, 2023)

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 actually called “merge,” even though both procedures do essentially the same thing.

Refer to this article for a better understanding of Git Rebase and Merge Read here

Task 1:

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, [hint try git checkout -b dev], swithch 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 local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]

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

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

  • 2nd line>> This is gadbad code

  • Commit this with message “ Added feature3 in development branch

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

  • Commit with message “ Added feature4 in development branch

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

Task 2:

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

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

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

Task 1:

Let's break down the steps for Task 1:

  1. Create a new branch called "dev" and switch to it:
git checkout -b dev
  1. Create a text file named version01.txt in the Devops/Git/ directory with the specified content:
echo "This is the first feature of our application" > Devops/Git/version01.txt
  1. Add and commit the changes:
git add Devops/Git/version01.txt
git commit -m "Added new feature"
  1. Push the changes to the remote repository:
git push origin dev
  1. Add new commits in the dev branch:
echo "This is the bug fix in development branch" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature2 in development branch"

echo "This is gadbad code" >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature3 in development branch"

echo "This feature will gadbad everything from now." >> Devops/Git/version01.txt
git add Devops/Git/version01.txt
git commit -m "Added feature4 in development branch"

  1. Restore the file to a previous version:

Option 1: Using git reset:

git reset --hard HEAD^

Option 2: Using git revert:

git revert HEAD~3..HEAD

Task 2:

  1. Demonstrate the concept of branches with 2 or more branches:
# Create and switch to a new branch named feature_branch
git checkout -b feature_branch

# Make changes in the feature_branch
# ...

# Commit the changes in feature_branch
git add .
git commit -m "Added changes in feature_branch"

# Switch back to the dev branch
git checkout dev

# Merge the feature_branch into master
git merge feature_branch

  1. Practice git rebase:
# Create and switch to a new branch named feature_branch2
git checkout -b feature_branch2

# Make changes in the feature_branch2
# ...

# Commit the changes in feature_branch2
git add 

# Rebase feature_branch2 onto master
git rebase dev

# Resolve any conflicts if needed
# Continue the rebase with: git rebase --continue

# Switch back to the dev branch
git checkout dev

# Merge the rebased feature_branch2 into master
git merge feature_branch2

Please note that rebase rewrites commit history, so use it carefully and avoid rebasing commits that have been pushed to a shared repository.