GitHub is wonderful place to learn new things and help open source community by participating in various projects. GitHub newcomers often have similar problems with git usage pattern, and I want to describe simple strategy to avoid them.
Work with a fork
The first thing is to fork the repository in your GitHub account. Go thru https://help.github.com/articles/fork-a-repo instructions on how to “Configure Remotes” and “Pull in upstream changes” to keep your fork in sync with changes that happen in the official repository.
Never commit on master branch
Each time you want to commit bug fix or a feature you need to create a branch for it. There is no problem for maintainer of a project to accept your pull request from you master branch but it is problematic for your fork when you want to pull the changes back and your master branch has diverged from upstream.
With this strategy you can think of your master branch as a “landing place” for upstream changes. Even your own commits will end up on master after they have been merged in upstream. Also when you do a pull request on a branch, you can continue to work on another branch and make another pull request.
Before create a new branch pull the changes from upstream, your master need to be uptodate.
# Pulls in changes not present in your local repository, without modifying
$ git merge upstream/master
# Merges any changes fetched into your working files
Create new branch on your local machine:
Switch to your new branch :
Check current branch you are working on:
Push the branch/changes from your commit on github :
Delete a branch on your local filesytem :
Delete the branch on github (you can also make it on github site):
Useful hacks
If you accidentally commit on master but have not pushed your changes:
# resets your master branch to the same state as upstream/master.
If you need to merge several commits to one commit:
Say your bug fix branch is called bugfix then on the master branch issue the following command:
$ git commit
# take all the commits from the bugfix branch, squash them into 1 commit and then merge it with your master branch.
If you need to amend last commit:
Make the fixes. (If you just want to change the log, skip this step.)
Commit the changes in “amend” mode:
# your editor will come up asking for a log message (by default, the old log message).