☞Git Branching
Git branching is a fundamental concept in version control that allows developers to work on multiple independent lines of development within a single repository. Branches are essentially pointers to specific commits, enabling developers to isolate work on new features, bug fixes, or experiments without affecting the main codebase.
Commands:
List Branches:
git branch
This command lists all branches in the repository, highlighting the currently active branch.
Create a New Branch:
git branch <branch_name>
Use this command to create a new branch with the specified name. The new branch will be based on the commit you are currently on.
Switch to a Branch:
git checkout <branch_name>
To switch to an existing branch, use this command. It updates the working directory to match the selected branch's state.
Create and Switch to a New Branch (Shortcut):
git checkout -b <new_branch_name>
This shortcut command creates a new branch and switches to it in a single step.
Merge Branches:
git merge <branch_name>
Merging integrates changes from the specified branch into the currently active branch. It creates a new merge commit if there are conflicting changes.
Delete a Branch:
git branch -d <branch_name>
Use this command to delete the specified branch. It prevents deletion if the branch contains unmerged changes.
Force Delete a Branch:
git branch -D <branch_name>
If you're sure you want to delete the branch, including unmerged changes, use this command.
Usage Example:
Let's say we want to create a new branch named "feature-1" to work on a new feature:
Create the branch:
git branch feature-1
Switch to the new branch:
git checkout feature-1
Make changes, add commits, and push the branch to the remote repository if needed.
Once the feature is complete, merge it into the main branch:
git checkout main git merge feature-1
☞Git Revert and Reset
Git revert and reset are essential commands for managing changes in a Git repository.
Commands:
Git Revert:
git revert <commit_hash>
Revert undoes a specific commit by creating a new commit that reverses the changes introduced by the original commit. It's useful for undoing changes without altering the repository's history.
Git Reset:
git reset [--soft | --mixed | --hard] <commit_hash>
Reset moves the current branch's HEAD to a specified commit. The
--soft
option keeps staged changes,--mixed
resets the staging area, and--hard
discards all changes.
Usage Example:
Let's say you want to revert the last commit and reset the changes made in the working directory:
Revert the last commit:
git revert HEAD
This creates a new commit that undoes the changes introduced by the previous commit.
Reset changes in the working directory:
git reset --hard HEAD^
This moves the HEAD to the parent of the current commit, effectively discarding any changes made in the working directory.
☞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.
COPY
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:
COPY
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:COPY
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:
COPY
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:COPY
git push origin feature
And that's a simple example of how git rebase
works.
☞What is Git Merge?
Git merge is a fundamental command used to integrate changes from one branch into another branch. When you merge branches in Git, it combines the changes made in the source branch with the target branch, creating a new merge commit that incorporates the changes from both branches.
Usage Example:
Suppose you have a feature branch (feature
) where you've been working on a new feature, and you want to merge it into the main branch (main
) once the feature is complete.
Switch to the main branch:
bashCopy codegit checkout main
Start the merge process:
bashCopy codegit merge feature
Resolve any merge conflicts that may arise during the merge process.
Once conflicts are resolved, Git creates a new merge commit that combines the changes from both
feature
andmain
, incorporating the feature into the main branch.
✤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:COPY
git checkout main
Merge Feature Branch: Now, you merge the
feature
branch intomain
:COPY
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:
COPY
git commit
Push the Merged Changes: Finally, push the merged changes to the remote repository:
COPY
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.
***I'm confident that this blog will prove to be valuable, helping you discover new insights and learn something enriching .***🙏
😊Happy Learning : )