Overview
Teaching: 10 min
Exercises: 10 minQuestionsObjectives
- How should we organize branches to avoid conflicts?
- Avoid conflicts, avoid double work, avoid losing patches.
master
typically once (at the end of their lifetime).master
more often (release branch).master
and code review and very high discipline:
master
except the occasional git cherry-pick
.master
:
master
to your topic branch often to stay in sync with main development line.feature-a
, my colleague develops feature-b
.feature-a
becomes ready, it cannot be integrated to the main line
because it is then diluted with feature-b
.Common situation:
master
but you cannot since your new feature is not ready yet.master
, so other developers can see it.hotfix-X
branch and merge it to master
.Help! I made a commit to the “wrong” branch and it is a public branch and I was told I should not edit public commits, what now?
Solution: git cherry-pick
the commit to the “right” branch:
You made few commits to the local master
branch.
You then realize that it broke some tests but you have no time now to fix them.
So you wish you had committed them to an experimental branch instead.
What now?
You want to move last three commits to a separate branch.
First make sure that your working directory and staging area are empty.
Then create a new branch (e.g. feature
):
Now reset master
back three commits:
$ git checkout master
$ git branch feature # create feature branch but stay on master
$ git reset --hard c2 # on master
Another job well done. However, this should not be done if the commits have already been shared with others.
git reset
because we do not want to change the history for commits that others depend on.git revert
which creates new commits that revert changes.git revert
does not modify past commits.In the following exercises you will practice cherry-picking commits, testing combinations of
features, and rewinding the master
branch.
Let’s continue with “recipe” directory from the previous episode:
$ git graph
* 13792bc (HEAD -> master) Merge branch 'dislike-cilantro'
|\
| * b542667 reduce the amount of cilantro
* | 5a35ac6 we try with more cilantro
|/
* f0272c2 (origin/master, origin/HEAD) Merge branch 'less-salt'
|\
| * 89d5730 (origin/less-salt) reduce amount of salt
* | 0e6c5b1 Merge branch 'experiment'
|\ \
| * | d4d360e (origin/experiment) maybe little bit less cilantro
| * | b859fe2 let us try with some cilantro
| |/
* | b100795 draft a README.md file
|/
* 78d6661 enjoy your dish!
* 8094aee add half an onion
* 4c02218 adding ingredients and instructions
And create a dev
branch:
$ git branch
dev
* master
We will fix a “bug”
on the dev
branch, and then cherry-pick it to master
.
dev
branch.master
branch, and cherry-pick the bugfix from the dev
branch.git graph
.Use the same repository as in the previous exercise.
master
branch.integration
from one of the new branches.integration
branch. Resolve conflicts if needed.git graph
.master
branch.feature
.feature
, and rewind the master branch
.git graph
.Key Points
Short-lived, well defined feature branches minimize risk of conflicts.
Fix bugs on main development line, not in feature branches.