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.
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:
Git identifies the changes introduced by the commit you specify.
It creates a new commit that reverses those changes.
The previous commit remains intact in the history.
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
Feature | git revert | git reset |
History | Preserves history by creating a new commit. | Rewrites history (can be destructive). |
Collaborative Use | Safe for shared branches. | Can cause issues in shared repositories. |
Reverts Specific Changes | Only reverts the specified commit. | Moves the branch pointer, reverting multiple commits. |
Best Practices
Use
git revert
in shared branches likemain
ordevelop
to undo changes while maintaining a clear history.Always review the changes before committing the reversion using
git show
orgit 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:
Mode | Effect on Commit History | Effect on Staging Area | Effect on Working Directory |
--soft | Moves HEAD to <commit> | Keeps changes staged | No changes to working directory |
--mixed (default Mode) | Moves HEAD to <commit> | Unstages changes | No changes to working directory |
--hard | Moves HEAD to <commit> | Clears staging area | Overwrites working directory |
Key Differences: git reset
vs. git revert
Feature | git reset | git revert |
History | Modifies branch history (rewrites it). | Preserves history by adding a new commit. |
Impact | Can remove commits and changes. | Reverses specific commits without removing them. |
Safety | Not 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.
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:
History Preservation: Retains the full history of all branches.
Safe for Collaboration: No risk of rewriting history, making it ideal for shared repositories.
Disadvantages of Merge:
- 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 <branch_name>
Re-applies commits of the current branch on top of <branch_name>
.
Advantages of Rebase:
Clean History: Results in a linear and easier-to-read commit history.
Better for Feature Branches: Makes the branch look like it was developed directly on top of the target branch.
Disadvantages of Rebase:
History Rewriting: It rewrites commit hashes, which can cause issues in shared branches.
Risky for Collaboration: Should not be used on branches that others are working on, as it rewrites history.
Key Differences
Aspect | Merge | Rebase |
Operation Type | Combines changes by creating a merge commit. | Re-applies commits on top of another branch. |
Commit History | Creates a branching history. | Creates a linear history. |
Use in Collaboration | Safe for shared branches. | Risky; avoid on shared branches. |
History Rewriting | Does 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?
Concurrent Changes to the Same Line:
Example:
Branch
main
:Line 2: Hello from Main
Branch
feature-branch
:Line 2: Hello from Feature
File Edited and Deleted:
Example:
Branch
main
:file.txt
is modified.Branch
feature-branch
:file.txt
is deleted.
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
(orgit 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
Aspect | git fetch | git pull |
Fetch Changes | Yes | Yes |
Merge Changes | No | Yes (merge into the current branch) |
Safety | Safe to use without modifying your branch | Risky if there are conflicts or unreviewed changes |
Flexibility | Allows you to inspect changes before merging | Directly 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!๐๐