Table of contents
- Introduction:
- Git Branching:
- Git Revert and Reset:
- Git Rebase and Merge:
- Task 1:
- 1. Create a new branch and add content:
- 2. Add new commit indevbranch after adding below mentioned content inDevops/Git/version01.txt: While writing the file make sure you write these lines.
- Task 2 :
- 1. Demonstrate the concept of branches with 2 or more branches with screenshot.
- 2. Add some changes todevbranch and merge that branch inmaster
- 3. As a practice try git rebase too, see what difference you get:
Introduction:
Git has revolutionized the way developers collaborate and manage code changes in software projects. However, mastering Git requires more than just basic commands for committing and pushing code. Understanding advanced concepts like branching, reverting, resetting, rebasing, and merging is essential for efficient collaboration and code management. In this blog, we'll delve into these concepts, equipping you with practical skills to navigate complex Git workflows with confidence.
Git Branching:
Branching is a cornerstone of Git workflows, allowing developers to work on multiple features or fixes simultaneously without affecting the main codebase. Here's how to harness the power of branching:
Creating a Branch: Use
git branch <branch_name>
to create a new branch.Switching Branches: Use
git checkout <branch_name>
to switch between branches.Merging Branches: Use
git merge <branch_name>
to merge changes from one branch into another.Branching Strategies: Explore popular strategies like Git Flow or GitHub Flow to streamline your development process.
Git Revert and Reset:
Both
git revert
andgit reset
undo changes, but with key difference.Sometimes, you make changes to your code that you later realize were a mistake, or maybe they broke something else. Reverting allows you to undo those changes and return to a previous state of your code. It's like hitting the undo button on your changes.
git revert
: Creates a new commit that effectively reverses the previous commit, leaving the original history intact. It's a safe and recommended option.git reset
: Rewrites history by moving the HEAD pointer to a specific commit, essentially deleting the following commits. Use with caution! This can be problematic if others have already pulled your code with the "deleted" commits.Remember:
git reset
is powerful, but use it thoughtfully as it can cause confusion and collaboration issues if not used carefully.Git Rebase and Merge:
Both
git rebase
andgit merge
methods are used to integrate code from one branch into another, but with different approaches:git rebase
: Re-applies the commits from one branch (usually a feature branch) on top of another branch (usually "master"). It rewrites history, creating a linear history as if the commits were originally made on the target branch.git merge
: Creates a new "merge commit" that reflects the integration of two branches. It maintains the original history of both branches, leading to a more complex history with additional merge commits.
When to use which:
Use
git rebase
when:You want a clean, linear history for your main branch.
You are working on a branch alone or with a small, closely coordinated team.
Use
git merge
when:You want to maintain a clear history of changes from each branch.
You are working in a larger team where others might have already pulled your code, and rewriting history with
git rebase
could cause issues.
Remember: While git rebase
offers a cleaner history, use it with caution and ensure others haven't already pulled your code before rebasing, as it can lead to conflicts and confusion.
Task 1:
1. Create a new branch and add content:
Create a new branch named "dev":
git checkout -b dev
:Create a file named "version01.txt" inside the "Devops/Git/" directory with the content: "This is the first feature of our application."
Stage and commit the changes:
git add version01.txt
,git commit -m "Added new feature"
Push your changes to the remote "dev" branch:
git push origin dev
2. Add new commit in
dev
branch after adding below mentioned content inDevops/Git/version01.txt
: While writing the file make sure you write these lines.1st line>> This is the bug fix in development branch.
Commit this with message “Added feature2 in development branch.”
2nd line>> This is gadbad code
Commit this with message “Added feature3 in development branch.
3rd line>> This feature will gadbad everything from now.
Commit with message “Added feature4 in development branch.
Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]
Task 2 :
1. Demonstrate the concept of branches with 2 or more branches with screenshot.
Let's demonstrate the concept of branches using an existing Git repository called
devops-learner
. We'll create two new branches and make some changes in each branch. Here are the steps:- Clone the repository:
git clone https://github.com/Deepika0313/devops-learner.git
- Create a new branch named
feature-branch1
:
git checkout -b feature-branch1
- Make some changes in the codebase. For example, let's create a new file
feature-branch1.txt
and add some content to it:
echo "This is feature 1 content" > feature-branch1.txt
git add feature-branch1.txt
git commit -m "feat: Add feature-branch1"
- Switch back to the
dev
branch:
git checkout dev
- Create another new branch named
feature-branch2
:
git checkout -b feature-branch2
- Make some changes in the codebase again. For example, let's create another new file
feature-branch2.txt
and add some content to it:
echo "This is feature 2 content" > feature-branch2.txt
git add feature-branch2.txt
git commit -m "Add feature-branch2"
Now you have two branches,
feature-branch1
andfeature-branch2
, each with its own set of changes. You can switch between branches to work on different features independently.To see the list of branches:
git branch
To switch back to the
dev
branch:git checkout dev
To merge changes from a feature branch into the
dev
branch:git merge feature-branch1
or
git merge feature-branch2
This demonstrates the concept of branches in Git, where you can work on different features or changes independently and merge them back into the main development branch (
dev
in this case) when they are ready.2. Add some changes to
dev
branch and merge that branch inmaster
We already have the
devops-learner
repository cloned:First, ensure you're on the
dev
branch:git checkout dev
Make some changes in the codebase. For example, let's create a new file
dev_changes.txt
and add some content to it:echo "Changes made in the dev branch" > dev_changes.txt git add dev_changes.txt git commit -m "feat: Add changes to dev branch"
Now, switch to the
master
branch:git checkout -b master
Merge the changes from the
dev
branch into themaster
branch:git merge dev
If there are no conflicts, Git will automatically perform a fast-forward merge, meaning it will move the
master
branch pointer to the same commit as thedev
branch.
That's it! You've successfully merged the changes from the dev
branch into the master
branch.
Remember to push the changes to the remote repository if you want to synchronize it with others:
git push origin master
By this way we can make changes in one branch (
dev
), and then merge those changes into another branch (master
) in Git.3. As a practice try git rebase too, see what difference you get:
let's demonstrate how to use
git rebase
in this scenario.
Assuming you're on the master
branch:
Switch to the
dev
branch:git checkout dev
Make some changes in the codebase. For example, let's create a new file
dev_changes.txt
and add some content to it:echo "Changes made in the dev branch" > dev_changes.txt git add dev_changes.txt git commit -m "feat: Add changes to dev branch"
Now, switch back to the
master
branch:git checkout master
Rebase the
master
branch onto thedev
branch:git rebase dev
This will take the commits from the
master
branch, replay them on top of thedev
branch, and then move themaster
branch pointer to the same commit as thedev
branch.If there are conflicts, Git will prompt you to resolve them manually, similar to the merge process.
Once the rebase is complete, you can push the changes to the remote repository:
git push origin master
Note:However, it's important to note thatgit rebase
rewrites commit history, which can make it unsuitable for shared branches where others may be working. Use it with caution and ensure clear communication with your team.
Hope you found this article informative and useful. Thanks for reading this article.
Keep Learning...