Working with master, our workflow is to
- fork the original (upstream) github repo to own github repo (origin)
- checkout own github repo locally.
- create a feature branch of master locally
- do some work
- push feature banch back to own forked repo
- create pull request.
- time passes, other people update the upstream master branch.
- syncronise the upstream reapo with local repo, then push up to origin (3 way sync)
- start from point 3 and do the next feature.
Working with feature branches of master branch the above flow is easy to use in git:
- fork upstream /original/ repo to your own copy in github UI.
- git clone https://github.com/YOUR-USERNAME/project.git
- git checkout -b my_branch
- do some work.
- git commit -m...
- git push origin my_branch.
- do a pull request via gitub UI
Time passes, someone else changes the upstream reapo, so get these changes and merge them into your local then back to your forked repo:
- git remote add upstream https://github.com/original/project.git
- git fetch --all
- git checkout master
- git merge upstream/master
- git push origin master.
Now your local repo AND the forked repo in your github are updated from the upstream original repo, and you can do more work.
The problem is now we have to do the same, but with the develop branch, but the above recipe does not work.
Firstly, if we do this:
git fetch --all
Does this actually fetch the develop branch from both origin and upstream?
I will assume it does and continue. Next I assumed you would do this:
git checkout develop
This fails ("develop did not match any files") for no obvious reason (if you can checkout master, why not develop? Why is trying to match files not branches). Next we try this:
git checkout origin/develop
This gives the dread detached head error. We have read much about this e.g. here, but don't understand enough about the low level git workings to comprehend it. We can only assume this is not what we want and move on. googling produces several recommended solutions:
git checkout -b develop origin/develop
git checkout --track origin/develop
git branch develop origin/develop
I dont know if the above are all equivalent, or which we should try.
$ git branch -a
master
feature_branch1
remotes/origin/HEAD -> origin/master
remotes/origin/develop
remotes/origin/master
remotes/origin/feature_branch1
remotes/upstream/develop
remotes/upstream/master
This is rather odd - the cloned local repo is missing the develop branch, but origin (which it was cloned from) has it. It seems clone does not clone, but selectively copy only some branches.
Lets assume one of the three above commands is correct.
Next we need to
- pull any changes from the develop branch from the upstream remote (will git pull --all do this?),
- merge the upstream branch with our local develop branch.
- push the local develop branch back to the origin remote.
- create a new feature branch of the delelop branch.
- push this feature to origin
- do a pull request
- go back to step 1.
Any ideas how we do this?
============= UPDATE ===============
For everyone's pleasure, based on the excellent answer from Narahari, here is the complete solution:
Create the develop branch in local repo (clone doesnt do it)
- git checkout -b develop origin/develop
Update develop branch from upstream (the original repo)
- git fetch --all
- git checkout develop
- git merge upstream/develop
- git push origin develop
do some work on a feature branch:
- git checkout develop
- git checkout -b my_develop_feature_branch
- do some work
- git commit -m ...
push changes to your forked repo:
- git push origin my_develop_feature_branch
create a git pull request in github, specifying the upstream develop branch as target.