🧬Day 10 - Advance Git & GitHub for DevOps Engineers: Part - I
🔃Git Branching
Git branching is a brilliant function of git in which if a user want to add some new functionality or fix Bugs without altering the main or primary branch, we can create different versions of our projects. To create a branch we use git branch
command.
Brief of how branching works in git:
Creating Branch: To create a new branch user have to use
git branch
command followed by the branch name.git branch <Branch_name>
Switching Branch: For switching to other feature branch we will use
git checkout
command followed by the branch that has to be used as new commit branch.git checkout <Branch_name>
Commit Changes: Currently our pointer is on new branch now we can make changes to our code and commit the code using
git add
and after adding to the staging area we can now commit the code.git add . (# here "." indicates all the file present in that branch add them to staging area) git commit -m "First version of my code" (This meassage is not compusory)
Merging Branches: When we finished working on our functionality or bugs, we can merge the changes back into the
main
branch using thegit merge
command. For example:git checkout main git merge <new_brach_name>
Delete Branches: After merging a branch and if no longer needed we can delete using
-d
flag.git branch -d <branch_name>
⏮️Git Revert and Reset
Two commonly used tools that git users will encounter are those of git reset and git revert . The benefit of both of these commands is that you can use them to remove or edit changes you’ve made in the code in previous commits. But both have the different purpose to serve.
Git Revert:
This is used to revert back the specific changes which have already made in previous commits.
- To revert back the specific commit we have the unique Hash ID of every commit through which we can track them to quote that we use
git log
command, copy the Hash Id of the commit which want to be revert.
Now, use command git revert
followed by the Hash_Id.
git revert <Unique_Hash_ID>
This will create a new commit that undo the changes introduced by the specific
commit.
Git Reset:
This is used to reset the commit or undo the commit to previous changes where we have to commit again to track that changes. It is safety for you that in case you do not want to reset your commit and it was done accidentally you can again add the files to staging area and commit those using git add .
and git commit -m "commit Message"
commands. video for reference: https://youtu.be/OGk5rvYw8c0?si=0pPAEovq20qM_pLt
There are different reset modes and let see the flags for them:
- Hard Reset (--hard): Resets both the index and working directory to the specified commit. This is the most aggressive mode, to shift pointer to the previous commit completely now head will be on previous commit completely.
git reset e3df726 --hard
- Soft Reset (--soft): Moves the HEAD to the specified commit but keeps changes in the index and working directory, or we can say normal reset.
git reset f2gh776 --soft
- Mixed Reset (--mixed): Default mode. Resets the index to the specified commit but keeps changes in the working directory.
git reset i7r89yu --mixed
🔀Git Rebase and Merge
What Is Git Rebase?
Command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs. this is used to keep neat and easy to find commits.
git rebase <base_branch> #Git Rebase
What is Git Merge?
Imagine you're working on a group project with your friends. Each of you has a copy of the project files, and you're all making changes independently. When you're done with your changes and want to combine everyone's work, you merge.
- Git merge is like combining different versions of a project into one. It takes the changes from one branch (like your work) and applies them to another branch (like the main project). This creates a new commit, which represents the combined work of everyone involved.
- Example: Let's say you have a branch called "feature" where you've been adding new features to the project. When you're finished, you merge the "feature" branch into the "master" branch to incorporate your changes into the main project.
git merge <source_branch> # Git Merge
Suppose you have a Git repository with two branches: master
and feature
:
# Checkout the feature branch
git checkout feature
# Make some changes to the files
# Add, commit your changes
git add .
git commit -m "Implemented new feature"
# Switch back to the master branch
git checkout master
# Merge the changes from the feature branch into master
git merge feature
In this example, the git merge feature
command combines the changes made in the feature
branch into the master
branch. After merging, you'll have a new commit on the master
branch that includes the changes from the feature
branch.
🚧Conclusion:
understanding when to use git revert
, git reset
, git merge
, and git rebase
can greatly enhance your Git workflow. git revert
is handy for undoing specific changes while preserving history, git reset
allows you to manipulate the current state of the repository, git merge
is useful for integrating changes from different branches, and git rebase
helps in maintaining a clean and linear commit history by integrating changes from one branch onto another. Mastering these commands empowers developers to efficiently manage project history and collaboration in Git.
Happy Learning😊
Subscribe to my newsletter
Read articles from Vivek Ashok Moudekar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vivek Ashok Moudekar
Vivek Ashok Moudekar
👋 Hello there! I'm Vivek, a DevOps enthusiast with a keen interest in streamlining software delivery. I hold a Master's degree in Computer Applications and have a solid foundation in key technologies such as Linux, Git, Docker, Kubernetes, and AWS. 💻 My passion lies in automation, ensuring efficient and seamless processes throughout the software development lifecycle. I thrive on creating robust CI/CD pipelines that empower teams to deliver high-quality software with confidence. 🚀 Beyond the code, I enjoy the ever-evolving world of DevOps and the challenges it brings. Join me on this journey as I explore new ways to enhance software delivery and foster a culture of continuous improvement. Let's connect, collaborate, and make the world of DevOps even more exciting together