简体   繁体   English

Git:如何确保新分支基于上游 master

[英]Git: How to ensure new branch is based on upstream master

I've been making a lot of rookie mistakes with github and as a result I'm seeking a foolproof (me-proof!) method for ensuring that:我在 github 上犯了很多新手错误,因此我正在寻找一种万无一失(我证明!)的方法来确保:

a) All new branches I create on a fork are based on origin master, not some other branch, and... a) 我在 fork 上创建的所有新分支都基于 origin master,而不是其他某个分支,并且...

b) That my origin master is always up-to-date with upstream master, and... b) 我的 origin master 总是与上游 master 保持同步,并且......

c) That when/before I commit (push?), my changes are rebased to upstream master. c) 当/在我提交(推送?)之前,我的更改将重新基于上游主服务器。

Here's what I have so far...这是我到目前为止...

To create new branch:创建新分支:

git fetch --all --tag
git pull upstream master
git push origin master
git checkout -b my_branch_name -t origin/master

To store my changes to that branch:要将我的更改存储到该分支:

git add -A
git commit -m "Summary of what changed"
git fetch --all --tag
git pull --rebase upstream master
git push origin my_branch_name

To load an existing branch at a later date (eg. to do some additional changes resulting from PR feedback):稍后加载现有分支(例如,根据 PR 反馈进行一些其他更改):

git fetch --all --tag
git pull upstream master
git push origin master
git checkout my_branch_name -t origin/master

And then to save my updates to that branch:然后将我的更新保存到该分支:

git add -A
git commit --amend --no-edit
git fetch --all --tag
git pull --rebase upstream master
git push origin my_branch_name -f

I must confess I don't fully comprehend what some of these commands do - there seems to be lots of ways to do lots of similar sounding things and despite lots of googling/reading I'm still a rookie.我必须承认我并不完全理解其中一些命令的作用 - 似乎有很多方法可以做很多类似的事情,尽管有很多谷歌搜索/阅读,我仍然是一个菜鸟。

Any guidance very much appreciated!非常感谢任何指导!

If upstream master refers to the latest master branch in the server/remote, you could simply run the following cmd to create a new branch.如果upstream master指的是服务器/远程中最新的 master 分支,您可以简单地运行以下 cmd 来创建一个新分支。

git fetch origin master
git checkout -b <new_branch> FETCH_HEAD

If you don't mind working on detached HEAD, you could also run如果你不介意在分离的 HEAD 上工作,你也可以运行

git fetch origin master
git checkout FETCH_HEAD

after setting upstream设置上游后

git checkout -b branch-name
git fetch upstream
git reset --hard upstream/master
git push --set-upstream origin branch-name

To create a feature branch based on the "current" master branch, you should take the following steps:要基于“当前” master分支创建功能分支,您应该执行以下步骤:

git checkout master                # switches to your local master branch
git pull origin master             # updates remote tracking branch, merges into local master
git checkout -b my_feature_branch  # create a new branch from your updated local master

Note that there is always the possibility that new changes keep coming into the remote master branch on GitHub.请注意,新的更改总是有可能不断进入 GitHub 上的远程master分支。 Actually, you should generally assume that this will be happening, and you need to deal with it via merging or rebasing.实际上,您通常应该假设这种情况会发生,并且您需要通过合并或重新定位来处理它。

If you have changes in the downstream master you want to keep, I would do the following to keep the downstream master the same but create a new branch on the upstream master.如果您想保留下游 master 中的更改,我将执行以下操作以保持下游 master 相同,但在上游 master 上创建一个新分支。

git checkout -b upstream-master
git fetch upstream
git reset --hard upstream/master
git push --set-upstream origin upstream-master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM