If you are like me, you will probably find your self working on the "master" branch of your project most of the time (when you are the sole contributor on a project of course...). Often I start working on an issue that has been raised by my users confident that I can fix it quickly in one go. But I soon discover that it is far more complicated than initially thought and regret not creating a new "issue branch" to work off (so my master branch is clean and the commits are atomic to features/issues I'm working on)
I often face this workflow problem when I'm working on my open source project React Stepzilla for example.
Here is what I do now when I come across this GIT workflow problem:
- Locally on my computer I've checked out "master" and I'm working on a new github issue with id 27 (for e.g)
- I modify multiple files trying to fix the issue
- I then discover that I'm not going to be able to complete the fix easily and regret not creating a new issue branch "issue-27"
- Running "git status" shows me all my changes so far to the "master" branch
- I then run "git checkout -b issue-27". This will create a new branch called issue-27 with all your local changes.
- I'm now in a new local branch called "issue-27"
- I continue my work and stop for the moment, I then add my code via "git add -A" and commit it using "git commit -m 'Working on #27'" (putting the #27 here makes the code change referenced in the github issue, which is nice...)
- I then push the new branch to origin and track it "git push -u origin issue-27"
- If you checkout master now and run "git status" you will notice that the changes are not longer "pending" here and your master is clean.
- Once you are done with your work on the "issue-27" branch, then your can merge the changes into "master" via a github "pull request" or locally and then delete the "issue-27" branch.
Hope this helps you guys, also here is a good stackoverflow that tell your more.