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

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


当前回答

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

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

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

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

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

$ 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来查看变更和分支的可视化树。

步骤1

创建并切换到一个新的“dev”分支,其中您的本地git文件与远程同步,但“dev”分支还不存在。

git branch dev # create
git checkout dev # switch
# No need to git add or git commit, the current
# branch's files will be cloned to the new branch by-default.
git push --set-upstream origin dev # push the "dev" branch to the remote.

步骤2

对“dev”分支(如果您遵循第1步,则是当前的)进行更改,提交并将其推送到远程的“dev”分支。

git add .
git commit -S -m "my first commit to the dev branch" # remove the -S if you're not "secure", secure = when you already setup crypto private and public keys (i.e "verified" green sign in github)
git push -u origin dev # push the changes to the remote, -u origin dev is optional but good to use.

步骤3

将“开发”分支合并到“主”分支中。

git checkout dev # switch to "dev" branch if you're not already.
git merge master # optionally, this command is being used to resolve any conflicts if you pushed any changes to your "master" but "dev" doesn't have that commit.
git checkout master # switch to "master", which is the branch you want to be merged.
git merge --no-ff dev # merge the "dev" branch into the "master" one.

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

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

基于@Sailesh和@DavidCulp:

(on branch development)
$ git fetch origin master
$ git merge FETCH_HEAD
(resolve any merge conflicts if there are any)
$ git checkout master
$ git merge --no-ff development (there won't be any conflicts now)

第一个命令将确保所有上游提交都已提交到远程主服务器,而Sailesh的响应不会发生。

第二个将执行合并并创建冲突,然后您可以解决这些冲突。

这样做之后,您终于可以签出master以切换到master。

然后将开发分支合并到本地主服务器上。no-ff标志将在master中创建一个提交节点,以便整个合并是可跟踪的。

在此之后,您可以提交并推动您的合并。

这个过程将确保人们可以看到从开发到master的合并提交,然后如果他们去查看开发分支,他们可以看到您在开发过程中对该分支所做的各个提交。

如果您想添加在开发分支中所做事情的摘要,您可以选择在推送合并提交之前修改它。

编辑:我最初的答案建议git合并主没有做任何事情,最好是做git合并FETCH_HEAD后获取的起源/主