我有两个分支,即master和开发在一个GitHub库。我正在做我所有的开发在开发分支显示。

git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development

现在我想将开发分支上的所有更改合并到主分支中。我目前的方法是:

git checkout master 
git merge development
git push -u origin master 

请让我知道我所遵循的程序是否正确。


当前回答

我通常喜欢先将master合并到开发中,这样如果有任何冲突,我可以在开发分支本身解决,并且master保持干净。

(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)

这两种方法没有太大的区别,但我注意到,有时在合并它们之后,我还不想将分支合并到master中,或者在合并它们之前还有更多的工作要做,所以我倾向于让master保持不变,直到完成最后的工作。

编辑:来自评论

如果你想跟踪谁在何时进行了合并,你可以在合并时使用——no-ff标志。这通常只在将开发合并到主开发(最后一步)时有用,因为您可能需要在工作流中多次将主开发合并到开发(第一步),并且为这些创建提交节点可能不是很有用。

git merge --no-ff development

其他回答

我认为最简单的解决办法是

git checkout master
git remote update
git merge origin/Develop -X theirs
git commit -m commit -m "New release"
git push --recurse-submodules=check --progress "origin" refs/heads/Master

这也保存了所有正在使用的分支的历史

1. //pull the latest changes of current development branch if any        
git pull (current development branch)

2. //switch to master branch
git checkout master 

3. //pull all the changes if any
git pull

4. //Now merge development into master    
git merge development

5. //push the master branch
git push origin master
1. //push the latest changes of current development branch if any        
git push (current development branch)

2. //switch to master branch
git checkout master 

3. //pull all the changes if any from (current development branch)
git pull origin (current development branch)

4. //Now merge development into master    
git merge development

5. //push the master branch
git push origin master

Error
To https://github.com/rajputankit22/todos-posts.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rajputankit22/todos-posts.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Then Use 
5. //push the master branch forcefully
git push -f origin master

对于那些不懂树枝的同学,从头到尾给你们讲解。

基本的主/主分支开发逻辑是:您只在另一个分支上工作,所以您只使用主/主分支与另一个准备合并的分支合并。

你开始以这样的方式创建一个新分支:

在您的本地目录克隆存储库(或创建一个新的存储库):

$ cd /var/www
$ git clone git@bitbucket.org:user_name/repository_name.git

创建一个新分支。它将包含主分支存储库的最新文件

$ git branch new_branch

将当前git分支更改为new_branch

$ git checkout new_branch

像往常一样进行编码、提交……

$ git add .
$ git commit -m “Initial commit”
$ git push # pushes commits only to “new_branch”

当这个分支上的任务完成时,与“master”分支合并:

$ git merge master
$ git checkout master # goes to master branch
$ git merge development # merges files in localhost. Master shouldn’t have any  commits ahead, otherwise there will be a need for pull and merging code by hands!
$ git push # pushes all “new_branch” commits to both branches - “master” and “new_branch”

我还推荐使用Sourcetree App来查看变更和分支的可视化树。

一旦你“签出”了开发分支,你……

 git add .
 git commit -m "first commit"
 git push origin dev
 git merge master

 git checkout master 
 git merge dev
 git push origin master