git
is a great tool; but, it cannot take the place of good communication among developers.
You have to ask whether the changes in master
must also be included in feature
. Ideally, a feature branch should have the smallest amount of code possible.
If the change(s) absolutely must be included in feature
, then the you have basically two options: git rebase
; and, git cherry-pick
. I suppose you could perform a backward merge from master
into feature
; but, that can lead to bad situations...
cherry-pick
allows you to apply a specific commit or multiple commits to the current HEAD
, keeping comment and author information. After having successfully cherry-pick
ed, git is smart enough to know that the two commits are the same when merging feature
back into master
. If it's just a few commits we're talking about, then cherry-pick
should be sufficient.
rebase
allows you to apply the current branch (line of commits) starting from a different point in history. As you pointed out, this can be troublesome for developer1 and developer2 who already have copies of feature
. They would also need to rebase
their local development onto the new feature
branch.
At any rate, the commit directly to master
by developer3 should have been in its own feature branch, and that feature branch should have been merged. That feature branch could then have been merged into feature
as necessary. Assuming just one (the most recent) commit in master
, you could rectify the situation as follows:
# Ensure clean working directory
$ git stash
# Create new branch at master
$ git branch some-descriptive-name master
# Move master back one commit
$ git checkout master
$ git reset --hard HEAD^
# Merge the new branch into master
$ git merge --no-ff some-descriptive-name
# Forcibly update master
# YOU SHOULD COMMUNICATE WITH OTHER DEVS BEFORE DOING THIS
$ git push -f origin master
# Merge the new branch into feature
$ git checkout feature
$ git merge --no-ff some-descriptive-name
I cannot stress enough how valuable good communication is because these kinds of "oops" things can and do happen all the time.
Good luck!
EDIT:
The part about cherry-pick
ing was written with the assumption that there was only a few commits (or just one) to master
and that they all would be cherry-pick
ed.
x -- y (master)
\
a -- b -- c (feature)