git switch分支并拉

时间:2020-09-24 06:33:57

标签: git version-control

我的组织不相信合并提交。因此,我们确实在master上进行工作并将其挑选到一个稳定的分支上,在这里我将其命名为stable。 (我们还有一个远程origin,它简化了此处的某些命令。)

所以我当前的流程如下:

$ git branch
* master
  stable 
# do some work and commit it to 'master'
$ git commit -a -m "some message"
# rebase on 'origin/master' because there were commits by colleagues in the meantime
$ git fetch && git rebase origin/master
$ git push origin master
# here's the rub: switch to 'stable', which is present in the local repo and behind 'origin/stable'
$ git checkout stable
# now update (a fetch may be necessary before, depending on how busy the remote repo is), git pull would also do
$ git merge --ff-only
# cherry-pick, push etc.
$ git cherry-pick master ...

我的问题是,切换到现有的本地分支后再进行更新有多余的操作:还原过时的文件。基本上,当可能有直接路线时,我会去另一个地方:

# suppose I have pushed my changes to origin/master already
$ git branch -d stable
# some git grumbling, but origin/stable is ahead, so no commits are lost and the operation has proceeded
$ git checkout stable

当然,我可以将其设为别名。但是我想知道:是否有更好的方法可以切换到分支并将其更新为远程状态?还有可能我忽略了我的提案可能存在的问题。 >

2 个答案:

答案 0 :(得分:1)

您现在正在做什么,但是如果您想强制stableorigin/stable相匹配,并且 同时进入stable时间,分别将git checkoutgit switch选项与-B-C一起使用,并同时使用两个名称。那是:

git fetch                              # if needed
git checkout -B stable origin/stable

这有点危险,因为它不能验证origin/stable严格地 stable之前,即此分支重置过程将导致快进。为了确保在不使用它的情况下可以快速前进,可以使用比较模糊的序列:

git push . origin/stable:stable

或等价物:

git fetch . origin/stable:stable

您可以通过以下方式将后者与git fetch组合到遥控器:

git fetch origin stable:stable

如果对stable的调整不是 快进,则会出现由于非快速转发而导致的拒绝。

(当当前分支命名为stable时,这些都不起作用;在这种情况下,请使用git merge --ff-only origin/stable前进或出现错误。)

(我建议编写一个小脚本来完成您想做的事情,并使其处理所有各种极端情况,例如检查您当前的分支名称。)

答案 1 :(得分:0)

首先获取所有更改,包括远程主分支和稳定分支
git fetch --all

然后您可以通过以下命令快速重置本地稳定分支
git branch -f stable origin/stable

切换分支后即可进行樱桃摘
git switch stable

根据我的经验,您使用的流程很奇怪。由于您使用cherry-pick将提交同步到稳定分支(尽管它们具有相同的内容,但会在目标分支上生成新的提交),因此对主服务器和原始服务器/主服务器执行rebase操作的含义是什么? 不建议在长期分支上使用Cherry-pick。