git: merge two branches: what are the correct steps?

For one of my project, I usually need to create new branch from development branch and when I completes my work (in new-branch) I need to merge new-branch into development branch. Here is the steps I do:

 git clone –b development <git repository url>
 git branch new-branch
 git checkout new-branch
 git push --all

I do my code in new-branch and commit/push code in new-branch.

Now I need to merge new-branch into development (keep in note that development branch get further commit of other developer by the time I complete my task in new-branch). My query is what is the right approach to merge new-branch into development-branch and push it to remote repository?

Thanks


I prefer to put the feature branch as a squashed commit on top of develop(ment) and throw away it afterwards.

git checkout development
git merge --squash new-branch
git branch -D new-branch
git push origin :new-branch

This way you will keep a single commit containing the whole feature.


If other people are pushing into 'development' then, as you've probably seen, your push might fail owing to a 'non-fast-forward' warning. Therefore the proper approach is to first pull changes from development, build and test, and then push back to development. Here would be a workflow for you:

$ git clone -b development <development repository>  <devdir>; cd <devdir>
$ git checkout -b new-branch
$ <edit, build test>
$ git add ...
$ git commit ...
# repeat above 3
#
# now you want to get your changes back on development
$ git pull origin development
$ <build, test - to ensure new-branch works w/ latest development>
$ git push origin new-branch:development

git checkout master
git merge dev
git commit -m 'merged branches'
链接地址: http://www.djcxy.com/p/49308.html

上一篇: 如何在Git中压缩提交?

下一篇: git:合并两个分支:什么是正确的步骤?