PyCharm Version Control and git complete tutorial for beginners

In this post:

  • Version control and PyCharm
  • setup git for windows
    • portable
    • installation
  • install git for Linux Ubuntu / Mint
  • PyCharm git operation
    • git commit
    • git push
    • git clone
    • git check out
    • git pull
  • PyCharm Version Control
    • Local Changes
    • Log
    • Console
    • Update info
  • Setup proxy and Git
  • error: PyCharm cannot run program git
  • Reference

Version control and PyCharm

In PyCharm the git configurations are located in:

  • File
  • Settings - CTRL+ALT+S
  • Version Control
  • Git
  • Path to Git executable

Setup git for Windows

Using portable git

If you want to use portable git for Windows and PyCharm then you can download: Git Portable

It is a portable version of Git for Windows.

You can installed it by:

  1. Download the GitPortable directory from the link above
  2. Download PortableGit-*.7z.exe from the Git for Windows repository - for example - PortableGit-2.17.0-64-bit.7z.exe
  3. extract PortableGit-2.17.0-64-bit.7z.exe to the GitPortable\App\Git directory.
  4. Setup the path in PyCharm

Add:

C:\your_path\PortableGit\cmd\git.exe

to PyCharm - Path to Git excetuable

Install git for windows

  1. Download the Git for Windows installer from: Git for Windows
  2. Started the installer
  3. Git Setup wizard screen
  4. Follow the instructions(if you are new you can use the default options).
  5. Open a Command Prompt / Git Bash
  6. Configure git username and mail by
git config --global user.name "My name"
git config --global user.email "my@yourdomain.com"

replace "My name" and "my@yourdomain.com" with your values which will be associated as git account values.

install git for linux

Adding git to Ubuntu and Linux Mint is really easy just by a single command:

sudo apt install git

Default installation on Ubuntu 18.04 and Linux Mint 18 is:

/usr/bin/git

PyCharm git operation

All version control operations like:

  • commit
  • pull
  • push
  • remote

are located in main menu - VCS

For example checking for remotes - linked projects with git can be found:

  • VCS
  • Git
  • Remotes

You can change, delete or add you remotes from the open dialog.

If you work with Version Control you will want to show tool bar Version control by:

  • ALT+9
  • or from menu:
    • View
    • Tool Windows
    • Version Control

PyCharm git commit

In order to commit your changes you need to have remove set up.

  • go on view Version Control - ALT+9
  • Local Changes
  • Select the Changelist - i.e. Default
  • Right click
  • commit - CTRL+K
  • Select what to be commit
    • you can commit everything or select only one / several files
  • Enter Commit Message
  • Select Commit or Commit and Push - CTRL+ALT+K
  • In case of errors or TODOs you can be asked to review the issues

PyCharm git push

You can push using the previous section or to push later by using:

  • Menu
  • VCS
  • Git
  • Push - CTRL+SHIFT+K

Pycharm git clone

If you want to clone a repository from github.com you can do it by these steps:

  • File
  • New
  • Project from Version Control
  • Git
  • URL - enter the remote URL address
  • Test if the URL is OK
  • Select Directory
  • Log in to GitHub ( if needed )
    • only if you used it for first time
  • Select Clone

By default you will clone the full repository and will set up the master branch. If you want to change the branch you can clone the address of the other branch.

Pycharm git checkout

If you want to change the current branch or preview older versions (previous commits) you can do it from

  • go on view Version Control - ALT+9
  • Log
  • Select Revision
    • you will see all revision with user and dates on left
    • on the right you will see all changes
  • Right click
  • Check out revision

Pycharm git pull

If you need to pull changes from other users using git can be done by:

  • Menu
  • VCS
  • Git
  • Fetch

This will check the remote repository for any changes. In case you need changes you will see them in the Version Control window - ALT+9. You can view the new modifications in tab Log. If you want to pull the changes then you can do it from:

  • Menu
  • VCS
  • Git
  • Pull

After successful pull request you will see new tab Update Info. There are all changes divided in three different categories:

  • Pull(12 items)
    • Updated from server(12 items)
      • Updated(3)
      • Created(7)
      • Deleted(2)

PyCharm Version Control

Version Control window can be shown by ALT+9 or:

  • Menu
  • View
  • Tools Windows
  • Version Control

Local Changes

The first tab is named Local Changes inside you can find all your changes separated in changelists. You can do several operations from this tab:

  • commit change - CTRL+K - Saving your changes in a repository. Git add adds a change in the working directory to the staging area.
  • revert - CTRL+ALT+Z - This will revert (remove, give up) your local changes and will return the older files versions.
  • delete - completely delete the file from this project
  • Move to another changelist - you may want to maintain several different changelist. This is useful in case of project specific files like IDE, DB settings etc
  • ignore - in case that the file is not added to VCS and the remote repository you can ignore files or folders. Several technical folders need to be ignored in order to avoid problems. You can ignore also test directories or big files.

Log

In this tab you can see all revisions and code changes. You can checkout to another version / branch. Compare with previous versions by selecting a file and pressing CTRL+D. New window will be open with two version of your code.

Console

If you want to know what commands are executed or simply check your daily activities related to git. You can visit tab Console. Inside you will find the actual git commands with all parameters. For example:

11:17:23.477: [python] git -c core.quotepath=false -c log.showSignature=false pull --progress --no-stat -v --progress origin master
From https://github.com/AllenDowney/ThinkPython.git
 * branch            master     -> FETCH_HEAD
 = [up to date]      master     -> origin/master

Update info

The last tab Update info will be shown after successful pull. Inside you can find information about all pulled changes:

  • Updated(3)
  • Created(7)
  • Deleted(2)

Setup proxy and Git

Setup by command

The easiest way too add proxy to your git configuration is by:

git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
  • proxyuser - proxy user ( you can check the proxy configuration Windows 10 - Settings/Network & Internet/Proxy or ubuntu - System/Preferences/Network Proxy)
  • proxypwd - proxy password
  • proxy.server.com - proxy server
  • 8080 - proxy port

Disable git proxy by

git config --global --unset http.proxy

Enable:

git config --global --get http.proxy

Setup by file

If the command line is not your favorite way of working, you can just edit .gitconfig

  • For Windows - check %userprofile% variable - the root of your profile - open it by: notepad %userprofile%.gitconfig in command prompt.
  • For Linux /home/user - you can access it by:
    • bash sudo nano ~/.gitconfig

and add this line:

[http]
proxy = http://proxyuser:proxypwd@proxy.server.com:8080

Cannot run program "git.exe": CreateProcess error=2, The system cannot find the file specified

If you get error:

Cannot run program "git.exe"

It means that PyCharm Cannot find the git program on your machine or the path which is setup is wrong. In this case you can:

  • verify that your machine has working git - run git command in your terminal
git --version

with expected result: git version 2.7.4

  • check the path in your PyCharm:
  • Settings - CTRL+ALT+S
  • Version Control
  • Git
  • Path to Git executable

In case of Ubuntu you will see:

Auto-detected: /usr/bin/git

Reference

Related Article