Deep Dive Into Git And GitHub

Deep Dive Into Git And GitHub

·

4 min read

Git Branching 🌿

  • To separate development work from other branches in the repository, use a branch. Each repository has a default branch and the option to have additional branches as well. A pull request can be used to merge one branch into another.

    Using branches, you can safely experiment with new ideas, develop features, or fix bugs in a specific area of your repository.

  • For example , Imagine you are drawing a picture, and you want to try out different designs without ruining your original drawing. Git branching is like making copies of your picture on separate sheets of paper. Each copy is a different version with its own changes. You can draw and experiment on these copies without affecting the original picture. This way, you can see different ideas and decide which one you like best.

      # syntax to add branch 
      git branch <branch name>
      # to create a branch and switch to the branch
      git checkout -b <branch name>
      # to switch the branch
      git switch <branch name>
    

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.

  • Think of Git Revert like having an "undo" button for your drawing. If you accidentally draw something you don't like, you can click "undo," and the mistake will disappear, but you'll still have the original drawing.

  • Git Reset is like going back in time to a specific point in your drawing process. It's like erasing everything after that point and starting over from there. But be careful! It will erase all the progress you made after that point.

      # syntax to reset ,Moves the branch pointer, resets the staging area, 
      # and discards all changes in the working directory 
      git reset --hard <cimmit-hash>
      # Moves the branch pointer, 
      #keeping the changes in the staging area.
      git reset --soft <commit-hash>
      # syntax to revert
      git revert <commit-hash>
    

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.

  • Suppose you are building a Lego castle with a friend, and you both build different parts separately. When you want to put the pieces together, you need to align them so that they fit perfectly. Git rebase is like adjusting the pieces to fit seamlessly. You take your friend's pieces and rearrange them, so they blend smoothly with your parts of the castle.

      git rebase <source-branch>
    

Git Merge 🤖

  • Git merge combines the changes from one branch into another branch, creating a new "merge commit" that represents the integration of both branches. The merge commit has multiple parents, one for each branch involved in the merge.

  • Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact

Hands-On Learning ⌨️

  • Let's understand this by doing few tasks

  • We have already Created a repo called Devops on GitHub on our last blog ( https://srdev.hashnode.dev/deep-dive-into-git-github ) , so we don't have to do the same again or we can simply clone it from GitHub

  • We create a file version01.txt and edit the file as given in task, commit with the commit message as mentioned.

  • After doing the above steps , we create a new branch and shift to that branch using "git checkout -b branch name" and push all the changes to that branch.

  • Now , we have to edit the version01.txt file and commit for each update as given in the task .

  • Now , we will check the log using "git log " and restore the file to previous version where the content is "This is the bug fix in development branch" or the commit message is " Added feature 2 in development branch", for this we can use git reset or git revert.

    Task 2 :

  • We can create new branches by using command " git branch <branch name>".

  • We can merge the branch using command " git merge <source-branch>".

  • Let's try using "git rebase <source-branch>

I hope you were able to do the tasks , go through the previous blog( https://srdev.hashnode.dev/deep-dive-into-git-github ) to understand how to create repo , add and commit

Stay tuned for furthere updates

Happy Learning :D