Working with Pull Requests in GitHub: Pushing Code to GitHub

To push code to GitHub, you need a repository. Open the repository of your choice. If you don’t have a repository yet, Chapter 7 walks through creating a repository that you can use.

TIP: If you’d like to follow along with our example but you haven’t completed the steps in Chapter 7, fork the repository example and then clone your fork to your local machine. If that last instruction sounds like gobbledygook to you, you may want to review Chapter 6, which covers forking and cloning.

The first thing to do is create a new branch. Creating a new branch before writing new code is standard operating procedure with Git. I have a confession to make. We neglect to mention that in the example in Chapter 7. The example directs you to commit code directly to the master branch. That was a shortcut we suggested to keep things simple.

In this chapter, you’ll do things the right way and work in a new branch. There’s an important reason for this. A pull request doesn’t consist of an arbitrary set of changes or commits. A pull request is always associated with a branch. In other words, a pull request is a request to merge one branch into another.

TIP: While it’s true that a pull request can target any branch (except itself), the most common scenario is to target the main branch of a repository, typically named master.

This relationship between pull requests and branches is why you should create a new branch when starting new work. We name the branch new-work for this example, but feel free to name it whatever you want by replacing new-work with your own branch name in the following command:

$ git checkout -b new-work

Now that we have a branch, we need to create a commit in that branch. For this example, the specific contents of the commit are not important. You can choose any file and make some edits to the file, such as adding some text to the end. Or if you’re following along with the repository we created in Chapter 7, manually edit the file manually or run the following command to append some text to end of the file:

$ echo “\n## Installation” >>

Now that you have a file with some changes, commit those changes. You can use the following command, for example, to commit all your changes. The commit message is not important here. The important thing is to have a commit in a branch to work with that is not in the master branch.

$ git add -A

$ git commit -m “Add text to the README”

Now push this new branch to (replace new-work with your branch name):

$ git push -u origin new-work

The git push command tells Git to push local commits to a remote repository. The -u flag specifies where to push it — in this case, to a branch also named new-work on the remote named origin.

The -u flag is only needed the first time you push a new branch to the server. From that point on, the new-work branch on your local machine is associated with the new-work branch on and any subsequent pushes to that branch do not need the flag.

Source: Guthals Sarah, Haack Phil (2019), GitHub for Dummies, Wiley.

Leave a Reply

Your email address will not be published. Required fields are marked *