Advance Git & GitHub for DevOps Engineers

Day 10 : #90DaysOfDevOps Challange

Version control is an essential aspect of software development. Git, a distributed version control system, is a popular tool that helps developers manage their codebases effectively. Git provides several powerful features that allow developers to work collaboratively, keep track of changes, and revert to previous versions of their code. In this article, we'll explore some of Git's most powerful features, including branching, reverting and resetting, rebasing, and merging.

Git Branching

Git branching is a powerful feature that allows developers to create multiple independent branches of their codebase. Branches are useful when developers want to work on a new feature or fix a bug without affecting the main codebase. Git allows developers to create branches easily and switch between them.

To create a new branch, use the git branch command followed by the name of the new branch. For example, to create a new branch called feature-branch, use the following command:

git branch feature-branch

To switch to a different branch, use the git checkout command followed by the name of the branch. For example, to switch to the feature-branch, use the following command:

git checkout feature-branch

Once you have made changes in your branch, you can merge it back into the main codebase using the git merge command. For example, to merge the feature-branch into the master branch, use the following command:

git checkout master
git merge feature-branch

Git Revert and Reset

Git revert and reset are two powerful features that allow developers to undo changes made to their codebase. Revert creates a new commit that undoes the changes made in a previous commit, while reset removes the changes from the current branch entirely.

To revert a commit, use the git revert command followed by the commit hash. For example, to revert the last commit, use the following command:

git revert HEAD

To reset a commit, use the git reset command followed by the commit hash. For example, to remove the last commit entirely, use the following command:

git reset --hard HEAD^

Git Rebase

Git rebase is a feature that allows developers to change the history of their codebase by modifying the order of commits. Rebase is useful when multiple developers are working on the same codebase and want to ensure that their commits are applied in the correct order.

To rebase a branch, use the git rebase command followed by the name of the branch you want to rebase onto. For example, to rebase the feature-branch onto the master branch, use the following command:

git checkout feature-branch
git rebase master

Git Merge

Git merge is a feature that allows developers to combine multiple branches into a single branch. Merge is useful when developers have multiple branches with different features or bug fixes that they want to combine into a single branch.

To merge a branch, use the git merge command followed by the name of the branch you want to merge. For example, to merge the feature-branch into the master branch, use the following command:

git checkout master
git merge feature-branch

Conclusion

Git provides several powerful features that allow developers to manage their codebases effectively. Git branching, reverting and resetting, rebasing, and merging are some of the most important features that developers can use to collaborate effectively and ensure that their code is stable and reliable. By mastering these features, developers can become more efficient and productive, and deliver high-quality code faster.

****************************Task Day 10************************************

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

swithch to dev branch ( Make sure your commit message will reflect as "Added new feature").

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

To add a text file called version01.txt inside the Devops/Git/ with "This is the first feature of our application" written inside, follow the steps below:

  1. Open your terminal or command prompt and navigate to the Devops/Git/ directory.

  2. Run the following command to create a new branch called dev and switch to it:

     git checkout -b dev
    

  3. Create a new file called version01.txt inside the Devops/Git/ directory with the following content:

     This is first feature of our application
    
  4. Add the new file to the Git repository and commit the changes with the message "Added new feature":

     git add version01.txt
     git commit -m "Added new feature"
    
  5. Push the changes to the remote repository for review:

     git push origin dev
    

To add new commit in dev branch after adding below mentioned content in Devops/Git/version01.txt, follow the steps below:

  1. Open the version01.txt file and add the following lines:

     This is the bug fix in development branch
    

  2. Add and commit the changes with the message "Added feature2 in development branch":

     git add version01.txt
     git commit -m "Added feature2 in development branch"
    

  3. Add the following lines to the version01.txt file:

     This is gadbad code
    

  4. Add and commit the changes with the message "Added feature3 in development branch":

     git add version01.txt
     git commit -m "Added feature3 in development branch"
    

  5. Add the following lines to the version01.txt file:

     This feature will gadbad everything from now.
    

  6. Add and commit the changes with the message "Added feature4 in development branch":

     git add version01.txt
     git commit -m "Added feature4 in development branch"
    

  7. To restore the file to a previous version where the content should be "This is the bug fix in development branch", follow one of the two methods below:

  8. Using git revert:

     git revert <commit hash>
    

    Replace <commit hash> with the hash of the commit where the content was changed. For example, to revert the last commit, run the following command:

     git revert HEAD
    

    1. Using git reset:

       git reset <commit hash> --hard
      

      Replace <commit hash> with the hash of the commit where the content was changed. For example, to reset to the commit with the message "Added feature2 in development branch", run the following command:

       git reset HEAD~2 --hard
      

      This will reset the version01.txt file to the state it was in after the "Added feature2 in development branch" commit.

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. Give step be step process

  1. Open a terminal or command prompt and navigate to the directory where you want to create your Git repository.

  2. Use the git init command to initialize a new Git repository. This will create a new directory called .git that contains all the necessary files for Git to work.

  3. Use the git branch command to create a new branch called dev. This will create a new branch that is identical to the current branch (in this case, master).

     git branch dev
    
  4. Use the git branch command again to list all branches in the repository. The current branch should be highlighted with an asterisk.

     git branch
    
  5. Switch to the dev branch using the git checkout command.

     git checkout dev
    
    1. Make some changes to a file in the dev branch. For example, let's add a new line to a file called file.txt.

       echo "This is a new line in dev branch" >> file.txt
      
    2. Use the git add command to stage the changes you made to the file.

       git add file.txt
      
      1. Use the git commit command to create a new commit with the changes you made to the file

         git commit -m "Added new line in dev branch"
        
        1. Switch back to the master branch using the git checkout command.

           git checkout master
          
          1. Merge the changes from the dev branch into the master branch using the git merge command.

             git merge dev
            
            1. Use the git log command to view the commit history of the repository. You should see a new commit that contains the changes from the dev branch.

               git log
              
            2. To practice Git rebase, switch back to the dev branch using the git checkout command.

               git checkout dev
              
            3. Make some more changes to the file.

               echo "This is another new line in dev branch" >> file.txt
              
            4. Use the git add command to stage the changes you made to the file.

                    git add file.txt
  1. Use the git commit command to create a new commit with the changes you made to the file.
                git commit -m "Added another new line in dev branch"
  1. Switch back to the master branch using the git checkout command.

     git checkout master
    
  2. Use the git log command to view the commit history of the repository. You should see two new commits from the dev branch.

     git log
    
  3. Use the git rebase command to rebase the dev branch onto the master branch.

     git rebase dev
    

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!