jbholden / cdcpool_google

0 stars 1 forks source link

What is the best way to use git for development? #8

Open jbholden opened 10 years ago

jbholden commented 10 years ago

Original question from Byron:

I'm ready to do some involved development on things like the setup sheet for week picks and the template for players to use to make picks. I suspect it will be something that I want to work on independent of what you are doing and I don't want to disturb your development. Is the best way to do that to create a branch of the master tree? Is this how you are doing your development? What is the procedure for pulling your changes back into the master tree?

jbholden commented 10 years ago

From Brent:

Currently I have been making changes to the master and pushing those changes to github. Instead we should probably make branches for development and then merge those changes with the master.

The startup engineering class recommended 3 branches:

Do we want to copy this model? Do we need the staging branch? Do the branch names need to change (can we both have a "develop" branch)?

jbholden commented 10 years ago

I tried the following method and it seemed to work okay (in this example I am using the branch name "brent_develop")

create a unique development branch named brent_develop

git checkout -b brent_develop
git branch
git push -u origin brent_develop

do all your development on this branch

git add file1.py
git commit -m "changed file1.py"
git push -u origin brent_develop    # push branch changes to github

merge your changes with the master branch

git checkout master
git merge brent_develop
# resolve any conflicts
git push -u origin master

after this point, I'm not currently sure what the best thing to do is. What I'm not sure of is how new changes to the master branch get pulled in to the development branch.

To delete a branch I did this

git checkout master   # switch to master branch
git branch -D brent_develop    # delete branch locally
git push origin --delete brent_develop   # delete branch on github
blreams commented 10 years ago

I've been doing some reading in the "Pro Git" book by Scott Chacon. It is available online at http://git-scm.com/book.

I agree with your "flow" above and will be doing the same thing. Branching is highly optimized in git and is the intended method for doing both short and long-term development efforts.

While you are doing long-term development on a branch you can always merge in the other direction (from master to brent_develop) to get updates from master to your development branch.

I suggest that we adopt a model where we always keep master in a clean useable state. Nothing should get merged back into master without first testing it in a staging branch. The steps to follow would go something like this:

  1. First do a merge from master into byron_develop (my development branch)
  2. Resolve any conflicts, make sure byron_develop passes all tests.
  3. Create a staging branch (call it master_staging).
  4. Merge changes from byron_develop into master_staging.
  5. Resolve conflicts and complete testing.
  6. Merge byron_develop into master.
blreams commented 10 years ago

I just completed a "worst-case" push to origin/master. I guess I didn't fully understand the difference between my local master and origin/master. I had created a separate development branch (as above) and got it to the point where I liked it. Apparently, Brent had committed some new stuff to origin/master a couple days ago (between the time I created my local master and when I was ready to merge my development changes).

So, after merging my development branch back into my local master, I tried to push this to origin. It failed with this message:

blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git push https://github.com/jbholden/cdcpool_google.git master
Username for 'https://github.com': blreams
Password for 'https://blreams@github.com': 
To https://github.com/jbholden/cdcpool_google.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/jbholden/cdcpool_google.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

To fix this, I first had to fetch from origin/master to my local master. The fetch worked fine as follows:

blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git fetch origin
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 17 (delta 5), reused 17 (delta 5)
Unpacking objects: 100% (17/17), done.
From git://github.com/jbholden/cdcpool_google
   0434b63..b4d4987  master     -> origin/master
   0434b63..b4d4987  brent_develop -> origin/brent_develop

But now when I checked status it showed a divergence:

blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 1 different commit each, respectively.
#
nothing to commit (working directory clean)

To fix this, I had to merge from origin/master to my local master:

blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git merge origin/master
Auto-merging main.py
CONFLICT (content): Merge conflict in main.py
Automatic merge failed; fix conflicts and then commit the result.
blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 1 different commit each, respectively.
#
# Changes to be committed:
#
#   modified:   code/update.py
#   modified:   code/week_results.py
#   new file:   pages/week_results.py
#   new file:   pages/week_results_json.py
#   new file:   stylesheets/error.css
#   new file:   views/bad_week.html
#   new file:   views/base.html
#   new file:   views/week_base.html
#   new file:   views/week_final_results.html
#   new file:   views/week_results.html
#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      main.py
#

Then I had to resolve the conflict in main.py, add main.py and commit. At this point my development changes were now merged with the latest from origin/master.

The last step was to push my local master up to origin/master as follows:

blreams@blr-linux12:~/www/giskard.homelinux.net/web/git/cdcpool/cdcpool_google$ git push https://github.com/jbholden/cdcpool_google.git master
Username for 'https://github.com': blreams
Password for 'https://blreams@github.com': 
To https://github.com/jbholden/cdcpool_google.git
   b4d4987..862d9ea  master -> master

And voila, DONE!

So, in retrospect, the mistake I made was not first fetching the latest from origin/master before merging my development changes. The whole process would have been much simpler.