如何有效地改组和推送本地git分支?

sie*_*ert 9 git merge rebase

我们正在使用我克隆的中央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
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以使用更少的git命令来实现相同的目标.之间的几个开关mastermybranch特别讨厌的,因为我们的仓库是相当巨大的,使他们需要一些时间.

CB *_*ley 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
Run Code Online (Sandbox Code Playgroud)

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

git push origin HEAD^:master
Run Code Online (Sandbox Code Playgroud)


Pat*_*otz 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
Run Code Online (Sandbox Code Playgroud)

当然,你也可以从你的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
Run Code Online (Sandbox Code Playgroud)