DEV Community

Programming Entry Level: learn github

Understanding Learn GitHub for Beginners

Have you ever worked on a project and wished you could easily track changes, collaborate with others, or revert to an older version if something went wrong? That's where GitHub comes in! For a new programmer, learning GitHub can seem daunting, but it's an incredibly valuable skill. Many job interviews now expect candidates to be familiar with version control and GitHub, and it's essential for contributing to open-source projects. This post will break down the basics of GitHub in a friendly, easy-to-understand way.

Understanding "Learn GitHub"

GitHub is a web-based platform built around something called Git. Think of Git as a super-powered "undo" and "save" system for your code. Instead of just saving my_file.py as my_file_v2.py, my_file_final.py, and so on (which gets messy quickly!), Git tracks changes to your files over time.

Imagine you're writing a story with a friend. Instead of emailing drafts back and forth, you both work on a shared document. Git is like that shared document, but for code. It keeps a history of every change, who made it, and when.

Here's a simple analogy:

  • Your project: A Lego castle.
  • Git: The instructions for building the castle, and a record of every change you make to it.
  • GitHub: A place to store those instructions online, share them with friends (collaborators), and keep a backup.

Key concepts you'll encounter:

  • Repository (Repo): A folder containing all your project files, along with the Git history.
  • Commit: A snapshot of your project at a specific point in time. Think of it as saving a version of your Lego castle after you've added a tower.
  • Branch: A separate line of development. Imagine building a new wing onto your castle without affecting the original structure.
  • Pull Request: A request to merge changes from one branch into another. This is how you propose your new wing be added to the main castle.

You can visualize this with a simple diagram:

graph TD
    A[Main Branch] --> B(Commit 1: Added foundation);
    B --> C(Commit 2: Built walls);
    C --> D(Commit 3: Added a tower);
    A --> E[New Branch: Adding a moat];
    E --> F(Commit 4: Dug the moat);
Enter fullscreen mode Exit fullscreen mode

Basic Code Example

Let's say you have a simple Python script:

def greet(name):
  print("Hello, " + name + "!")

greet("World")
Enter fullscreen mode Exit fullscreen mode

This script greets the world. Now, let's imagine you want to add a feature to greet a different language. You'd make changes, and then "commit" those changes to Git. While you don't directly write code in GitHub, you use Git commands to manage your code and then push it to GitHub.

Here's how the process looks conceptually (using Git commands you'd run in your terminal):

  1. Initialize a Git repository: git init (This creates a hidden .git folder in your project directory)
  2. Stage your changes: git add . (This tells Git which files to include in the next snapshot)
  3. Commit your changes: git commit -m "Added greeting function" (This saves a snapshot with a descriptive message)
  4. Connect to GitHub: git remote add origin <your_github_repo_url> (This links your local repository to your GitHub repository)
  5. Push your changes: git push -u origin main (This uploads your commits to GitHub)

Common Mistakes or Misunderstandings

Here are a few common pitfalls for beginners:

❌ Incorrect: Committing too often with vague messages.

git commit -m "Update"
Enter fullscreen mode Exit fullscreen mode

✅ Correct: Committing logically with descriptive messages.

git commit -m "feat: Add Spanish greeting option"
Enter fullscreen mode Exit fullscreen mode

Explanation: Vague commit messages like "Update" don't tell anyone (including your future self!) what changed. Descriptive messages make it easier to understand the history of your project.

❌ Incorrect: Forgetting to git add files before committing.

You modify a file, run git commit -m "My changes", but the changes aren't included!

✅ Correct: Always git add before git commit.

git add my_file.py
git commit -m "My changes"
Enter fullscreen mode Exit fullscreen mode

Explanation: git add stages the changes, telling Git to include them in the next commit.

❌ Incorrect: Pushing directly to the main branch without a pull request (when collaborating).

This can disrupt the project for others.

✅ Correct: Create a branch, make changes, and submit a pull request.

Explanation: Pull requests allow for code review and discussion before changes are merged into the main branch.

Real-World Use Case

Let's say you're building a simple to-do list application. You could structure your project like this:

to_do_list/
├── README.md          # Project description

├── todo.py            # Main application logic

├── tests/             # Unit tests

│   └── test_todo.py
└── requirements.txt   # Dependencies

Enter fullscreen mode Exit fullscreen mode

You'd use Git to track changes to each file. If you wanted to add a feature to prioritize tasks, you'd:

  1. Create a new branch: git checkout -b prioritize-tasks
  2. Modify todo.py to add priority levels.
  3. Write unit tests in test_todo.py to verify the new feature.
  4. Commit your changes: git commit -m "feat: Add task prioritization"
  5. Push your branch to GitHub.
  6. Create a pull request to merge prioritize-tasks into the main branch.

Practice Ideas

Here are some small projects to practice your GitHub skills:

  1. Simple Calculator: Create a basic calculator program and track your changes with Git.
  2. Text-Based Game: Build a simple text-based adventure game and use branches to experiment with different storylines.
  3. Personal Website: Create a basic HTML/CSS website and host it on GitHub Pages.
  4. Contribute to an Open-Source Project: Find a small bug or feature request in an open-source project and submit a pull request.
  5. Documentation Project: Write documentation for a small Python library and use GitHub to manage the documentation files.

Summary

You've now learned the fundamental concepts of Git and GitHub. You understand what repositories, commits, branches, and pull requests are, and how they work together. Don't be afraid to experiment, make mistakes, and learn from them. GitHub is a powerful tool that will become invaluable as you continue your programming journey.

Next steps: Explore more advanced Git commands like git rebase, git merge, and learn about GitHub Actions for automating tasks. Happy coding!

Top comments (0)