Version control
Understanding Git basics
Developing software can be a team sport, and even if you're flying solo, keeping track of your code changes is super important. That's where version control comes in!
The problem Git solves
Imagine you're writing a huge Python script. You make some changes, and suddenly, something breaks. You wish you could go back to how it was yesterday, or even an hour ago.
Or, you're working with a friend on the same project. You both make changes, and merging them together becomes a nightmare. Who has the latest version?
How Git works
Git is like a super-smart historian for your code. It tracks every change you make to your files. Instead of just saving the 'final' version, it saves snapshots of your project over time.
This means you can always jump back to an older version, see who changed what, and easily combine work from different people without losing anything.
Key concepts (all you need)
- Repository (repo): Your project's folder, where Git stores all its history.
- Commit: A snapshot of your project at a specific moment. Each commit has a message describing the changes.
- Branch: Independent lines of development. You can work on new features without messing up the main project.
- Merge: Combining changes from one branch into another.
First: Install Git
Before we start, you need to have Git installed on your computer. It's usually a quick process.
- Windows: Download from
git-scm.com. - macOS: Git often comes pre-installed, or you can get it with Homebrew (
brew install git). - Linux: Use your distribution's package manager (e.g.,
sudo apt install gitfor Debian/Ubuntu).
After installation, open your terminal or command prompt and type:
git --versionYou should see the Git version printed, confirming it's installed.
The simplest Git workflow
Let's create a new project and add it to Git.
mkdir my_python_project
cd my_python_project
git initNow, create a simple Python file, for example, hello.py:
# hello.py
def greet(name):
print(f"Hello, {name}!")
if __name__ == "__main__":
greet("World")Tell Git about your new file and commit your first changes:
git add hello.py
git commit -m "Initial commit: Add hello.py script"You've just made your first commit! Now, let's make a small change to hello.py:
# hello.py
def greet(name):
print(f"Hello there, {name}!") # Changed this line
if __name__ == "__main__":
greet("Pythonista") # Changed this line as wellAnd commit the changes again:
git add hello.py
git commit -m "Refactor: Improve greeting message"Common questions
- "What if I mess up?" Git is designed for exactly that! You can always revert to a previous commit.
- "Do I need to commit every tiny change?" Not usually. Group related changes into a single commit with a clear message.
- "What's
git statusfor?" It shows you which files Git is tracking, which are changed, and which are ready to be committed. Use it often!
What's next?
This was just a tiny peek into the world of Git. You've learned the absolute basics of tracking your project's history.
- Explore branching and merging to work on features concurrently.
- Learn about remote repositories like GitHub or GitLab to collaborate with others.
- Practice regularly! The more you use Git, the more comfortable you'll become.