git 将开发分支与 master 合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14168677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:12:19  来源:igfitidea点击:

Merge development branch with master

gitgit-merge

提问by Pawan

I have two branches namely masterand developmentin a GitHub Repository. I am doing all my development in development branch as shown.

我有两个分支,即masterdevelopment在GitHub的库。如图所示,我正在开发分支中进行所有开发。

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

Now I want to merge all the changes on the developmentbranch into the master. My current approach is:

现在我想将development分支上的所有更改合并到master. 我目前的做法是:

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

Please let me know if the procedure I am following is correct.

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

回答by Sailesh

I generally like to merge masterinto the developmentfirst so that if there are any conflicts, I can resolve in the developmentbranch itself and my masterremains clean.

我一般喜欢合并master到第development一个,这样如果有任何冲突,我可以在development分支本身解决,我的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)

There isn't much of a difference in the two approaches, but I have noticed sometimes that I don't want to merge the branch into masteryet, after merging them, or that there is still more work to be done before these can be merged, so I tend to leave masteruntouched until final stuff.

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

EDIT: From comments

编辑:来自评论

If you want to keep track of who did the merge and when, you can use --no-ffflag while merging to do so. This is generally useful only when merging developmentinto the master(last step), because you might need to merge masterinto development(first step) multiple times in your workflow, and creating a commit node for these might not be very useful.

如果您想跟踪谁进行了合并以及何时进行,您可以在合并时使用--no-ffflag 来执行此操作。这通常仅在合并developmentmaster(最后一步)时有用,因为您可能需要在工作流程中多次合并masterdevelopment(第一步),并且为这些创建提交节点可能不是很有用。

git merge --no-ff development

回答by David Culp

Personally, my approach is similar to yours, with a few more branches and some squashing of commits when they go back to master.

就我个人而言,我的方法与你的类似,多一些分支,当他们回到 master 时会压缩一些提交。

One of my co-workers doesn't like having to switch branches so much and stays on the development branch with something similar to the following all executed from the development branch.

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

git fetch origin master

git merge master

git push origin development:master

The first line makes sure he has any upstream commits that have been made to master since the last time updated his local repository.

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

The second pulls those changes (if any) from master into development

第二个将这些更改(如果有)从 master 拉入开发

The third pushes the development branch (now fully merged with master) up to origin/master.

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

I may have his basic workflow a little wrong, but that is the main gist of it.

我可能对他的基本工作流程有点错误,但这是它的主要要点。

回答by Gediminas

Explanation from the bottom for ones who came here without any knowledge of branches.

对于没有任何分支知识的人来说,从底部开始解释。

Basic master branch development logic is: You work only on another branches and use master only to merge another branches.

基本的 master 分支开发逻辑是:你只在另一个分支上工作,使用 master 只合并另一个分支。

You begin to create a new branch in this way:

您开始以这种方式创建一个新分支:

1) Clone repository in your local dir (or create a new repository):

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

$ cd /var/www
$ git clone [email protected]:user_name/repository_name.git

2) Create a new branch. It will contain the latest files of your master branch repository

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

$ git branch new_branch

3) Change your current git branch to the new_branch

3) 将你当前的 git 分支更改为 new_branch

$ git checkout new_branch

4) Do coding, commits, as usual…

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

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

5) When job is finished on this branch, merge with “master” branch:

5)当这个分支上的工作完成后,与“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”)

Update: I highly recommend to use GitKraken for this to see visual tree of changes and see better all logic and commits.

更新:我强烈建议使用 GitKraken 来查看更改的可视化树并更好地查看所有逻辑和提交。

回答by Rizwan Siddiquee

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

回答by Harsha

It would be great if you can use the Git Flow workflow. It can merge develop branch into master easily.

如果您可以使用Git Flow 工作流,那就太好了。它可以轻松地将开发分支合并到主分支。

What you want to do is just follow the git-flow instruction mentioned here:

您要做的只是按照此处提到的 git-flow 指令进行操作:

STEPS:

脚步:

  • setup the git-flow project
  • create branches and merge everything to develop
  • run the command git flow release start <version_number>
  • then provide a meaningful messagefor the release
  • run the command git flow release finish <version_number>
  • it will merge everything into masterand change the branch to master.
  • run the command git pushto publish the changes to the remote master.
  • 设置 git-flow 项目
  • 创建分支并合并所有内容以进行开发
  • 运行命令 git flow release start <version_number>
  • 然后为发布提供有意义的信息
  • 运行命令 git flow release finish <version_number>
  • 它会将所有内容合并到master并将分支更改为master
  • 运行命令git push将更改发布到远程主服务器

For more information, visit the page - http://danielkummer.github.io/git-flow-cheatsheet/

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

回答by Sergiu Dumitriu

Yes, this is correct, but it looks like a very basic workflow, where you're just buffering changes before they're ready for integration. You should look into more advanced workflowsthat git supports. You might like the topic branchapproach, which lets you work on multiple features in parallel, or the graduation approachwhich extends your current workflow a bit.

是的,这是正确的,但它看起来像是一个非常基本的工作流程,您只是在准备好集成之前缓冲更改。您应该研究git 支持的更高级的工作流程。您可能喜欢主题分支方法,它可以让您并行处理多个功能,或者毕业方法可以稍微扩展您当前的工作流程。

回答by Haris Np

If you are on Mac or Ubuntu, go to the working folder of the branch. In the terminal

如果您使用的是 Mac 或 Ubuntu,请转到分支的工作文件夹。在终端

suppose harisdev is the branchname.

假设 harisdev 是分支名称。

git checkout master

if there are untracked or uncommitted files you will get an error and you have to commit or delete all the untracked or uncommitted files.

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

git merge harisdev 

git push origin master

One last command to delete the branch.

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

$ git branch -d harisdev

回答by kataras

Step 1

第1步

Create and switch to a new "dev" branch, where your local git files are in-synced with the remote but "dev" branch does not exist yet.

创建并切换到一个新的“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.

Step 2

第2步

Make your changes to the "dev" branch (your current if you follow step 1), commit and push them to the remote "dev" branch.

对“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.

Step 3

第 3 步

Merge your "dev" branch into the "master".

将您的“dev”分支合并到“master”。

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.

回答by Nesha Zoric

This is how I usually do it. First, sure that you are ready to merge your changes into master.

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

  1. Check if development is up to date with the latest changes from your remote server with a git fetch
  2. Once the fetch is completed git checkout master.
  3. Ensure the master branch has the latest updates by executing git pull
  4. Once the preparations have been completed, you can start the merge with git merge development
  5. Push the changes with git push -u origin masterand you are done.
  1. 使用远程服务器的最新更改检查开发是否是最新的 git fetch
  2. 一旦提取完成git checkout master
  3. 通过执行确保主分支具有最新的更新 git pull
  4. 准备工作完成后,就可以开始合并了 git merge development
  5. 推送更改git push -u origin master并完成。

You can find more into about git mergingin the article.

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

回答by Himanshu Mahajan

1) On branch Development, check git status using following command:

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

git status

There should be no uncommitted code. If it is, push your code on Development branch:

不应该有未提交的代码。如果是,请将您的代码推送到 Development 分支:

git add *

git commit -m "My initial commit message"

git push origin Development

2) On Development branch, run following two commands:

2)在开发分支上,运行以下两个命令:

git branch -f master HEAD

git push -f origin master

It will push your Development branch code to master branch.

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