How to create sub branch and generate pull request from sub branch to master branch in git

Subbranch provides a way through which we can generate our code without affecting the master branch or code base. After completing the changes we can generate pull request from sub-branch to master branch. Here in this blog, we will discuss below points:
1. Create a sub-branch from the master branch
2. Add few changes on sub branch
3. Add, commit and push the code in sub branch
4. Send the pull request from sub-branch to master branch
5. Accept the pull request and merge the code into the master branch

1. Create a branch or sub-branch

We have multiple ways to create a git branch. In this tutorial, we will see how we can create a branch using Git Bash. First, we navigate to our project which resides in our local system and right click and selects the Git Bash option here.
Git Bash Option

It will navigate to you on Git Bash command line window. Now use checkout command to create a new branch.
checkout command

2. Add few changes on sub branch

Now open the eclipse, here you can see your child branch name here. You need to add few changes or code in your child branch. Let us add any Java class in this branch.
create java class

3. Add, commit and push the code in sub-branch

Now we need to add these code in our GitHub sub-branch.
Note: If we check this branch in git hub repository, it will not appear there unless and until we don’t push the code. Here we don’t have child branch.
child branch unavailability

Now we need to add, commit and push the code in our sub or child branch. First, we use command “git status”, through which we can see the current status of the branch.
git status

Now we will add this Java file in index or stash.
git add
If we check again “git status” command, we see that all changes are green now. It means these changes are on the index.
git status again
After adding we need to commit the code.
commit the code
In this command -m is basically used for giving the message so that we can understand commit message later on GitHub repository.
After commiting the code we need to push the code on branch.
push code on branch

4. Send the pull request from sub-branch to master branch

After pushing code on the branch, now we are able to see our sub-branch on git hub.
see sub-branch
Now select the child branch from the drop down and click on Pull requests option(3rd option) here.
In pull, request window clicks on New pull request button.
pull request button
In compare drop down, we need to select sub-branch here. Once we select the sub-branch, we can see our changes or code in below on the page. We first review it and click on “Create pull request” button.
compare branches
Once we click on “Create pull request” button it provides us the below window to give the detail description about our changes.
Create pull request button-1
Then we need to click on “Create pull request” button, It will create the pull request in the below manner.
Create pull request button-2

5. Accept the pull request and merge code into master branch

After generating the pull request we need to first verify the sub-branch code and accept or merge the code into the master branch.
verify sub-branch code
We need to click on “Merge pull request” and click on “Confirm merge” button.
click confirm button
Once we click on “Confirm merge” button, it will merge this latest commit into the master branch from the sub or child branch.
merge latest commit

For better understanding you can see these step on our youtube video: