Colaborating in Git with more than one branch
- Author: Fernando A. Damião <me@fadamiao.com>
- Revisor: André Boutros <andre.boutros@hotmail.com>
- Created At: 2014-04-24
- Last Update: 2014-05-21
Some open source projects in sites like GitHub have rules to contributions, most of them have a rule to create a separate branch from master to contribute.
When I contribute to GitHub's Training Kit, they already have a specific branch to receive the contribution, and I have some difficulties working in my fork with this "remote" branch.
So in this scenario I have:
- The project repository: https://github.com/github/training-kit
- My Fork: https://github.com/fadamiao/training-kit
- The branch I want to contribute: cheat-sheet-pt
Fork and clone the repository (if you did not do this before):
$ git clone git@github.com:fadamiao/training-kit.git
Now you need to create a new remote, named upstream
, with the project repository URL:
$ git remote add upstream git@github.com:github/training-kit.git
With the remote of the project added to your local repository, update all the infos of the repository:
$ git fetch upstream
Now you are able to change to the branch and work on them:
$ git checkout -b cheat-sheet-pt upstream/cheat-sheet-pt
This command creates a new branch with the same history as upstream
.
Keep the fork updated
To keep the fork updated, you need to merge the upstream
infos with your branch:
$ git pull upstream master
Some people like use git rebase
instead of git pull
, but I like git pull
=P
If you want you can update your fork by sending a git push
:
$ git push origin master
References
These links help me understand and solve this: