- What this tutorial is about
- What this tutorial is not about
- Before you start
- Creating a Git repository
- What's changed in the PyCharm's UI ?
- Putting a file under version control
- Committing to the local repository
- Which branch we are working in?
- Changing the code in the master branch
- Creating new branches
- Changing the code in new branches
- Merging branches
What this tutorial is about
In this tutorial we'll go step by step through using Git integration with PyCharm: creating a Git repository, committing changes, creating branches, etc.
What this tutorial is not about
Using Git per se is out of scope of this tutorial. Refer to the Git documentation instead.
Before you start
Make sure that:
- You are working with PyCharm version 2.7 or higher.
- You have a project already created. You can use the project from the Getting Started tutorial.
- The Git integration plugin is enabled and the location of the Git executable file is correctly specified on the Git page of the Settings/Preferences dialog box.
Creating a Git repository
Press Alt+` to show the the most popular VCS commands (same action is available on the main menu: VCS→VCS Operations Popup). Then select Create Git repository:
As you already know, Git sets up a local repository by creating a .git folder in the specified parent directory. After that all the files in the directories below this parent can be considered stored in this repository. In our case, let’s create a .git directory right below the project root:
What's changed in the PyCharm's UI ?
We immediately see changes in the PyCharm's UI:
- First, Changes tool window appears.
- Next, the file Solver.py changes its color in the Project tool window and in the editor tab:
It means that the new file is still not added to the version control (later we'll see how it's done).
- Finally, if you open the Settings/Preferences dialog (on Windows, press Ctrl+Alt+S) , and then click Version Control, you will see that the directory MySimplePythonApplication is now associated with Git:
Putting a file under version control
PyCharm provides several ways to put a file under version control. Here we'll follow just one of them. Select the file Solver.py in the Project tool window, and press Ctrl+Alt+A.
See that the color code of the file Solver.py changed again - now it's green, which means that the file is under version control, but the change is not yet committed. You see it in the Changes tool window:
Committing to the local repository
Next, let's commit the new file to the local repository. To do that, select the file Solver.py in the Changes tool window, and press Ctrl+K. Next, type your check-in comment, and click Commit.
To make sure your changes appeared in your repository, open the Log tab of the Changes tool window:
Which branch we are working in?
Actually, this information is available in the Logs tab, but there are two another ways to see the current branch:
- First, there is a special command on the main menu VCS→Git→Branches. The pop-up window of existing branches appears:
- Second (and most handy) is to use the Git widget in the Status bar:
As you see, we are now working in the master branch.
Changing the code in the master branch
Let's change the code. For example, let's rename the discriminant. To do that, place the caret at the symbol discr, press Shift+F6, and type the new name discriminant.
Note the blue marker in the left gutter - it means that something has been changed:
If you click the blue marker, PyCharm will show a window with the previous version of the code, and a toolbar enabling you, for example, to revert changes. Let's now commit the updated code (Ctrl+K).
Creating new branches
Again, click the branch indicator in the Status bar, then choose to create a new branch, and type the new branch name:
Note that new branch is now shown as the current one in the Log tab of Changes tool window, in the Status bar of IDE, and in the VCS→Git→Branches command:
Next, create another branch (Branch2) from the master branch.
Changing the code in new branches
Now all the branches are identical. Let's change the same name in Branch1 and Branch2.
First, switch to Branch1 using the branch indicator in the Status bar:
Then place the caret at the symbol discriminant, press Shift+F6, type something shorter, for example dis, and press Ctrl+K to commit the changes.
Next, same way switch to Branch2, rename the same variable to, say, disc, and also commit changes.
What happens if want to merge the two branches? You can only merge the current branch to some other one. So, with Branch1 as the current branch, choose VCS→Git→Merge Changes on the main menu:
So, let's choose to accept THEIR changes and again commit changes. By the way, look again at the Log tab. it looks more and more complicated, reflecting all our a little bit hectic activities: