🟠 Git Branching
Git branching is like creating different versions of your project. It allows you to work on new features or fix issues without affecting the main or stable version of your code. To create a new branch in Git, you can use the git branch
command.
Overview of how branching works in Git with an example:
Creating a Branch: To create a new branch, you use the
git branch
command followed by the name of the new branch. For example:git branch new-feature
Switching Branches: You can switch between branches using the
git checkout
command followed by the name of the branch you want to switch to. For example:git checkout new-feature
Adding Commits: Once you're on the new branch, you can make changes to your code as usual and commit them using
git add
andgit commit
.Merging Branches: After you've finished working on your feature or fix, you can merge the changes back into the main branch (often
master
ormain
) using thegit merge
command. For example:git checkout main git merge new-feature
Resolving Conflicts: Sometimes, when you merge branches, Git may encounter conflicts if changes were made to the same part of a file in both branches. In such cases, you'll need to manually resolve the conflicts before completing the merge.
Deleting Branches: Once you've merged a branch and no longer need it, you can delete it using the
-d
option with thegit branch
command. For example:git branch -d new-feature
🟠 Git Revert and Reset
The git revert and git reset commands are both used to undo changes in Git. However, there are some key differences between the two commands.
git revert
is used to create a new commit that undoes the changes made in a previous commit. git revert is the safer option to use, as it does not rewrite the commit history.
You can use git revert
:
Identify the Commit to Revert: You need to find the commit hash of the commit you want to revert. You can do this using
git log
.Revert the Commit: Use
git revert
followed by the commit hash. For example:git revert xyz90897
This will create a new commit that undoes the changes introduced by the specified commit.
git reset
is used to reset the current branch to a specific commit or undo changes. git reset should only be used if you are sure that you want to rewrite the commit history.
you can use git reset
:
Identify the Commit or Reference: You need to specify the commit hash, branch name, or any other reference to indicate the state you want to reset to.
Choose Reset Mode:
Soft Reset (--soft): Moves the HEAD to the specified commit but keeps changes in the index and working directory.
Mixed Reset (--mixed): Default mode. Resets the index to the specified commit but keeps changes in the working directory.
Hard Reset (--hard): Resets both the index and the working directory to the specified commit. This is the most aggressive mode and can result in the loss of uncommitted changes.
Here are examples of each reset mode:
Soft Reset:
git reset --soft xyz2389
Mixed Reset:
git reset --mixed pqr9089
Hard Reset:
git reset --hard qwer0098
🟠 What is Git Rebase?
Git rebase is a command that allows you to move or combine commits on a branch. This can be useful for cleaning up your commit history or for integrating changes from another branch.
git rebase <base_branch> #basic syntax for Git Rebase
Here's a simple example of how git rebase
works:
Let's say you have two branches: main
and feature
. Both branches have diverged, meaning there are commits on both branches since they were last in sync.
Initial Commit History:
codemain: P---Q---R \ feature: S---T
Starting a Rebase: You're on the
feature
branch and want to incorporate changes frommain
intofeature
. You initiate the rebase:git checkout feature git rebase main
Reapplying Commits: Git will pause the rebase process if it encounters any conflicts that need to be resolved. Assuming there are no conflicts, Git will automatically reapply each commit from
feature
onto the tip ofmain
.Final Commit History: After the rebase is complete, the commit history might look like this:
codemain: P---Q---R \ feature: S'---T'
Here,
D'
andE'
are new commits, which are the same changes asD
andE
but with potentially different commit hashes due to the rebase operation. Thefeature
branch now contains the changes frommain
as well, but in a linear fashion.Pushing Changes: Finally, you can push the rebased
feature
branch to the remote repository:git push origin feature
And that's a simple example of how git rebase
works.
🟠 What is Git Merge?
Git merge is a command used to combine changes from two or more branches into a single branch. It is a powerful tool that allows developers to work on different features or bug fixes in parallel, and then merge their changes together when they are ready.
git merge <source_branch> #basic syntax for Git Merge
✤Example 1: Merge Feature Branch into Main Branch
Suppose you have a main
branch and a feature
branch. You've completed work on the feature
branch and want to merge those changes into main
.
Checkout the Main Branch: First, ensure you're on the
main
branch:git checkout main
Merge Feature Branch: Now, you merge the
feature
branch intomain
:git merge feature
This command integrates the changes from the
feature
branch into themain
branch.Resolve Conflicts (if any): If there are conflicts during the merge process (e.g., if changes were made to the same part of a file in both branches), Git will pause and prompt you to resolve them. You can use
git status
,git diff
, andgit add
to resolve conflicts.Commit the Merge: After resolving conflicts, commit the merge:
git commit
Push the Merged Changes: Finally, push the merged changes to the remote repository:
git push origin main
✸Conclusion:
In conclusion, mastering advanced Git and GitHub techniques empowers DevOps engineers to streamline development workflows, improve collaboration, and accelerate software delivery, making them indispensable assets in modern software development environments.
Happy Learning : )