Advance Git & GitHub for DevOps Engineers

Advance Git & GitHub for DevOps Engineers

ยท

7 min read

In this article, we will learn about:

  • Git Branching

  • Git Revert and Reset

  • Git Rebase and Merge

  • Merge Conflicts

Git Branching

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.

Git Branch

Working with git, we follow different types of git branch strategies, to know more about this check out this blog : https://medium.com/@sreekanth.thummala/choosing-the-right-git-branching-strategy-a-comparative-analysis-f5e635443423

Git Revert and Reset

git revert is a Git command used to undo changes made by a specific commit by creating a new commit. Unlike git reset, which can rewrite history, git revert maintains the integrity of the repository's history and is safe to use in shared or collaborative environments.

How Does git revert Work?

When you revert a commit:

  1. Git identifies the changes introduced by the commit you specify.

  2. It creates a new commit that reverses those changes.

  3. The previous commit remains intact in the history.

Use Git Reset - Manage Your Code Project With Git and GitHub -  OpenClassrooms

Syntax

git revert <commit_hash>

Revert Multiple Commits

To revert multiple commits, you can use the --no-commit option to stage the reversions and then commit them together.

git revert --no-commit C B
git commit -m "Revert changes from commits C and B"

-m 1: Indicates the main parent branch (e.g., main or master).

When to Use git revert

  • Undo a specific commit without rewriting history.

  • Fix mistakes in a shared branch.

  • Reverse unintended changes in a collaborative project.

Key Differences from git reset

Featuregit revertgit reset
HistoryPreserves history by creating a new commit.Rewrites history (can be destructive).
Collaborative UseSafe for shared branches.Can cause issues in shared repositories.
Reverts Specific ChangesOnly reverts the specified commit.Moves the branch pointer, reverting multiple commits.

Best Practices

  • Use git revert in shared branches like main or develop to undo changes while maintaining a clear history.

  • Always review the changes before committing the reversion using git show or git diff.

What is git reset?

git reset is a Git command used to undo changes by moving the HEAD and (optionally) the current branch pointer to a specified commit. It can modify the working directory, staging area, or both, depending on the reset mode.

How Does git reset Work?

When using git reset, you essentially move the branch pointer to another commit, effectively "forgetting" the commits that come after it in the branch. However, the scope of the impact depends on the chosen reset mode.


Modes of git reset

There are three main reset modes:

ModeEffect on Commit HistoryEffect on Staging AreaEffect on Working Directory
--softMoves HEAD to <commit>Keeps changes stagedNo changes to working directory
--mixed(default Mode)Moves HEAD to <commit>Unstages changesNo changes to working directory
--hardMoves HEAD to <commit>Clears staging areaOverwrites working directory

Key Differences: git reset vs. git revert

Featuregit resetgit revert
HistoryModifies branch history (rewrites it).Preserves history by adding a new commit.
ImpactCan remove commits and changes.Reverses specific commits without removing them.
SafetyNot safe in shared branches.Safe to use in shared branches.

Best Practices

  • Use git reset cautiously, especially in collaborative projects.

  • Prefer --soft or --mixed for reversible operations.

  • Avoid --hard unless you're sure the changes are unnecessary or backed up.

Git Merge vs. Git Rebase

git merge combines the changes from one branch into another by creating a new merge commit. It preserves the complete history of the original branches.

Difference Between Git Merge, Rebase and Squash - DEV Community

Syntax

git merge <branch_name>
  • Merges <branch_name> into the current branch.

  • Creates a new commit to combine the histories of both branches.

Advantages of Merge:

  1. History Preservation: Retains the full history of all branches.

  2. Safe for Collaboration: No risk of rewriting history, making it ideal for shared repositories.

Disadvantages of Merge:

  1. Messy History: Can lead to a cluttered commit history with multiple merge commits.

Git Rebase

What It Does:

git rebase rewrites the commit history by applying changes from one branch on top of another branch. It creates a linear history.

How It Works:

Git Rebase vs. Git Merge: What's the Difference? | phoenixNAP KB

git rebase <branch_name>

Re-applies commits of the current branch on top of <branch_name>.

Advantages of Rebase:

  1. Clean History: Results in a linear and easier-to-read commit history.

  2. Better for Feature Branches: Makes the branch look like it was developed directly on top of the target branch.

Disadvantages of Rebase:

  1. History Rewriting: It rewrites commit hashes, which can cause issues in shared branches.

  2. Risky for Collaboration: Should not be used on branches that others are working on, as it rewrites history.

Key Differences

AspectMergeRebase
Operation TypeCombines changes by creating a merge commit.Re-applies commits on top of another branch.
Commit HistoryCreates a branching history.Creates a linear history.
Use in CollaborationSafe for shared branches.Risky; avoid on shared branches.
History RewritingDoes not rewrite history.Rewrites commit history.

What Are Merge Conflicts in Git?

A merge conflict occurs when Git cannot automatically combine changes from two branches because both branches have made conflicting changes to the same part of the codebase. Git pauses the merge process and asks you to resolve the conflicts manually.

When Do Merge Conflicts Happen?

  1. Concurrent Changes to the Same Line:

    • Example:

      • Branch main: Line 2: Hello from Main

      • Branch feature-branch: Line 2: Hello from Feature

  2. File Edited and Deleted:

    • Example:

      • Branch main: file.txt is modified.

      • Branch feature-branch: file.txt is deleted.

  3. Unmerged Changes: When you have uncommitted changes in your working directory that conflict with the branch being merged.

Bonus Point :

The difference between git pull and git fetch ?

git fetch

  • Purpose:
    Updates your local copy of the remote repository without altering your working directory.

  • What it does:

    • Downloads the latest commits, branches, and tags from the remote repository.

    • Updates your local references (e.g., origin/main) to match the remote state.

    • Does not merge or change your working files.

  • Use case:
    When you want to review the changes from the remote before integrating them.

git pull

  • Purpose:
    Fetches updates from the remote repository and automatically integrates them into your current branch.

  • What it does:

    • Combines git fetch + git merge (or git rebase, depending on your configuration).

    • Downloads the latest changes and merges them into your current branch.

  • Use case:
    When you want to update your branch and incorporate changes immediately.

Key Differences

Aspectgit fetchgit pull
Fetch ChangesYesYes
Merge ChangesNoYes (merge into the current branch)
SafetySafe to use without modifying your branchRisky if there are conflicts or unreviewed changes
FlexibilityAllows you to inspect changes before mergingDirectly integrates changes into the branch

Conclusion

By following these commands, you can understand how Git works and how you can use these commands and concepts in your projects to avoid any conflicts.

Follow for more useful content!๐Ÿ™ƒ๐Ÿ™ƒ

ย