YouTip LogoYouTip

Pycharm Git

PyCharm Version Control Integration | Beginner Tutorial

PyCharm Version Control Integration | Beginner Tutorial


Configuring Git/GitHub

  • Open File/PyCharm → Settings → Version Control → Git
  • Confirm that PyCharm has automatically detected Git in the "Git executable path"
  • Click the **Test** button to verify if Git is available
  • Image 1

Configuring User Information

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Integrating GitHub

  1. Add GitHub Account:
    • Open File/PyCharm → Settings → Version Control → GitHub
    • Click **+** to add an account
    • Select login method:
      • Login via GitHub: Log in through browser authorization
      • Login via Token: Use personal access token
      Image 2
  2. SSH Configuration (Optional):
    • Generate SSH key: ssh-keygen -t ed25519 -C "your_email@example.com"
    • Add public key to your GitHub account's SSH keys settings

Basic Operations

We can create a Git repository using the version control button on the toolbar:

Image 3

Afterwards, the version control button on the project will turn into master (main branch):

Image 4

Committing and Pushing

Commit Code

In the **Commit Tool Window**:

  • Select files to commit
  • Enter commit message
  • Select action:
    • Commit: Local commit only
    • Commit and Push: Commit and push immediately to remote
    Image 5

Shortcuts:

  • Commit: Ctrl+K (Win/Linux) / ⌘K (Mac)
  • Push: Ctrl+Shift+K (Win/Linux) / βŒ˜β‡§K (Mac)

View Changes

File status color markers:

  • Blue: Modified
  • Green: Added
  • Gray: Untracked
  • Red: Conflict
Image 6

Pull and Merge

Pull Latest Code

  1. `Git → Pull` or `Ctrl+T` (Win/Linux) / `⌘T` (Mac)
  2. Select merge strategy:
    • Merge: Retain all commit history
    • Rebase: Linear history record

Merge Branch

  1. `Git → Branch → Merge Branch`
  2. Select target branch to merge
  3. Resolve possible conflicts (see Section 6.3)

View History and Differences

View Commit History

  1. `Git → Show History` or `Alt+9`
  2. Features:
    • View file/project history
    • Compare different versions
    • Roll back to specific version

Compare Differences

  • File Differences:
    • Right-click file → `Git → Compare with Current Branch`
    • Or Ctrl+D (Win/Linux) / ⌘D (Mac)
  • Inline Differences:
    • Modified lines will be displayed with colored markings
    • Click marking to view specific modification content

6.3 Resolving Conflicts

1. Scenarios for Conflict Generation

  • Multiple people modifying the same area of the same file
  • Conflict when merging branches
  • Conflict prompt when pulling code

2. Resolution Steps

  1. Open Merge Tool
    • Automatic pop-up merge dialog when conflict occurs
    • Manually open: `Git → Resolve Conflicts`
  2. Three-window Comparison Interface
    • Left: Your version (current branch)
    • Right: Other person's version (merge branch)
    • Center: Merge result editing area
  3. Handle Conflict
    • Click **>>** to accept left changes
    • Click **<<** to accept right changes
    • Manually edit the middle area merge content
  4. Mark as Resolved
    • Complete editing and click **Apply**
    • Right-click file → `Git → Mark Conflict as Resolved`
  5. Submit Merge Result
    • Submit the resolved file

3. Advanced Conflict Resolution

  1. Use External Merge Tool
    • Configure in settings: `File → Settings → Version Control → Git → Merge Tool`
  2. Completely Accept One Version
    • Right-click file → `Git → Accept Theirs` or `Accept Ours`

Practical Tips

Partial Commit

In the commit window, you can:

  • Select specific changes within a file to commit
  • Right-click code block → `Commit Selected Lines`

Stash Changes

Temporarily save unfinished modifications:

  • `Git → Stash`
  • Restore: `Git → Pop Stash`

Interactive Rebase

  1. `Git → Interactive Rebase`
  2. You can:
    • Reorder commits
    • Combine commits
    • Edit commit messages
← Java Vector Addint Index ObjecPycharm Code Editor β†’