Day10: Advance Git and GitHub for DevOps

Day10: Advance Git and GitHub for DevOps

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.

Tasks for Hands-on

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 the dev branch ( Make sure your commit message will reflect as "Added new feature"). (Hint use your knowledge of creating branches and Git commit commands).

  • 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 faulty code

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

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

  • 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]

Steps:

  • First, create a file version01.txt inside the repository of the master branch with "This is the first feature of our application" written inside the text file you created.

  • Create a new branch name "dev" by using git checkout -b dev. It will create and switch branches.

  • Use git status to check the unstaged file which is version01.txt. To stage the file use git add version01.txt command.

  • After staging the file, to commit use git commit -m "Added new feature" command. -m used for a commit message.

  • Then use git push origin dev command. It pushes the dev branch and its changes to the remote repository.

  • Re-edit the file version01.txt and write This is the bug fix in the development branch.

  • Add the file using git add version01.txt and then commit the file using git commit -m "Added feature2 in development branch".

  • Re-edit the txt file and write "this is faulty code" in it.

  • Add the file using git add version01.txt and then commit the file using git commit -m "Added feature3 in the development branch".

  • Re-edit the file and write "This feature will fault everything from now on" in it.

  • Again add the file using git add version01.txt and then commit the file using git commit -m "Added feature4 in the development branch".

  • To restore the file to a previous version, use git reset command.

Task 2:

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

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

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

Steps:

  • Create a new branch using git checkout -b dev.

  • Create a Txt file in the dev branch and write something in it.

  • Use git add . command to stage the file and for commit use git commit -m "added changes to dev branch"

  • Create a new branch git checkout -b feature command. A feature branch will be created.

  • To add changes, create a Txt file in the feature branch and write something in it.

  • Use git add . command to stage the file and for commit use git commit -m "added changes to feature branch"

  • Use git checkout dev to switch the existing branch and use the git merge feature command to add all the changes of the feature branch to the dev branch.

  • Then go master branch by using the git checkout command and use the git merge dev command to add all the changes of the dev branch to the master branch. Simple as that.

  • For rebase, switch to the feature branch again and use the git rebase dev command. This will move your "feature" branch to the tip of the dev branch, resulting in a linear history.

<Thank you for reading till THE END. Follow and join me in the journey of DevOps>