Day 10 : Advance Git & GitHub for DevOps Engineers.

ยท

4 min read

What is Git Branch? | How to Create a New Git Branch? | Examples

What is Git Branching and its type.

Git branching is a feature in the Git version control system that allows developers to create separate lines of development within a project. Think of it like having multiple copies of your project where you can work on different features or fixes without affecting the main codebase.

Here are some common types of branches:

  1. Main/Branch: This is typically the default branch in a Git repository. It represents the stable version of the project. It's also sometimes referred to as master or main.

  2. Feature Branch: This type of branch is used to develop new features or functionalities. It's created off the main branch and merged back into it once the feature is complete. For example:

     git checkout -b new-feature main
    
  3. Release Branch: This branch is created when preparing for a new release of the software. It's used for finalizing changes and fixing any last-minute bugs before the release is made public. For example:

     git checkout -b release-1.0 main
    
  4. Hotfix Branch: In case of critical bugs or issues discovered in the released version of the software, a hotfix branch is created. It's used to quickly address the problem and merge the fix into both the main branch and any active release branches. For example:

     git checkout -b hotfix-1.1.1 main
    

Here are some common commands used with Git branching:

  • git branch: Lists all the branches in the repository.

      git branch
    
  • git checkout -b : Creates a new branch off the specified base branch and switches to it.

      git checkout -b new-feature main
    
  • git checkout : Switches to the specified branch.

      git checkout new-feature
    
  • git merge : Merges the changes from the specified branch into the current branch.

      git merge new-feature
    
  • git branch -d : Deletes the specified branch.

      git branch -d new-feature
    
  • git branch -D : Forces deletion of the specified branch, even if it contains unmerged changes.

      git branch -D new-feature
    

What is Git Revert and Reset?

Git revert and reset are both commands used to undo changes in a Git repository, but they work in slightly different ways.

  1. Git Revert:

    • Git revert is used to create a new commit that undoes the changes made by a specific commit or range of commits.

    • It's a safe way to undo changes because it preserves the commit history and doesn't alter existing commits.

    • Revert is often preferred for undoing changes that have already been pushed to a shared repository.

    • Example:

        git revert <commit-hash>
      

      This command will create a new commit that undoes the changes introduced by the specified commit.

  2. Git Reset:

    • Git reset is used to move the HEAD pointer and possibly the index (staging area) to a different commit, effectively resetting the state of the repository.

    • It can be used to undo changes by discarding commits or unstage changes.

    • Resetting can be a more aggressive operation compared to revert because it alters the commit history.

    • Example:

        git reset --hard HEAD~1
      

      This command will reset the repository to the state of one commit before the current HEAD, discarding the latest commit and all changes made in it.

Difference:

  • Revert: Creates a new commit to undo changes while keeping the commit history intact.

  • Reset: Moves the HEAD pointer and possibly the index to a different commit, altering the commit history.

What is Git Rebase and Merge?

Git rebase and merge are both ways to integrate changes from one branch into another, but they do so in different ways.

  1. Git Rebase:

    • Git rebase integrates changes by moving the commits from one branch to another, reapplying them onto the target branch's tip.

    • It results in a linear history, making the commit history cleaner and easier to follow.

    • Rebase is useful for keeping a clean and linear history, especially in collaborative environments.

    • Example:

        git checkout feature-branch
        git rebase main
      

      This command will move the commits from feature-branch onto the tip of main.

  2. Git Merge:

    • Git merge integrates changes by combining the commits from one branch into another, creating a merge commit to represent the merge.

    • It preserves the commit history of both branches, resulting in a more complex but accurate representation of the development process.

    • Merge is useful for integrating feature branches into the main development branch.

    • Example:

        git checkout main
        git merge feature-branch
      

      This command will merge the changes from feature-branch into main, creating a merge commit.

Difference:

  • Rebase: Moves commits from one branch to another, resulting in a linear history. It's good for keeping a clean and linear history but can rewrite commit history.

  • Merge: Combines commits from one branch into another, preserving the commit history of both branches. It's good for integrating feature branches into the main branch without altering history.

ย