Git Commands Cheat Sheet for Python and AI Developers – From Basics to Advanced
Master Version Control with Git for Clean, Collaborative, and Scalable Python & AI Projects
Git is an essential tool for every Python developer. Whether you're working on personal projects, collaborating with a team, or contributing to open-source, Git helps you manage code changes efficiently, track progress, and collaborate with others.
Thisguide provides an in-depth, contextual explanation of Git commands categorized from basics to advanced, along with real-life Python development use cases.
Why Git Matters for Python Developers
Version Control: Track changes in Python scripts, Jupyter notebooks, and requirements files.
Collaboration: Seamlessly collaborate with teams on GitHub or GitLab.
Backup: Keep code secure and restorable from anywhere.
Experimentation: Test new features in separate branches without affecting main code.
1. Initial Setup and Configuration
1.1 Configure Git (First-Time Setup)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Purpose: Sets your identity for all local repositories on your machine.
Use Case: When committing changes in a new Python project, your name and email will be tagged.
2. Repository Initialization
2.1 Initialize Git in a Python Project
git init
Explanation: Creates a .git
folder in your project, starting version control.
Example:
cd my-python-app
git init
Use this when starting any new Python application.
3. Tracking File Changes
3.1 Check Status
git status
Explanation: Shows what files are staged, unstaged, or untracked.
Python Example: You updated app.py
or added main.py
— this command confirms what will be committed.
3.2 Add Files
git add filename.py
git add .
Explanation:
Adds specific or all changes to the staging area.
Required before commit.
3.3 Commit Changes
git commit -m "Add feature to parse JSON in app.py"
Explanation: Records staged changes with a descriptive message.
4. Remote Repository Operations (GitHub, GitLab)
4.1 Add Remote Repository
git remote add origin https://github.com/user/repo.git
Explanation: Links your local repo with GitHub or GitLab.
4.2 Push Code to Remote
git push -u origin main
Explanation: Uploads commits to the main
branch on the remote.
Python Use Case: You’ve created a data processing script and want to back it up online.
4.3 Clone Repository
git clone https://github.com/user/repo.git
Explanation: Downloads a copy of an existing repository.
4.4 Pull Latest Code
git pull origin main
Explanation: Updates your local branch with the latest changes from the remote.
5. Branching and Feature Development
5.1 Create a New Branch
git branch new-feature
Explanation: Creates an isolated line of development.
5.2 Switch Branches
git checkout new-feature
# or
git switch new-feature
Explanation: Switches your working directory to the given branch.
5.3 Create and Switch in One Step
git checkout -b bugfix
5.4 Merge Branches
git checkout main
git merge new-feature
Explanation: Integrates changes from one branch into another.
5.5 Delete Branch
git branch -d new-feature
Explanation: Deletes local branch after merging.
6. Stashing Work
6.1 Save Temporary Work
git stash
Explanation: Temporarily saves uncommitted changes.
Use Case: You're debugging api.py
and need to switch branches without committing.
6.2 Apply Stashed Changes
git stash apply
6.3 List All Stashes
git stash list
7. Viewing and Comparing Changes
7.1 View Differences
git diff
Explanation: Shows differences between working directory and staging.
7.2 View Staged Differences
git diff --staged
7.3 View File from Commit
git show abc123:file.py
Explanation: Displays file content from a specific commit.
8. Undoing and Resetting
8.1 Undo Last Commit (Keep Files)
git reset --soft HEAD~1
8.2 Undo Last Commit (Discard Changes)
git reset --hard HEAD~1
8.3 Revert Specific Commit
git revert abc123
Explanation: Creates a new commit to undo a previous one.
9. Tags and Releases
9.1 Create Tag
git tag v1.0
9.2 Push Tag
git push origin v1.0
Use Case: Tagging a stable Python version before release.
10. Clean Up and Recovery
10.1 Remove Untracked Files
git clean -f
10.2 Recover Lost Commits
git reflog
Explanation: Shows all changes including those not visible in git log
.
Sample Git Workflow in a Python Project
# Start a Python project
mkdir my-python-app
cd my-python-app
python -m venv venv
touch main.py
# Initialize git
git init
echo "venv/" > .gitignore
git add .
git commit -m "Initial commit"
# Push to GitHub
git remote add origin https://github.com/user/my-python-app.git
git push -u origin main
Git Commands Summary
Top Git Version Control Management Tools
Top GUI Git Clients
These tools provide a visual interface for Git operations, great for those who prefer drag-and-drop or button-based workflows over the command line.
Integrated Git Clients in IDEs
Most modern IDEs come with built-in Git support:
Conclusion
Git is more than a version control tool — it's a productivity booster, a safety net, and a collaboration enabler. As a Python developer, mastering Git ensures cleaner workflows, better team alignment, and faster debugging. Use this cheat sheet as your day-to-day reference, and practice these commands while building your Python projects.
Stay consistent. Write meaningful commit messages. And never fear breaking things — Git’s got your back!
For more in-depth technical insights and articles, feel free to explore:
Girish Central
LinkTree: GirishHub – A single hub for all my content, resources, and online presence.
LinkedIn: Girish LinkedIn – Connect with me for professional insights, updates, and networking.
Ebasiq
Substack: ebasiq by Girish – In-depth articles on AI, Python, and technology trends.
Technical Blog: Ebasiq Blog – Dive into technical guides and coding tutorials.
GitHub Code Repository: Girish GitHub Repos – Access practical Python, AI/ML, Full Stack and coding examples.
YouTube Channel: Ebasiq YouTube Channel – Watch tutorials and tech videos to enhance your skills.
Instagram: Ebasiq Instagram – Follow for quick tips, updates, and engaging tech content.
GirishBlogBox
Substack: Girish BlogBlox – Thought-provoking articles and personal reflections.
Personal Blog: Girish - BlogBox – A mix of personal stories, experiences, and insights.
Ganitham Guru
Substack: Ganitham Guru – Explore the beauty of Vedic mathematics, Ancient Mathematics, Modern Mathematics and beyond.
Mathematics Blog: Ganitham Guru – Simplified mathematics concepts and tips for learners.