我有两个分支,即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

其他回答

如果你使用的是Mac或Ubuntu,进入分支的工作文件夹。在终端

假设harisdev是分支名。

git checkout master

如果有未跟踪或未提交的文件,您将得到一个错误,您必须提交或删除所有未跟踪或未提交的文件。

git merge harisdev 

git push origin master

最后一个删除分支的命令。

$ git branch -d harisdev

就我个人而言,我的方法与您的类似,在提交返回到master时增加一些分支和一些压缩。

我的一个同事不喜欢如此频繁地切换分支,而是停留在开发分支上,并从开发分支执行类似于以下内容的内容。

git fetch origin master    
git merge master    
git push origin development:master

第一行确保他拥有自上次更新本地存储库以来提交给master的所有上游提交。

第二步将这些更改(如果有的话)从master导入到开发中

第三个将开发分支(现在与master完全合并)推到origin/master。

我可能把他的基本工作流程搞错了,但这是它的主要要点。

我通常都是这么做的。首先,确保您已经准备好将更改合并到master中。

通过git获取,检查开发是否与远程服务器的最新更改保持一致 一旦获取完成,git签出主。 通过执行git pull,确保主分支有最新的更新 一旦准备工作完成,就可以开始使用git合并开发进行合并了 用git Push -u origin master进行修改,就完成了。

您可以在本文中找到更多关于git合并的信息。

如果您可以使用Git Flow工作流,那就太好了。它可以很容易地将开发分支合并到主系统中。

你要做的就是遵循这里提到的git-flow指令:

步骤:

设置git-flow项目 创建分支并合并所有内容进行开发 执行命令git flow release start <version_number> 然后为发布提供有意义的信息 执行命令git flow release finish <version_number> 它会将所有内容合并到master中,并将分支更改为master。 运行命令git push将更改发布到远程主机。

更多信息,请访问页面- http://danielkummer.github.io/git-flow-cheatsheet/

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