Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

github

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!

like image 886
Aubergine18 Avatar asked Jun 24 '16 02:06

Aubergine18


3 Answers

after setting upstream

git checkout -b branch-name
git fetch upstream
git reset --hard upstream/master
git push --set-upstream origin branch-name
like image 142
Jay Zhan Avatar answered Oct 25 '22 17:10

Jay Zhan


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
like image 38
ElpieKay Avatar answered Oct 25 '22 18:10

ElpieKay


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.

like image 43
Tim Biegeleisen Avatar answered Oct 25 '22 18:10

Tim Biegeleisen