9

我们正在使用我克隆的中央 git 存储库,并且正在本地分支上工作。

当我想让我的更改在中央存储库中可用时,我必须发出以下命令(从 开始mybranch):

#Stash local changes not yet ready for checkin
git stash

#Make sure we have all changes from the central repository
git checkout master
git pull

#Rebase local changes
git checkout mybranch
git rebase

#Push changes
git checkout master
git merge mybranch
git push

#Back to my branch and continue work
git checkout mybranch
git stash apply

我想知道是否可以使用更少的 git 命令来实现相同的目标。master和之间的几个切换mybranch特别烦人,因为我们的存储库相当大,所以它们需要一些时间。

4

4 回答 4

13

如果您不需要更新本地主分支,则无需触摸它,这似乎会导致您进行很多不必要的分支切换。

这是一个更简单的工作流程。

git fetch

# ensure that everything is committed
# perhaps git commit -a is required...

git rebase origin/master


# If you don't want to push the very latest commits you might
# want to checkout a parent or ancestor of the current commit
# to test that the proposed commit passes tests, etc.
# e.g. git checkout HEAD~n

# push to the remote master
git push origin HEAD:master

# if you checked out a parent, go back to the original branch
git checkout mybranch

如果您对父提交非常有信心,您可以跳过结帐步骤并执行以下操作,但我强烈建议您不要这样做。发布未经测试的提交不是“最佳实践”。

git push origin HEAD^:master
于 2009-06-04T20:55:06.897 回答
6

没有必要对 master 和 mybranch 分支都进行拉取。既然你是一个很好的公民,并且进行快进更新,这很简单:

# Save local mods not ready for commit
git stash
# Do the pull & rebase local work assuming this is a remote tracking branch
git pull --rebase
git checkout master
git merge mybranch
git push

当然,你也可以从你的 mybranch 分支推送

# Save local mods not ready for commit
git stash
# Do the pull & rebase local work assuming this is a remote tracking branch
git pull --rebase
git push origin mybranch:master
于 2009-06-03T18:50:53.180 回答
1

您可以将 pull 和 rebase 合二为一:

git pull --rebase master

但总的来说,是的,根据我的经验,它涉及所有这些命令。

为了保持您的存储库清洁,经常运行“git gc”会删除未使用的对象,这很有帮助。这应该减少分支切换时间。

于 2009-06-03T15:24:47.173 回答
0

我通常会。

git co master
git pull
git rebase master mywrk # fix conflicts if any
git rebase mywrk master
git push

如果您喜欢这种方式,您可以定义别名以节省输入。

于 2009-06-03T15:32:02.727 回答