Git and GitHub

This guide has information about various git-related things.

Pull Requests

A GitHub pull request is an action which requests that code from a branch in one GitHub repository be merged with code in a branch from another GitHub repository. Most of the time a pull request is requesting that code from a fork be merged into a main repository for a project, but this does not have to be the case. For repositories that only have one branch, a pull request is generally requesting that code from the main branch of one repository be merged with the code from the main branch of another repository (GitHub recently started using main as the default branch name instead of master).

The status of a pull request is either open or closed. When a pull request is created it is open. A pull request can then be closed by merging the pull request, or by closing it without merging.

Pushing new commits to a branch of a GitHub repository that is currently on the source end of an open pull request causes the pull request to be automatically updated. So if you create a pull request and the maintainer of the project asks you to make additional changes before the pull request is merged, you can make the additional changes locally, commit, and push to your fork and the pull request will be updated automatically with the new changes.

Bringing your Local Code Up to Date

Say the following sequence of events has occurred:

Now your local repository is up to date with Fork, but Main has new commits that you do not have locally or in Fork. You now need to pull the new commits from Main to your local repository.

Adding a Remote

The first thing you need to do is to add a new remote for Main. A remote in git is simply a stored URL for a repository. When you clone, a remote named origin is automatically created which stores the URL that you cloned. You can add as many additional remotes as you want.

To add a remote in PyCharm, follow these steps:

To do this on the command line, run this command from the repository directory:

$ git remote add main <URL for Main>

Now that the remote is added, you will never need to add it again for this local repository.

Fetching and Merging

Now you need to fetch the changes and merge them into your local main branch. This is two separate steps. To do this in Pycharm:

On the command line:

$ git fetch main
$ git merge main/main

If the merge is successful, your “origin/main” branch (which is the branch that pushes to Fork by default) will now be up to date with all the commits from Main’s main branch.

Removing Files Without Deleting

If you accidentally added some files that should not be in the repository (like your .idea folder) you can remove the files from the git repository without actually deleting them.

From the command line:

$ git rm --cached <filename>

If you want to remove a directory you need to add the -r option. You could use this command to remove your .idea folder.

$ git rm -r --cached .idea

Unfortunately this action is not possible from within PyCharm. If you are not comfortable using the command line, the best “quick and dirty” option may be to delete your fork on GitHub, re-fork, re-clone your fork into a new project directory on your computer, copy the code you changed from the old clone directory, commit only those files, and then make a new pull request.