|
| 1 | +Lets see the usages of Git Branching with real-world examples including how to create a branch, update it, delete it, switch to another branch, and merge them locally and remotely. |
| 2 | + |
| 3 | +Most of the software projects have two mainstream branches as master and dev. |
| 4 | + |
| 5 | +The master branch is used for ready for production changes and the dev branch is used for testing purposes. |
| 6 | + |
| 7 | +When new changes are tested on the dev branch, eventually it will be merged into the master branch and finally, we will take a deployment on the master branch. |
| 8 | + |
| 9 | +we should work neither on the master branch nor on the dev branch directly. For every tiny or massive change in a software project, we should first create a new branch from the dev branch by naming the new branch related to our new change. |
| 10 | + |
| 11 | + |
| 12 | +Let me assume that we already have a GitHub repository. |
| 13 | + |
| 14 | +And you can see we have only the “master” branch which is created by default. |
| 15 | + |
| 16 | +Setting Remote and Local Branches |
| 17 | + |
| 18 | +Branch on Remote |
| 19 | + |
| 20 | +But as most software projects have common branches as master and dev. Click on branch section and By typing “dev” and clicking the appeared “Create branch: dev” section, we can create the dev branch on the remote repository. |
| 21 | + |
| 22 | +Create a Branch on Local Repository |
| 23 | + |
| 24 | +git pull origin dev |
| 25 | + |
| 26 | +git branch ==> master and dev branch will be there in local repo |
| 27 | + |
| 28 | + |
| 29 | +Switching branches |
| 30 | + |
| 31 | +Now we are ready for development and just before starting, we need to change our current branch from master to dev by using git command on our local repository. |
| 32 | + |
| 33 | +git checkout dev |
| 34 | + |
| 35 | +The dev branch which will be our main development branch. All development operations will be originated from that branch. |
| 36 | + |
| 37 | +Now Let us create a new branch from the dev branch |
| 38 | + |
| 39 | +git branch my-bugfix |
| 40 | + |
| 41 | +Now make changes to your branch and commit the changes |
| 42 | + |
| 43 | +push the branch to github |
| 44 | + |
| 45 | +git push origin my-bugfix |
| 46 | + |
| 47 | +After pushing our commit with our new branch, we can check the remote repository from the branches section. You could see the new branch |
| 48 | + |
| 49 | + |
| 50 | +Once you work on branch is complete, you can delete it |
| 51 | +git branch -D my-new-branch |
| 52 | + |
| 53 | +Delete a branch on Remote repo |
| 54 | + |
| 55 | +git push origin --delete my-bugfix |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
0 commit comments