|
| 1 | +--- |
| 2 | +title: "Branches" |
| 3 | +teaching: 15 |
| 4 | +exercises: 15 |
| 5 | +--- |
| 6 | + |
| 7 | +:::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- Why use branches? |
| 10 | +- How do we use branches? |
| 11 | + |
| 12 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 13 | + |
| 14 | +::::::::::::::::::::::::::::::::::::: objectives |
| 15 | + |
| 16 | +- Be able to create and merge a branch |
| 17 | + |
| 18 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## About Branches |
| 25 | + |
| 26 | +Branches are independent development lines. Working independently, you can likely get away with using git without creating any branches, as in the first diagram in the figure below. However, when you begin to work with others, branches allow team members to work on the code without impacting the work of other developers or users. |
| 27 | + |
| 28 | +{alt="" width="75%"} |
| 29 | +*These graph diagrams show repositories with different numbers of branches. The vertices, or circles, in these graphs show different commits, and each horizontal path is a branch. The first shows a repository with 1 main branch, the second a repository with 1 main and 1 feature branch, and the third repository 1 main and 2 feature branches.* |
| 30 | + |
| 31 | +Branches can have a few different purposes. Branches created to develop a specific feature are referred to as feature branches, and are often short-lived. Branches can also be used for longer-term purposes, such as having separate development and production branches. How you organize and use branches in your project is referred to as a branching strategy, which we will cover more when we talk about git workflows. |
| 32 | + |
| 33 | +Changes made in one branch can be brought into another branch with a merge. For example, when a feature branch is completed it may be merged into the main branch. And, if while you are working on a feature branch the main branch changes, you can merge the changes from the main branch into your feature branch while you are still working. |
| 34 | + |
| 35 | +## How to Work with Branches |
| 36 | + |
| 37 | +### Creating Branches |
| 38 | + |
| 39 | +You can create a branch on your local clone of the repository. If didn't just clone the repository it is always good to make sure you have any recent changes to the main branch by checking out the main branch and running "git pull": |
| 40 | + |
| 41 | +```shell |
| 42 | +git checkout main |
| 43 | +git pull |
| 44 | +``` |
| 45 | + |
| 46 | +Then, to create a branch we can run: |
| 47 | + |
| 48 | +```shell |
| 49 | +git checkout -b my_new_branch |
| 50 | +``` |
| 51 | + |
| 52 | +This will create the branch `my_new_branch` and move you to the new branch. If you run `git status` at this point it will display `On branch my_new_branch`. Making and committing any changes will only update `my_new_branch`. |
| 53 | + |
| 54 | +{alt="" width="75%"} |
| 55 | +*Creating a new branch. When you run `git checkout -b my_branch` your new branch gets created and checked out, meaning you are now on your new branch (represented by the smiley face). Any commits you make will be on this branch until you checkout another one.* |
| 56 | + |
| 57 | +{alt="" width="75%"} |
| 58 | +*Every time you run the `git commit` command the commit will be added to your current branch.* |
| 59 | + |
| 60 | +The first time you push your branch to the remote repository, you will need to publish the branch by running run: |
| 61 | + |
| 62 | +```shell |
| 63 | +git push --set-upstream origin my_new_branch |
| 64 | +``` |
| 65 | + |
| 66 | +After this any commits can be pushed with a simple `git push`. |
| 67 | + |
| 68 | +### Changing Branches |
| 69 | + |
| 70 | +If you need to switch to another existing branch you can use the `git checkout` command. For example, to switch back to the main branch you can run: |
| 71 | + |
| 72 | +```shell |
| 73 | +git checkout main |
| 74 | +``` |
| 75 | + |
| 76 | +Remember, if you aren't sure which branch you are on you can run `git status`. It is good practice before you start making changes to any of your files to check that you are on the right branch with `git status`, particularly if you haven't touched the code recently. |
| 77 | + |
| 78 | +{alt="" width="85%"} |
| 79 | +*Switching branches using `git checkout`.* |
| 80 | + |
| 81 | +### Merging Branches |
| 82 | + |
| 83 | +Bring the changes from one branch into another branch by merging them. When you merge branches, changes from the specified branch are merged into the branch that you currently have checked out. For example, |
| 84 | + |
| 85 | +```shell |
| 86 | +git checkout my_new_branch |
| 87 | +git merge main |
| 88 | +``` |
| 89 | + |
| 90 | +will merge any changes introduced to `main` into the `my_new_branch`. Merging in this direction is especially useful when you've been working on `my_new_branch` for a while and `main` has changed in the meantime. |
| 91 | + |
| 92 | +{alt="" width="60%"} |
| 93 | +*Merging new commits from the main branch into the feature branch with `git merge main`.* |
| 94 | + |
| 95 | +When development is complete on `my_new_branch`, it would be merged into `main`: |
| 96 | + |
| 97 | +```shell |
| 98 | +git checkout main |
| 99 | +git merge my_new_branch |
| 100 | +``` |
| 101 | + |
| 102 | +{alt="" width="50%"} |
| 103 | +*Merging new commits the feature branch into the main branch with `git merge my_branch`.* |
| 104 | + |
| 105 | +Git will do its best to complete the merge automatically. For example, if none of the changes have happened in the same lines of code, things will usually merge cleanly. If the merge can't complete automatically, this is called a merge conflict. Any conflicts in the files must be resolved before the merge can be completed. |
| 106 | + |
| 107 | +## Merge vs Rebase |
| 108 | + |
| 109 | +There is another way to introduce changes from one branch to another: rebasing. A rebase rewrites history. For example, if there are new commits on the main branch while you are working on a feature branch, you could merge those commits into your feature branch, as we describe above. This creates a new commit with two parent commits: one in your feature branch, one in the main branch. Alternatively, you can rebase your feature branch onto the new end of the main branch with `git rebase main`. Rebasing "replays" your feature branch commits onto the new commits of the main branch, as if you started your branch there. |
| 110 | + |
| 111 | +{alt="" width="50%"} |
| 112 | +*Rebasing the feature branch onto new commits in the main branch with `git rebase main`.* |
| 113 | + |
| 114 | +Rebasing creates a cleaner history, without the extra merge commit. However, rebases never be done on public branches that others might be using or even looking at. It will result in your repository and everyone else's having different history, which can be confusing and difficult to fix. If no one else is looking at your branch, especially if you haven't published it yet, rebasing is safe. |
| 115 | + |
| 116 | +## Activity |
| 117 | + |
| 118 | +Everyone: |
| 119 | + |
| 120 | +- Create a new branch in your local clone |
| 121 | +- Publish the branch to the remote repository |
| 122 | +- Make a small change, commit, and push |
| 123 | +- Look at the remote repository, look at your change and your group's changes |
| 124 | + |
| 125 | +Now, **all but one** should run the following: |
| 126 | + |
| 127 | +```shell |
| 128 | +git pull |
| 129 | +git checkout main |
| 130 | +git merge branch_name |
| 131 | +git push |
| 132 | +``` |
| 133 | + |
| 134 | +Did anyone run into merge conflicts? How might this have been prevented? |
| 135 | + |
| 136 | +There should be one team member who still has an unmerged branch at the end of this exercise. |
| 137 | + |
| 138 | +## Additional Resources |
| 139 | + |
| 140 | +- [Code Refinery: Branches](https://coderefinery.github.io/git-intro/branches/) |
| 141 | +- [Code Refinery: Conflict Resolution](https://coderefinery.github.io/git-intro/conflicts/) |
| 142 | +- [Atlassian Git Tutorials: Merging vs. Rebasing](https://www.atlassian.com/git/tutorials/merging-vs-rebasing) |
| 143 | +- [Slide version of this lesson](https://github.com/INTERSECT-training/collaborative-git/blob/main/presentations/CollaborativeGit.pdf) |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +::::::::::::::::::::::::::::::::::::: keypoints |
| 149 | + |
| 150 | +- Branches allow team members to work on code without impacting the work of other developers or users |
| 151 | +- Create a new branch with the git checkout -b command, for example: git checkout -b my_branch |
| 152 | +- Change to another branch with the git checkout command, for example: git checkout my_other_branch |
| 153 | +- Merge my_branch into the current branch with with the git merge command, for example: git merge my_branch |
| 154 | +- Rebasing rewrites history and can sometimes be used instead of merge, where appropriate |
| 155 | +- Never rebase on a public branch |
| 156 | + |
| 157 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
0 commit comments