如何以更简洁的方式更新本地主分支并合并特性分支

huangapple go评论56阅读模式
英文:

How to update local master and merge a feature branch in a shorter way

问题

以下是翻译好的部分:

我的合并特性分支与上游远程的工作流程是:

git stash / commit # 保存当前特性分支上的工作
git checkout master
git pull
git checkout feature
git merge master

对我来说,似乎多了一步。是否有更简洁的工作流程来实现这一点?也许我可以运行 git fetch 并与 origin/master 合并,但这样我的本地 master 分支会过时?

英文:

My workflow to merge my feature branch with upstream remote is:

git stash / commit # save current work on feature branch
git checkout master
git pull
git checkout feature
git merge master

It seems to me I'm doing one too many steps. Is there a shorter workflow to achieve this? I could perhaps run git fetch and merge with origin/master but then my local master would go out of date?

答案1

得分: 2

你是正确的。
你可以执行

git commit
git fetch
git merge origin/master

git fetch会获取来自origin的最新更改,将其合并到origin/master分支,然后你可以将其合并到你的特性分支中。

英文:

You're correct.
You can do

git commit
git fetch
git merge origin/master

git fetch will get the latest changes from origin onto the origin/master branch allowing you to merge it into your feature branch.

答案2

得分: 2

你不需要更新你的"local master",或者切换到master分支来更新origin/master,因为你实际上想要与之保持同步的是origin/master。你只需要运行git fetch,它会从远程拉取变更。

一旦你拉取了变更,你可以使用rebase命令将你的分支上的提交移到从远程拉取的内容之上,通常我会使用rebase -i来执行这个操作,这样我可以查看我正在移动的提交。

完整的步骤是:

git fetch
git stash
git rebase -i origin/master
git stash pop

请注意,就像每次执行rebase操作时一样,你应该确保你是唯一在这个分支上工作的人。否则,你会创建不同的分支,当其他人尝试合并你的变更时可能会出现问题。

英文:

You don't need to update your "local master", or change to the master branch in order to update origin/master, which is the thing you actually want to keep up-to-date with. All you need is git fetch which pulls in changes from the remote.

Once you've pulled in changes, you can rebase onto origin/master to move the commits from your branch on top of whatever has come in from the remote. I typically use rebase -i for this, so I can review the commits I'm moving.

The full set of steps is:

git fetch
git stash
git rebase -i origin/master
git stash pop

Note that, as is always the case when rebasing, you should make sure you are the only person working on the branch. Otherwise you will create divergent branches and there will be problems when other people try to incorporate your changes.

huangapple
  • 本文由 发表于 2023年5月17日 07:20:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定