not logged in | [Login]

As a starting point, I have the following:

  • a fork of the MP repository, in my case it's tve/micropython
  • a master branch that tracks micropython/micropython's master
  • a 'tve' branch where I do all my hacking and that has all the features I want added to MP
  • lots of other feature branches, mostly to create PRs
  • a git remote called "origin", which refers to my repo on github (this is created automatically when doing the initial git clone from github to my local machine)
  • a git remote called "upstream", which refers to the micropython/micropython repo on github, I created that using git remote add upstream

Now sometimes I just hack on the "tve" branch, sometimes I create a feature branch from the outset, but in both cases I often end up with changes I want to put into a PR and with changes I don't (they either belong to something else or they're debug statements I don't want to delete yet but that shouldn't be in the PR). Here's what I typically do:

  • create a clean feature branch from the micropython master: first make sure the local git index is up-to-date with git fetch upstream and then create my branch with git checkout -b my-feature upstream/master
  • now pull in changes from the hack branch into this feature branch: git checkout -p hack-branch and go through the interactive flow (a tall terminal window helps!)
  • once you have done that, you can further edit your branch and then commit everything using git commmit -a -m "message", if you want to review what you're adding (recommended unless you didn't do anything but the checkout -p) use git add -pi and go through the interactive flow.
  • once done, push your feature branch to github: git push -u origin my-feature (if you just type git push git will print what you should have typed)
  • now go to github and create the PR (if you got to your repo on github there will be a yellow bar with a button to do that)

Usually things don't go as cleanly as above... Some tips:

  • in the git checkout -p you can split hunks using 's' to separate lines you want included from ones you don't, you can even use an editor ('e') to hand-edit a hunk, for example, to delete "+" lines that add debug code you don't want
  • you can also switch back to your hack branch and fix-up some stuff: git stash to save what you have done; git checkout hack-branch; edit away; git checkoutmy-featureto come back,git stash applyto get the changes you already pulled over back, and then anothergit checkout -p hack-branch`

Down the road, reviewers of course will ask you to make changes to your PR. I find that cherry-pick is my best friend here. The idea is to make the changes in the hack-branch or the my-feature branch and then cherry-pick into the other to keep them in sync:

  • git checkout hack-branch and hack away
  • git add -pi to add just the changes you want into a commit, then git commit -m "message", copy the SHA which commit prints (something like [hack-branch 66486f9c3] where 66486f9c3 is the beginning of the SHA and all you need)
  • now pull this commit into your my-feature branch: git checkout my-feature; git cherry-pick 66486f9c3
  • and after looking thing over one last time you can push this to github to update your PR: git push
  • this workflow works exactly the same with the two branches flipped: you hack on the my-feature branch, create a commit, and then cherry-pick it into the hack-branch to keep that up-to-date