This is one of my old posts from a blog that’s not active anymore. The content is still pretty good so I’ll reshare it here on this blog 🙂
Forking – If you’d like to contribute to a Github project, the very first thing to do is fork the project.
Cloning – Then create a folder on your computer, let’s call it “sandbox”. Use the terminal to cd into it. And then clone the forked repo using git clone repo_address. This will get the project on your computer.
Branching – Next create branches where you can make changes to the code. For example, I created a branch called fix/error_msg to fix or edit the existing error message in Jetpack.
You can create a branch with git branch branch_name and then git checkout branch_name to switch to that branch OR you can create and switch in one single command git checkout -b branch_name. You can see all the branches on the computer using git branch and status using git status.
Changes – You can make changes to the files now but if you need to discard any of those changes just type in git checkout . and you’ll be okay.
Commit – Once you’re ready to commit. Add all the changed files using git add . and then make a commit using git commit -m 'commit_description' where commit description can be “wrapped text in esc_url function” or something like that. Just describe what is this commit about.
Push – Once committed, next step is to push these changes to the forked repo. You do that by typing git push origin branch_name. In my example branch, this would mean git push origin fix/error_msg.
Pull Request – Go to your forked repo and you’ll see an option to create a Pull Request. You should explain what this PR will do and submit it.
Review – If the reviewers ask you to make changes, make changes on your branch, add/stage the files, make a commit, push the changes and it’ll automatically add another commit under your existing PR.
Merge – If your code looks good, the reviewers or code maintainers will merge it to master and it’ll be added to the next release. (btw you can party at this point, you did something incredibly awesome!).
Other useful commands
Deleting Branches. The first one deletes remote branch, the other deletes a local branch.
$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>
Adding a new remote. Here’s me adding a remote called upstream that links to Jetpack’s original repo.
$ git remote add upstream git@github.com:Automattic/jetpack.git
If it’s been a while since you created that fork, you’ll want to make sure you’re using your project’s latest version. Use the fetch and merge commands to apply the latest project changes to your local repository, without losing any of your local changes.
$ git fetch upstream
$ git checkout master
$ git merge upstream/master
Bonus tip: If your current branch becomes stale and master branch moves on with new code, you may need to rebase your pull request.