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 actually called “merge,” even though both procedures do essentially the same thing.
Task 1:
Note: When your creating a file in Github and cloning to your local machine by default it will create main branch and when your creating a file in local machine and pushing them into your remote repository like Github then it will in master branch.In this case now we are in main branch because Think your self and comment me.....😊
- Navigate to the Devops/Git/ directory and create a new branch called
dev
:
Bash
git checkout -b dev && git branch
- Create a text file named
version01.txt
inside the Devops/Git/ directory with the given content:
Bash
echo "This is the first feature of our application" > version01.txt
Bash
cat version01.txt
- Add the file to the staging area and commit the changes with the message
"Added new feature"
:
Bash
git add version01.txt
Bash
git status
Bash
git commit -m "Added new feature"
Bash
git status
- Push the branch to the remote repository for review:
Bash
git push origin dev
Note: In the place of password you should past your Github Personal access token.
- Now come to your Github and navigate to Devops repository and you will see pop up that
dev had recent pushes 1 minute ago
like this which means you have pull request in your Github.
To access this pull request click on green button which is text with Compare & Pull request.
After clicking onit you will see like this ⬇️ and then Create Pull Request
- Then After clicking onit you will see like this to Merge pull request click on it and then ask to Confirm merge click onit.
Now your pull request will completed.
- Add new commits in the
dev
branch with the specified content:
Add new commit indev
branch after adding below mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines
- 1st line>> This is the bug fix in development branch
Bash
echo "This is the bug fix in development branch" >> version01.txt
- Commit this with message “ Added feature2 in development branch”
Bash
git add version01.txt
Bash
git commit -m "Added feature2 in development branch"
- 2nd line>> This is gadbad code
Bash
echo "This is gadbad code" >> version01.txt
- Commit this with message “ Added feature3 in development branch
Bash
git add version01.txt
Bash
git commit -m "Added feature3 in development branch"
- 3rd line>> This feature will gadbad everything from now.
Bash
echo "This feature will gadbad everything from now." >> version01.txt
- Commit with message “Added feature4 in development branch"
Bash
git add version01.txt
Bash
git commit -m "Added feature4 in development branch"
- To restore the file to a previous version, you can use either
git revert
orgit reset
. Let's usegit reset
in this case:
Bash
git reset --hard HEAD~3
Task 2:
Demonstrate the concept of branches with 2 or more branches with a screenshot.
- Create and switch to a new branch (e.g.,
feature_branch
):
Bash
git checkout -b feature_branch
Bash
echo "created a new branch and added some changes in this feature_branch" > feature_branch.txt
- Add changes to the
dev
branch and merge it into themain
branch:
Bash
git checkout dev
Bash
echo "Hello" >> version01.txt
Bash
cat version01.txt
Bash
git add version01.txt
Bash
git commit -m "added some changes"
Bash
git checkout main
Bash
git merge dev
Practicegit rebase
:
- Create and switch to a new branch (e.g.,
feature_branch2
):
Bash
git checkout -b feature_branch2
- Make some changes in this branch, then rebase it onto the
dev
branch:
Bash
git branch
Bash
echo "created a new branch and added some changes in this feature_branch2" > feature_branch2.txt
Bash
git add feature_branch2.txt
Bash
git commit -m "Added feature_branch2"
Bash
git rebase dev
This is the changes when you do rebase ⬆️
Happy Learning
Thanks For Reading! :)
-SriParthu💝