How should I keep my git project branch up to date with master?
Let's say I have a master
branch and a project
branch, both on our remote origin
. Work is mainly being done in the project
branch, but occasionally a bug fix needs to go into master
so it can be deployed immediately. Eventually when the project is done, I want to be able to squash all the commits in project into a single commit and then merge that into master
.
Usually with feature branches (that are not pushed to origin
), we keep them up to date by merely rebasing with master
and going on our merry way, but because project
is its own branch on origin
, I'm not sure how to keep the history the way I want it (commits from master
, then new project
开发者_高级运维 commits, with no merge commits ideally) due to the safeguards about rewriting history on remote branches. Currently we do it by deleting the remote project
and recreating it with the correct history, but this is definitely suboptimal.
I'm okay with rewriting history on remote project
because this is only a team of 2 and we understand the implications and are willing to be ridiculously careful. But how do I accomplish it?
you can merge braches in git
git checkout project
git merge origin/master
That will merge any changes that master has into project, as long as they came from the same ancestor.
git checkout project
git rebase master
git push origin +project:project
Now just to figure out how to correctly pull the changes on another computer!
I would be inclined to cherrypick the respective change in 'project' into 'master', so that 'master' contains the code you want.
I would then either merge 'master' back into 'project', or just keep track of the patch-id's that have been applied. (Whichever approach, do it consistently!). Personally, I think merging 'master' back into 'project' is simpler for the long term.
Regarding feature branches I prefer rebasing them on master
. This is especially useful for long running feature branches that may require a lot of development before getting back into master
while master
will evolve in parallel.
Rebasing will take your branch's change and apply them on master
, the result will be your new branch. master
itself will stay untouched.
You can do this like follows when your project
branch is checked out:
(project) $ git rebase master
or explicitly:
(some-branch) $ git rebase master project
Note: Rebasing will rewrite your branch's history! So be careful.
Please see the documentation for more information.
精彩评论