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

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


当前回答

如果您正在使用gerrit,那么以下命令可以完美地工作。

git checkout master
git merge --no-ff development

您可以使用默认的提交消息进行保存。确保已经生成了变更id。您可以使用下面的命令来确定。

git commit --amend

然后用下面的命令进行推送。

git push origin HEAD:refs/for/refs/heads/master

您可能会遇到如下所示的错误消息。

! [remote rejected] HEAD -> refs/for/refs/heads/master (you are not allowed to upload merges)

为了解决这个问题,gerrit项目管理员必须在gerrit中创建另一个引用,命名为'refs/for/refs/heads/master'或'refs/for/refs/heads/*'(这将覆盖所有 未来的分支)。然后授予该引用的“Push Merge Commit”权限,如果需要提交GCR,则授予“Submit”权限。

现在,再次尝试上面的push命令,它应该可以工作。

学分:

https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit

https://stackoverflow.com/a/21199818/3877642

其他回答

1)在分支开发上,使用以下命令检查git状态:

git status

不应该有未提交的代码。如果是,将你的代码推到开发分支:

git add *

git commit -m "My initial commit message"

git push origin Development

2)在Development分支中,运行以下两条命令:

git branch -f master HEAD

git push -f origin master

它将把您的开发分支代码推到主分支。

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

假设harisdev是分支名。

git checkout master

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

git merge harisdev 

git push origin master

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

$ git branch -d harisdev

如果您可以使用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/

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

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

 git checkout master 
 git merge dev
 git push 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来查看变更和分支的可视化树。