The git branch is essentially an independent development line. You can take advantage of branching when working on new features or bug fixes because it isolates your work from that of other team members.
Check the list of project branches
$ git branch --list
Create a new branch locally
Creating a new branch does not change the repository. It just points to a commit For example, uses the git branch command to create a branch called “bugfix”.
$ git branch bugfix
Then check the list of local branches. Green indicates you are in that branch.
How do switch the branch?
You can switch branches by checking the file using the git checkout command. A working tree that matches the version stored in the branch you want to switch to. I used to be in the master branch, but I would like to switch the branch to a bug fix.
$git checkout bugfix
Let’s confirm it…
You are now in the bugfix branch.
Existing code needs to be modified or rewritten based on your needs Files… etc
Let’s create a new file in the bug fix branch.
$ touch bugfix.txt
Write something in that file….
How to push your local branch to a remote server
Previously, the remote server had only one branch, the master branch.
Transfer the local branch to the remote branch using the following command:
$ git push –u origin <brnch-name>
Let’s check if the local branch is transferred remotely …
It’s successfully pushed to the bugfix branch on the remote server (GitHub).
How to merge a branch into another branch.
I created a bugfix for a new branch and created a new file bugfix.txt that is not this file available on the master branch You need to merge this file into the master. let’s do it.
First, you need to commit your changes and push them to the remote branch.
First, you need to switch to the master branch.
$ git checkout master
Use the user git merge command to merge the two branches.
$ git merge < which branch needs to merge >
Then run git push to apply the changes to the master branch
Next, check with the main branch of the remote server to see if the files have been merged.
Once you’ve finished working on the branch and merged it into your main code base, You are free to delete branches without losing history.
$ git branch –d <branch name want to delete>
Let’s confirm it
$ git branch --list
The git branch –d option is only deleted locally, not the branch deleted in the remote repository workplace. If you want to delete a branch in a remote repository, use the following command:
$ git push origin -d bugfix
How to get a new branch from a remote server to a local server
You can create a branch in two ways. One is local, as I explained earlier, and the other is us.
You can create a branch directly on the remote server.
If someone creates a feature branch on a remote server, I want to bring it to my local system
You need to work on this branch. Please use the following steps …
Use git fetches to get all the information on the remote server.
To switch to the feature branch, use the git checkout command …
$ git checkout feature
View the branches on the local and remote servers using the following command:
$ git branch –a