我在 GitHub 存储库中有两个分支,即 master
和 development
。如图所示,我正在开发分支中进行所有开发。
git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development
现在我想将 development
分支上的所有更改合并到 master
中。我目前的做法是:
git checkout master
git merge development
git push -u origin master
请让我知道我遵循的程序是否正确。
git pull -u
为分支(或所有分支,如果推送多个分支)设置上游跟踪。设置后,跟踪将持续存在。没有理由持续使用它。
我通常喜欢先将 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)
这两种方法没有太大区别,但我注意到有时我不想在合并它们之后将分支合并到 master
中,或者在这些方法之前还有更多工作要做可以合并,所以我倾向于让 master
保持不变,直到最后的东西。
编辑:来自评论
如果要跟踪合并的人员和时间,可以在合并时使用 --no-ff
标志来执行此操作。这通常仅在将 development
合并到 master
(最后一步)时有用,因为您可能需要在工作流程中多次将 master
合并到 development
(第一步),并为这些创建提交节点可能不是很有用。
git merge --no-ff development
就个人而言,我的方法与您的方法相似,当它们返回 master 时,会有更多的分支和一些压缩的提交。
我的一位同事不喜欢这么多地切换分支并留在开发分支上,类似于以下所有从开发分支执行的东西。
git fetch origin master
git merge master
git push origin development:master
第一行确保他有自上次更新他的本地存储库以来已对 master 进行的任何上游提交。
第二个将这些更改(如果有)从 master 拉到开发中
第三个将开发分支(现在与 master 完全合并)推送到 origin/master。
我可能对他的基本工作流程有点错误,但这是它的主要要点。
rebase
来更新 dev
分支而不是 merge
。
对于没有任何分支知识的人来说,从底部进行解释。
基本的主/主分支开发逻辑是:你只在另一个分支上工作,所以你只使用主/主分支来与另一个准备合并的分支合并。
您开始以这种方式创建一个新分支:
在本地目录中克隆存储库(或创建新存储库):
$ 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 应用程序来查看更改和分支的可视化树。
develop
的 release
分支。然后添加了一个发行说明文件并提交。然后完成合并回 master/develop
的发布。但我的主分支只有新添加的发行说明。之前的开发提交期间没有其他文件在其中更新。
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
如果您可以使用 Git Flow 工作流程,那就太好了。它可以轻松地将开发分支合并到主分支。
您要做的就是按照此处提到的 git-flow 指令进行操作:
脚步:
设置 git-flow 项目
创建分支并合并一切以开发
运行命令 git flow release start
然后为发布提供有意义的信息
运行命令 git flow release finish
它将所有内容合并到 master 并将分支更改为 master。
运行命令 git push 将更改发布到远程主机。
有关详细信息,请访问页面 - http://danielkummer.github.io/git-flow-cheatsheet/
是的,这是正确的,但它看起来像是一个非常基本的工作流程,您只是在准备好集成之前缓冲更改。您应该查看 git 支持的 more advanced workflows。您可能会喜欢 topic branch 方法,它可以让您并行处理多个功能,或者 graduation approach 可以稍微扩展您当前的工作流程。
如果您使用的是 Mac 或 Ubuntu,请转到分支的工作文件夹。在终端
假设 harisdev 是分支名称。
git checkout master
如果有未跟踪或未提交的文件,您将收到错误,您必须提交或删除所有未跟踪或未提交的文件。
git merge harisdev
git push origin master
最后一个删除分支的命令。
$ git branch -d harisdev
步骤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 步
将你的“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.
这就是我通常的做法。首先,确保您已准备好将更改合并到 master 中。
使用 git fetch 检查开发是否与远程服务器的最新更改保持同步 获取完成后 git checkout master。通过执行 git pull 确保 master 分支有最新的更新准备工作完成后,您可以使用 git merge development 开始合并 使用 git push -u origin master 推送更改,您就完成了。
您可以在文章中找到有关 git merging 的更多信息。
基于@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)
第一个命令将确保您已向远程 master 提交了所有上游提交,并且不会发生 Sailesh 响应。
第二个将执行合并并创建您可以解决的冲突。
这样做之后,终于可以checkout master切换到master了。
然后将开发分支合并到本地主节点上。 no-ff 标志将在 master 中创建一个提交节点,以便整个合并可跟踪。
之后,您可以提交并推送您的合并。
此过程将确保人们可以看到从开发到 master 的合并提交,然后如果他们查看开发分支,他们可以看到您在开发期间对该分支所做的各个提交。
或者,您可以在推送之前修改合并提交,如果您想添加在开发分支中完成的内容的摘要。
编辑:我的原始答案建议一个没有做任何事情的git merge master
,最好在获取原点/主人之后做git merge FETCH_HEAD
1)在分支开发中,使用以下命令检查 git 状态:
git status
不应该有未提交的代码。如果是,请将您的代码推送到开发分支:
git add *
git commit -m "My initial commit message"
git push origin Development
2)在开发分支上,运行以下两个命令:
git branch -f master HEAD
git push -f origin master
它会将您的开发分支代码推送到主分支。
-f
不推荐。
一旦您“签出”开发分支,您...
git add .
git commit -m "first commit"
git push origin dev
git merge master
git checkout master
git merge dev
git push 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,则授予“提交”权限。
现在,再次尝试上面的推送命令,它应该可以工作。
学分:
https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit
https://stackoverflow.com/a/21199818/3877642
我认为最简单的解决方案是
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. //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
pull
。此外,与现有答案相比,目前尚不清楚该答案增加了什么价值。
不定期副业成功案例分享
--no-ff
来合并到 master 来解决这个问题。--no-ff
的用途。 :)git merge --no-ff development
只是为了纠正 @elect 的用法。A->B->C
成为 master,而A->X->Y
是您的 dev 分支。如果您更改X
中可能与A
中的更改发生冲突的文件的一部分,则不会发生冲突,因为A
是X
的祖先。关于丢失的更改,请查看 stackoverflow.com/questions/7147680/… 以恢复所有更改。