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
Configuring User Information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Integrating GitHub
- 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
- 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:
Afterwards, the version control button on the project will turn into master (main branch):
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
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
Pull and Merge
Pull Latest Code
- `Git → Pull` or `Ctrl+T` (Win/Linux) / `βT` (Mac)
- Select merge strategy:
- Merge: Retain all commit history
- Rebase: Linear history record
Merge Branch
- `Git → Branch → Merge Branch`
- Select target branch to merge
- Resolve possible conflicts (see Section 6.3)
View History and Differences
View Commit History
- `Git → Show History` or `Alt+9`
- 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
- Open Merge Tool
- Automatic pop-up merge dialog when conflict occurs
- Manually open: `Git → Resolve Conflicts`
- Three-window Comparison Interface
- Left: Your version (current branch)
- Right: Other person's version (merge branch)
- Center: Merge result editing area
- Handle Conflict
- Click **>>** to accept left changes
- Click **<<** to accept right changes
- Manually edit the middle area merge content
- Mark as Resolved
- Complete editing and click **Apply**
- Right-click file → `Git → Mark Conflict as Resolved`
- Submit Merge Result
- Submit the resolved file
3. Advanced Conflict Resolution
- Use External Merge Tool
- Configure in settings: `File → Settings → Version Control → Git → Merge Tool`
- 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
- `Git → Interactive Rebase`
- You can:
- Reorder commits
- Combine commits
- Edit commit messages
YouTip