πŸ—‚οΈ Python Developer Tutorial β€” Version Control with Git

Introduction 🌟

Git is the most widely used version control system in the world. It helps developers track code changes, collaborate with others, roll back mistakes, and manage project history efficiently.

Note

πŸ’‘ Distributed Version Control (everything stored locally)

πŸ’‘ Fast, secure, reliable

πŸ’‘ Essential skill for every developer and team

1. Install Git πŸ› οΈ

  • Windows β†’ Download Git
  • Linux β†’ sudo apt install git
  • Mac β†’ brew install git

2. Configure Git (Important!) βš™οΈ

git_config.sh

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

βœ” Identifies you in every commit

3. Initialize a Repository πŸ“

git_init.sh

git init

βœ” Creates a hidden .git folder
βœ” Starts tracking your project

4. Check Repository Status πŸ”

git_status.sh

git status

5. Adding Files to Staging Area βž•

git_add.sh

git add file.py      # single file
git add .            # all files

6. Commit Changes πŸ“

git_commit.sh

git commit -m "Initial commit"

βœ” Saves snapshot of your project

7. Viewing Commit History πŸ“œ

git_log.sh

git log

8. Connecting to GitHub 🌐

github_remote.sh

git remote add origin https://github.com/username/repo.git
git branch -M main
git push -u origin main

βœ” Pushes your local code β†’ GitHub

9. Cloning Repositories 🧩

git_clone.sh

git clone https://github.com/user/repo.git

10. Branching 🌿

Branches allow you to work on features without affecting main code.

git_branch.sh

git branch           # list branches
git branch feature-login     # create branch
git checkout feature-login   # switch branch

11. Merging Branches πŸ”€

git_merge.sh

git checkout main
git merge feature-login

βœ” Integrates feature branch into main

12. Handling Merge Conflicts ⚠️

If two branches edit the same lines, Git asks you to resolve conflicts manually.

Code Snippet

<<<<<<< HEAD <br/>
  Your changes <br/>
  ======= <br/>
  Their changes <br/>
  >>>>>>> branch

13. Git Ignore β€” Ignoring Files 🚫

gitignore.txt

__pycache__/
*.log
.env
node_modules/

βœ” Prevents unwanted files from being tracked

14. Undoing Changes πŸ”„

Undo file modifications

git_restore.sh

git restore file.py

Unstage a file

git_reset.sh

git restore --staged file.py

Undo last commit

git_reset_commit.sh

git reset --soft HEAD~1   # keep changes
git reset --hard HEAD~1   # delete changes

Note

⚠️ Avoid reset on shared repos!

15. Working with Remote Repos 🌍

Pull new changes

git_pull.sh

git pull origin main

Push your changes

git_push.sh

git push origin main

16. Stashing Work πŸ“¦

git_stash.sh

git stash         # store current changes
git stash pop     # apply changes back

βœ” Useful when switching branches temporarily

17. Tagging Versions 🏷️

git_tag.sh

git tag v1.0
git push origin v1.0

18. GitHub Pull Requests πŸ”§

PRs allow code review before merging changes.

  • 1. Push feature branch
  • 2. Open GitHub β†’ Create Pull Request
  • 3. Reviewer checks & approves
  • 4. Merge into main

Git Cheat Sheet πŸ“˜

ActionGit Command
Init repogit init
Add filesgit add .
Commitgit commit -m ""
Create branchgit branch new
Switch branchgit checkout new
Mergegit merge new
Pushgit push
Pullgit pull
Tagsgit tag

Best Practices πŸ’‘

  • βœ” Commit small & meaningful changes
  • βœ” Write clear commit messages
  • βœ” Use branches for features/bugs
  • βœ” Never commit secrets (.env files)
  • βœ” Pull before pushing
  • βœ” Use PRs for team collaboration

Conclusion πŸŽ‰

>>β€œGit is your project's time machine β€” giving you full control, safety, and collaboration superpowers.” ✨

You now understand Git Version Control! Want the next topic? Try GitHub, Git Branching Strategies, CI/CD Pipelines, or Docker. Just tell me! 😊