I use PyCharm for my Python development needs and up until recently I had not been using any sort of version control. I was able to use PyCharm’s provided plugins to connect my project to a remote GitHub repository and felt that others may find a write-up useful for saving time during setup.
Version Control Systems and GitHub
A version control system (VCS) allows a developer to record specific changes to a file or set of files over time so that they can recall specific versions down the line. Among many benefits a VCS allows for:
- Creating separate branches of a project to allow for changes and experimentation without jeopardizing the stability of the project’s main branch.
- Restoring a project to a previously saved stated.
- Keeping track of which user made changes to specific files and when those changes were made.
- Bug tracking
- Feature requests
- Project wikis
- Or simply sharing your code on the internet for other developers to make use of (many open-source projects have made GitHub their home)
Setting up GitHub with PyCharm
To integrate GitHub with PyCharm, follow the instructions below:
Download and install Git using the default configuration.
Point PyCharm to your Git installation (Ctrl+Alt+S | Version Control | Git | Path to Git executable:). For me on Windows 10 it was:
Click the ‘Test’ button in the Settings window to ensure that your path is correct and click OK to exit settings.
Go to VCS | Enable Version Control Integration… and select Git in the drop-down menu. Click OK. A local Git repository will be created for your project.
Use Alt+9 to bring up the Version Control tool window. All of the files in your project will be listed as unversioned. Click the ‘Unversioned Files’ drop-down and use Ctrl+Alt+A to add all of the files to version control.
Go to VCS | Import into Version Control | Share Project on GitHub. In the login window that appears click ‘Create API Token’. Enter your GitHub credentials and click ‘Login’ to generate a token that will link PyCharm to your GitHub account without the need to store your GitHub credentials. Once the token is generated it will autofill the token entry box. Click ‘Login’ to connect to GitHub.
After a successful login a ‘Share Project on GitHub’ window will open. Enter a name and description for the new GitHub repository that will be created for your local project. Click ‘Share’.
The ‘Add Files For Initial Commit’ window that appears will allow you to choose which files will be be sent in your initial commit to your new repository. Here you can for example, exclude the ‘.idea’ folder that PyCharm creates for managing your project. You may edit the ‘Commit Message’ if desired. Click OK to commit your files.
PyCharm should now indicate that the process was successful and provide a link to your GitHub repository. Your PyCharm project is now successfully version controlled. You may now use VCS | Commit and VCS | Git | Push… to record your project changes on GitHub. Other desired repository operations can also be performed from the VCS menu.
With the above I was able to share my Kaggle code to GitHub in its own repository. GitHub’s branching functionality will be crucial for a few major changes that I would like to make to that code over the next few weeks…