This post is part 1 of a multi-part series intended to describe how to use PyCharm’s integrated Git (version control) features. See Part 2 here. PyCharm makes it easy to link a Github account directly to the PyCharm IDE and perform Git-related tasks within the PyCharm IDE rather than performing these tasks in a command prompt.
Today I will describe a few basic features to help you get started, including: creating a repository, adding files and committing changes. Future posts will discuss more detailed features, including branching, pushing/pulling, merging, etc. While PyCharm does have a very detailed website geared toward explaining some of this, this blog post series is intended to help those who just want some basic steps and pictures to get started.
You can find background information on Git on this blog, including an introduction to git, as well as tutorials on local version control and remote repositories. A glossary of Git terminology and commands can be found here.
PyCharm is one of many IDEs one can use to edit Python code. I offer some background here on why I chose PyCharm for my Python programming needs, and how to get PyCharm.
The following tutorial will assume you already have PyCharm and Git installed, and have a Github account.
- Within PyCharm, link your Github account.
File –> Settings –> Version Control –> GitHub
Input your GitHub login (username) and password.
- Create a new PyCharm project as pictured below, by going to:
File –> New Project.
(Note: A “project” can just be a folder/directory of related files that PyCharm will recognize as a “project”. If you already have a project, you can simply open the existing “project” to follow these steps to create a repository).
- Having established a new project, now create a local git repository for the project as pictured below, by going to:
VCS –> Import Into Version Control –> Create Git Repository.
If completed correctly, you should see a directory named “.git” appear in the project directory. Note that you must have already downloaded git for this to work (test that git.exe works by going File –> Settings –> Version Control –> Git –> Test).
- Having created a git repository, now create python files and add them to the repository.
Right click on your project in the Project menu, and select New — Python File, as pictured below.
PyCharm will prompt you to include this file in your repository, as is pictured below. If you select “Yes”, you can now commit and track changes you make to this file.
- Commit the file to repository by right-clicking the python file in the project menu, and selecting Git–>Commit File, as is shown in the two images below:
You can include a message with your commit when you are prompted to commit, as shown below:
Note that file names in your project menu will appear in green text as long as the file has not been committed yet. The file name will no longer be green once you commit it.
- Now that your file has been added, you can make changes to the file and commit them.
As soon as you make edits to this newly committed file, the file name in the menu will change colors (to blue in my version of PyCharm). This signifies that uncommitted changes exist in that file. You can commit the changes following the same process I described before, or by clicking the green “VCS” up arrow icon on the top menu.
You will now be prompted with a Commit Changes window, as appears below.
You can review changes that have been made, in case you have forgotten what has changed since you last committed, by double clicking on the file name (in the figure above, this would be the blue “blog_post_file.py” icon). PyCharm will show you what changes have been made in green. (It will also show deletions and/or rearrangements of code).
7. Having committed the file and a change to the file, you can now go to the “Version Control” menu at the bottom of the PyCharm window to unveil a variety of features, including a “Log”, which stores all of the changes I have made to this local repository. The log for my example case is shown below.
I will continue to discuss more detailed features in future posts in this series.