Advance Git & GitHub for DevOps Engineers: #Day10 of 90DaysofDevOps

Git Branching

Git branching is a strategy to use a branch to isolate development work without affecting other branches in the repository.

Branching facilitates the development of bug fixes, the addition of new capabilities and the integration of new versions after developers have been tested in isolation.

Git Revert and Reset

The git revert command is used to create a new commit that undo the changes made by a previous commit. It is useful when you want to undo a commit that has already been pushed to a remote repository, without deleting the commit.
Syntax: git revert <commit-ID>

The git reset command is used to reset the state of the repository to a specific point in the commit history. This can be useful for undoing changes, reverting to a previous commit.
Syntax: git reset <commit-ID>

Git Rebase and Merge

What is Git Rebase?

Git rebase integrates the changes from one branch to another. Rebasing is the process of combining or moving a sequence of commits on top of a new base commit. Git rebase is the linear process of merging.

What is Git Merge?

Git merge is used to merge changes from one branch to another branch. When you want to merge changes from a feature branch into a main branch, you can use git merge to combine the changes.

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

  1. To complete this task, I am using the existing repository ‘Devops’ that was created for the Day09 task.

  2. Create a new folder as ‘Git_Day10’ inside Devops.

  3. Initialize the Git repository at Git_Day10

  4. Create a file ‘version01.txt’

  5. Switch to the new branch ‘dev’

  6. Write messages in the file.

  7. Add commits locally

  8. Push file from local to the remote repository dev branch

  9. Add new commits in the dev branch

  10. You can see your all commits through the ‘git log’ command

  11. 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]

---------------------------------------------------------------------------------------

Task 2:

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

    GitFlow is a branching model that includes a set of guidelines that developers can follow when using Git to avoid merge conflicts.

    Developers may need to work on multiple branches to develop the code and get the code ready for deployment.

  • The typical Gitflow branching model used in larger projects is as follows:

    Master: Represents the stable and production-ready codebase.

    Develop: Serves as the integration branch for ongoing development.

    Feature: Used for adding new features or changes to the codebase. Created from the develop branch and merged back when complete.

    Release: Created when preparing for a new release. It allows for bug fixes and preparation for the release. Merged into both 'develop' and 'master' branches.

    Bug: In case there is a critical bug in the current deployed code then the Bugfix branch will be created from the master to start fixing the code bugs. This branch will be deleted once the fix is merged with the master.

    Few git commands:

    Create a new branch: git branch -b <branch_name>
    Show all branches: git branch

    For going to a specific branch: git checkout <branch_name>

    For deleting branch: git checkout -d <branch_name>

    For creating and going to that branch: git checkout -b <branch_name>

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

    When I switched to master branch and tried to merge the dev branch with the master, I received the error "Falat : refusing to merge unrelated histories"

    When I searched about this error, I got to know the "refusing to merge unrelated histories" error message occurs when you are trying to merge two branches that have diverged and don't share a common ancestor. This typically happens when you have initialized a new repository and made separate commits on the dev and master branches without any common commit or commit history.

    Here's how to resolve the issue successfully:

    Ensure Both Branches are Up-to-date: First, ensure that both the dev and master branches are up-to-date with the remote repository. Run the following commands to fetch and pull the latest changes:
    Switch to the master branch : git checkout master

    Fetch the latest changes from the remote repository :git fetch

    Pull the latest changes from the remote master branch :git pull origin master

    Switch to the dev branch : git checkout dev

    Fetch the latest changes from the remote repository :git fetch

    Pull the latest changes from the remote dev branch :git pull origin dev

Merge with --allow-unrelated-histories: Now, try to merge the dev branch into master using the --allow-unrelated-histories flag, which allows Git to merge unrelated histories:
git checkout master git merge dev --allow-unrelated-histories

Resolve Conflicts (if any): If there are any conflicts between the two branches, Git will prompt you to resolve them. Open the conflicting files, make the necessary changes to resolve the conflicts, save the files, and then continue the merge with the following commands:
After resolving conflicts, add the changes : git add .

Continue the merge : git commit

Push the Changes: Finally, push the merged changes to the remote repository:
git push origin master

Thank you for reading this article. Happy Learning !!!😊