Advance Git & GitHub for DevOps Engineers.

Advance Git & GitHub for DevOps Engineers.

#90DaysofDevOpsChallenge - Day-10

  • Git Branching

    1. Git branching is a feature of the Git version control system that enables developers to create separate lines of development within a repository.

    2. It allows multiple people or teams to work on different aspects of a project simultaneously without conflicts.

    3. Branches are movable pointers to specific commits, with the default branch typically named "master" or "main."

    4. By creating new branches, developers can isolate their changes from the main codebase until they're ready to merge them back.

    5. Git provides commands for creating, switching, and deleting branches, as well as merging changes from one branch to another.

    6. Different branching strategies, such as feature branching or GitFlow, can be used based on team preferences.

    Overall, Git branching facilitates efficient collaboration and version control in software development.

    To create a new branch, the following command can be used:

      git branch <branch_name>
    

    To change branches, the following command is used:

      git checkout  <branch_name>
    

    Git branching allows multiple people

  • Git Revert and Reset

    Git revert and reset are commands used to undo changes in a Git repository, but they have different behaviours.

    • Git revert creates a new commit that undoes the changes made in a previous commit, preserving the commit history. It is a safe way to undo commits without altering the repository's history.

        git revert <commit-id>
         # <commit-id> we can get this from the output of git log
      

    • Git reset, on the other hand, allows you to move the branch pointer to a specific commit, discarding subsequent commits. It can modify the repository's history and is more powerful but also more dangerous.

        git reset <commit_ID>
      

In summary, git revert undoes changes by creating new commits, while git reset moves the branch pointer and can erase commits from the branch's history.

  • Git Rebase and Merge

    • What Is Git Rebase?

      Git rebase is a command used to integrate changes from one branch onto another, resulting in a linear commit history.

      It replays the commits from the current branch onto a target branch. By using git rebase, you can incorporate changes while maintaining a cleaner commit history.

      However, it should be used with caution, especially when collaborating with others.

      Here's the basic syntax of the git rebase command:

        git rebase <target_branch>
      

      The <target_branch> parameter specifies the branch you want to rebase onto.

    • What Is Git Merge?

      Git merge is a command in Git that combines changes from multiple branches into a single branch, typically merging a feature branch into the main branch.

      It allows you to incorporate the changes made in one branch onto another branch, resulting in a unified codebase.

      When you perform a merge, Git identifies the common ancestor commit between the two branches being merged. It then combines the changes made in both branches and creates a new commit that represents the merged state.

      Here's the basic syntax of the git merge command:

        git merge < branch_name >
      

      The <branch_name> parameter specifies the branch from which you want to merge changes into the current branch.

      During the merge process, Git automatically combines the changes from the source branch with the current branch. If the changes in both branches do not conflict, Git applies the changes and creates a new commit with the merged content.

      However, if there are conflicting changes (i.e. the same lines of code were modified in both branches), Git prompts you to manually resolve the conflicts.

      To resolve conflicts, you need to edit the affected files, choosing which changes to keep and discarding conflicting ones.

      After resolving the conflicts, you can continue the merge by using git merge --continue. If you decide to abort the merge and return to the original state, you can use git merge --abort.

Tasks:-

  • 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 the master or main.

    we pushed our file to GitHub remote repository.

  • Now, we have created a new branch dev.

  • Added new commit in the dev branch after adding the below-mentioned content in Devops/Git/version01.txt

    While writing the file make sure we 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.

    restoring it to the first feature commit state. so first we check git log

    if the error comes like merging conflict then go to the version01.txt file and remove the line after feature2 then do revert continue.

    Task 2:-

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

Let's assume that we are working on a project that has a main branch, which contains very sensitive data but we want to work on a new feature on that project so what we will do, we make a new branch named dev

The below command is used for creating a new branch.

    git branch dev

A new dev branch is created, now we have to switch on the dev branch. for switching from main to dev:

    git checkout dev

Now we can add our files here and commit them to this dev branch. Now our the main branch is safe. But if we want to merge the dev and main branch, we must have to switch to the main branch and run the command for merging branches.

    git checkout main  # switching to main
    git merge dev # merging dev to the main

To view all branches we can use this command:

    git branch --all
    or
    git branch -a

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

    Before merging the branches :

    after merging the dev with the main branch.:

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

    Let's assume that we are working on a new dev branch and we have added 2 new features at that time our team member added a new commit file to the main branch.

    Now we have to rebase the dev branch feature to the main branch so we will checkout to the main branch pull the latest from the remote repo and rebase that dev branch.

So this was my explanation about git merge, reset, rebase and revert.

Thank you for taking the time to read this article on DevOps and #90daysofDevOps! I sincerely hope you found it helpful and informative.

If you have any questions or suggestions for improvements, please don't hesitate to reach out. Your feedback is valuable to me.

see you in the next blog ...

Wishing you continued success and happy learning on your DevOps journey!