简体   繁体   中英

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:

a) All new branches I create on a fork are based on origin master, not some other branch, and...

b) That my origin master is always up-to-date with upstream master, and...

c) That when/before I commit (push?), my changes are rebased to upstream master.

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):

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.

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

If you don't mind working on detached HEAD, you could also run

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:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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