Git Branching🌿
Use a branch to isolate development work without affecting other branches in the repository. Each repository has one default branch and can have multiple other branches. You can merge a branch into another branch using a pull request.
Branches allow you to develop features, fix bugs, or safely experiment with new ideas in a contained area of your repository.
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.
Git Rebase and Merge🔗
What Is Git Rebase?
Git rebase is a 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.
What Is Git Merge?
Git merge is a command that allows developers to merge Git branches while the logs of commits on branches remain intact.
The merge wording can be confusing because we have two methods of merging branches and one of those ways is called “merge,” even though both procedures do essentially the same thing.
Refer to this article for a better understanding of Git Rebase and Merge
💼Task 1 :
Add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside.
This should be in a branch coming from
master
, [hint trygit checkout -b dev
], swithch todev
branch ( Make sure your commit message will reflect as "Added new feature").
1️⃣ Step 1 : Create a New Branch and Add version01.txt, committing Changes in dev Branch
git checkout -b dev
vim version01.txt
git add version01.txt
git commit -m "added a new feature"
2️⃣Step 2: Pushing Changes to Remote Repository
git push origin dev
✔️Verify the chages after git push
in remote repo
3️⃣Step 3: Adding Additional Commits in dev Branch
vim version01.txt
git add version01.txt
git commit -m "Added feature2 in development branch"
vim vee version01.txt
git add version01.txt
git commit -m "Added feature3 in development branch"
vim version01.txt
git add version01.txt
git commit -m "Added feature4 in development branch"
4️⃣Step 4: Restoring version01.txt to a Previous Version
We can use either git revert
or git reset
to achieve this. Let's use git reset
in this example.
git reset e95ede1
💼Task 2 :
Demonstrate the concept of Branching, Merging, and Rebase
1️⃣ Step 1 : Creating Additional Branches
git checkout -b staging
vim staging1.txt
git add staging1.txt
git commit -m "staging1 added"
git checkout -b uat
vim uat1.txt
git add uat1.txt
git commit -m "uat1 added"
2️⃣ Step 2 : Make some changes on dev branch
vim version01.txt
git add version01.txt
git commit -m "added another new feature"
3️⃣Step 3: Merging dev Branch into master
git branch
git checkout master
git merge dev
4️⃣Step 4: Using Git Rebase
git branch
git checkout staging
git rebase dev
📍 Conclusion:
Understanding these Git commands empowers developers to efficiently manage their codebase. Branching enables parallel development, revert and reset correct mistakes, and rebase and merge to facilitate integration. By knowing when and how to use these tools, you can ensure a smooth and collaborative development process.