如何从另一个分支(开发)更新我的工作 Git 分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26137852/
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-09 02:33:34  来源:igfitidea点击:

How to update my working Git branch from another branch (develop) ?

gitgithubbranchgit-merge

提问by Eki Eqbal

I made a new branch for my code like a month ago, I created feature1branch from developbranch.

一个月前,我为我的代码创建了一个新分支,我从开发分支创建了feature1分支。

?  git branch 
  develop
* feature1

I've been working on feature1for a month now and a lot of the changes pushed to developbranch, How can I update my current branch feature1with the latest commits at developone?

我一直工作在特征1一个月了,有很多的变化推到开发分支,我如何更新当前分支特征1与最新提交发展呢?

I don't want to checkout master and merge my feature1branch. Neither I want to use git cherry-pickto manually move commits from develop to feature1.

我不想结帐 master 并合并我的feature1分支。我也不想使用git cherry-pick手动将提交从 develop 移动到 feature1。

Any help ?

有什么帮助吗?

回答by musiKk

You just merge develop to feature1:

您只需将 develop 合并到 feature1:

git checkout feature1
git merge develop

There is no need to involve another branch such as master.

没有必要涉及另一个分支,例如 master。

回答by stackdave

First you need to update your developbranch, then checkout your featureand merge/rebase it.

首先,您需要更新您的开发分支,然后检出您的功能并合并/重新调整它。

git checkout develop
git pull
git checkout feature/myfeature

Now you can decidebetween running:

现在你可以决定运行:

git merge develop
git rebase develop

The difference between mergeand rebaseis that mergekeeps all commits history from your branch, and that is important if your partial commits have a lot of content that can be interesting to keep.

之间的区别mergerebasemerge保持所有提交的历史从你的分支,这一点很重要,如果你的部分提交有很多内容可以是有趣保持。

The rebaseoption is obligatory in some teams.

rebase选项在某些团队中是强制性的。

When you are ready you can push to your own branch (for example for a pull request)

准备好后,您可以推送到您自己的分支(例如拉取请求)

git push origin feature/myfeature

回答by EliuX

This use case is very helpful to keep updated your PR branch. Firstly, I would recommend you to fetch first your remote changes, i.e. git fetchand then merge or rebase from develop, but from the remote one, e.g.

这个用例对于保持更新你的 PR 分支非常有帮助。首先,我建议您先获取远程更改,即git fetch,然后从develop,但从远程更改,例如合并或变基

git rebase -i origin/develop

or

或者

git merge origin/develop

This way you will update your PR branch without going back and forth between branches.

这样你就可以更新你的 PR 分支,而无需在分支之间来回切换。

回答by Laudiocínio Carvalho

BRANCHS:

分支机构:

DEV ====> develop

开发 ====> 开发

feature1 ====> working

功能 1 ====> 工作



STEP 1GIT SENDING FROM THE SITE

第 1步从站点发送 git

checks the branch you're syncing

检查您正在同步的分支

git status

add files for the commit

为提交添加文件

git add .

commits with a description

带有描述的提交

git commit -m "COMMENT"

send to the branch that you are synchronized

发送到您同步的分支

git push


STEP 2SYNCHRONIZING THE UPDATED WORK BRANCH WITH DEV (development) - synchronizes the working branch with the development branch (updates the development branch)

STEP 2SYNCHRONIZING THE UPDATED WORK BRANCH WITH DEV (development) - 将工作分支与开发分支同步(更新开发分支)

synchronize with the remote and switch to the DEV branch

与遥控器同步并切换到DEV分支

git checkout DEV

request to merge the branch that you are syncing with the feature1 branch

请求合并您正在同步的分支与 feature1 分支

git merge feature1

Merge the current branch with the feature1 branch

将当前分支与 feature1 分支合并

git push


STEP 3GIT FINDING THE REMOTE - Update the working branch from the updated development branch

第 3步 GIT 查找遥控器 - 从更新的开发分支更新工作分支

connects to the reference branch

连接到参考分支

git checkout DEV

Search changes

搜索更改

git pull

Syncs with your work branch

与您的工作分支同步

git checkout feature1

request to merge the branch that you are synchronized with the DEV branch

请求合并与 DEV 分支同步的分支

git merge DEV

Merge the current branch with the DEV branch

将当前分支与 DEV 分支合并

git push

回答by Omar Al-Howeiti

use checkout without merge to fetch specific files or fetch from specific commit, I tried this way and it works!

使用 checkout without merge 来获取特定文件或从特定提交中获取,我试过这种方式并且它有效!

git checkout develop <commit hash>_or_<specific-files-path>

The source.

回答by htafoya

To avoid having the commits from develop by using a simple merge, i've found that the easier (less techier) way to do it is specially if you already pushed:

为了避免通过使用简单的合并从开发中提交,我发现更简单(不太技术)的方法是特别如果您已经推送

  1. Change to develop and be sure you pulled latest changes
  2. Create another branch from develop , feature1_b
  3. Merge feature1to feature1_b
  4. Delete if you wish original feature1
  1. 更改以开发并确保您提取了最新的更改
  2. 从 develop 创建另一个分支, feature1_b
  3. 合并feature1feature1_b
  4. 想要原创就删 feature1

So when you do your PR of feature1_binto develop, it will only have your new changes and not the whole history of commits.

因此,当您执行feature1_binto develop的 PR 时,它只会有您的新更改,而不是整个提交历史。

If you haven't pushed then @stackdave's answer is a good answer.

如果您还没有推送,那么@stackdave 的答案是一个很好的答案。

回答by Vedha Peri

$ git checkout <another-branch> <path-to-file> [<one-more-file> ...]
$ git status
$ git commit -m "Merged file from another branch"