2

Working with master, our workflow is to

  1. fork the original (upstream) github repo to own github repo (origin)
  2. checkout own github repo locally.
  3. create a feature branch of master locally
  4. do some work
  5. push feature banch back to own forked repo
  6. create pull request.
  7. time passes, other people update the upstream master branch.
  8. syncronise the upstream reapo with local repo, then push up to origin (3 way sync)
  9. 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:

  1. fork upstream /original/ repo to your own copy in github UI.
  2. git clone https://github.com/YOUR-USERNAME/project.git
  3. git checkout -b my_branch
  4. do some work.
  5. git commit -m...
  6. git push origin my_branch.
  7. 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:

  1. git remote add upstream https://github.com/original/project.git
  2. git fetch --all
  3. git checkout master
  4. git merge upstream/master
  5. 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

  1. pull any changes from the develop branch from the upstream remote (will git pull --all do this?),
  2. merge the upstream branch with our local develop branch.
  3. push the local develop branch back to the origin remote.
  4. create a new feature branch of the delelop branch.
  5. push this feature to origin
  6. do a pull request
  7. 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)

  1. git checkout -b develop origin/develop

Update develop branch from upstream (the original repo)

  1. git fetch --all
  2. git checkout develop
  3. git merge upstream/develop
  4. git push origin develop

do some work on a feature branch:

  1. git checkout develop
  2. git checkout -b my_develop_feature_branch
  3. do some work
  4. git commit -m ...

push changes to your forked repo:

  1. git push origin my_develop_feature_branch

create a git pull request in github, specifying the upstream develop branch as target.

John Little
  • 10,707
  • 19
  • 86
  • 158

1 Answers1

2

You can try it this way, same way as you did for master branch:

git checkout -b develop origin/develop --> Creates a local branch called "develop" from your forked github repo

Create a feature branch off the develop branch, and merge into your remote "develop" branch

As Other people make changes to upstream/develop branch

git fetch --all --> Fetches from all remote points for your repo (both upstream and origin)

git checkout develop --> Go to your local develop branch

git merge upstream/develop --> Merge upstream/develop to your local develop branch

git push origin develop --> push latest changes from local develop to your remote "develop" branch

Also to answer your question on "git fetch --all" command, git fetch origin fetches data only from origin, and git fetch --all fetches data from all remotes (origin is one of them). So it will pull from origin and upstream and any other remotes associated. Hope this helps

nsg
  • 129
  • 2
  • 3
  • Many thanks, I will try this. regarding "git fetch --all", the question was whether it fetches the develop branch, as git clone does not. – John Little Jan 23 '18 at 20:58
  • What is the correct way to create a feature branch on develop? Is it just "git checkout develop" followed by "git checkout -b my_develop_feature_branch", or wiil this create a branch of master? E.g. do we have to do do something like "git checkout -b my_develop_feature_branch origin/develop" or perhaps "git checkout -b my_develop_feature_branch origin/my_develop_feature_branch"? – John Little Jan 23 '18 at 21:06
  • Yes git fetch -all will fetch the develop branch. – nsg Jan 24 '18 at 01:52
  • You could simply do git checkout -b "my_develop_feature_branch" origin/develop. That would create a local feature branch and track it to your remote develop branch. This would be the simplest way i guess If you simply create a branch saying "git checkout -b my_develop_feature_branch" it wont track the local branch to any remote branch. And you would have separately do that before pushing the code, using the git upstream command. So it is better you set this tracking while creating the branch – nsg Jan 24 '18 at 02:04
  • A useful tool to manage develop and feature branches is given in this link below: Its called git-flow and manages the develop and feature branches Check it out if you are interested: https://jeffkreeftmeijer.com/git-flow/ – nsg Jan 24 '18 at 02:05