How you can learn Git and GitHub while you‘re learning to code

As an aspiring developer, you have a lot on your plate. Between learning one or more programming languages, building projects for your portfolio, and developing problem-solving skills, it may feel like there‘s hardly time for anything else. But one skill you absolutely can‘t afford to neglect is version control with Git and GitHub.

Used by millions of developers worldwide, Git is a distributed version control system that allows you to track changes to your codebase over time. GitHub is a cloud-based hosting service built on top of Git that enables you to store your code remotely and collaborate seamlessly with other developers. Together, they form an indispensable toolkit that every coder needs in their arsenal.

Consider these statistics:

  • Git is used by 93% of developers worldwide (Source: Stack Overflow Developer Survey 2018)
  • GitHub has over 56 million users and more than 100 million repositories, making it the largest host of source code in the world (Source: GitHub)
  • 77% of companies use Git for version control (Source: 2015 Eclipse Community Survey)

Clearly, Git and GitHub are not just niche tools used by a handful of coding geeks. They are essential technologies that power the software development industry. As a professional full-stack developer, I use Git and GitHub on a daily basis to manage my codebase, collaborate with my team, and contribute to open source projects. I simply couldn‘t do my job effectively without these tools.

In this comprehensive guide, I‘ll share a game plan for how you can learn Git and GitHub alongside whatever programming language or framework you‘re currently focusing on. We‘ll walk through everything from basic setup to advanced workflow strategies, sprinkling in real-world examples and best practices along the way. By the end, you‘ll have a solid foundation for using these powerful tools in your own projects.

But first, let‘s talk about why learning Git and GitHub is well worth your time and effort as a budding programmer.

The Importance of Version Control

Imagine you‘re working on a simple website project. You get the basic HTML structure in place, add some CSS for styling, and layer on interactivity with JavaScript. Things are going smoothly—until they aren‘t. One small change breaks the entire application and you can‘t figure out why. If only you could revert back to a previous working version!

This is where version control swoops in to save the day. By tracking the evolution of your code, version control allows you to revisit past iterations, undo mistakes, and isolate new changes without jeopardizing the integrity of the codebase. It‘s like an infinite undo button for your project.

But the benefits of version control extend far beyond the individual developer:

  • Collaboration: Git makes teamwork frictionless. Multiple developers can work on the same codebase simultaneously without stepping on each other‘s toes. Git‘s branching model allows teammates to work on features independently and merge changes back together seamlessly.

  • Accountability: Git keeps a detailed record of who changed what and when. This makes it easy to audit the codebase and track down the source of bugs. Paired with pull requests, this forms the backbone of a team‘s code review process.

  • Backup & Recovery: Storing your code in a Git repository ensures that it won‘t be lost if your local machine crashes. Pushing to a remote host like GitHub adds another layer of redundancy. If disaster strikes, you can always roll back to a previous version.

  • Experimentation: Git‘s branching model encourages developers to experiment freely without fear of breaking things. You can create a new branch, try out an idea, and discard it if it doesn‘t pan out—all without affecting the main codebase.

Moreover, using Git and GitHub signals to potential employers that you understand the tools and workflows of the professional development world. Hiring managers expect candidates to be well-versed in version control, and your GitHub profile can serve as a valuable addition to your programming portfolio.

According to the Stack Overflow Developer Survey 2020, nearly 82% of professional developers use Git for version control. Of those, 90% host their code on GitHub. Suffice it to say, Git and GitHub are table stakes for anyone serious about a career in software development.

Convinced? Great! Let‘s dive into how you can start learning Git and GitHub, step by step.

Getting Started with Git and GitHub

Step 1: Install Git

The first step is to install Git on your local machine. The process varies slightly depending on your operating system:

  • For Windows: Download the installer from the official Git website and run it, using all the default options.
  • For Mac: If you have Homebrew installed, simply run brew install git in the terminal. Otherwise, download the installer from the Git website.
  • For Linux: Use your distro‘s package manager, e.g. sudo apt install git on Debian/Ubuntu.

Step 2: Configure Git

Next, you‘ll need to do some basic configuration so Git knows your identity. Open up a terminal and run the following commands, replacing the name and email with your own:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

While you‘re at it, I also recommend configuring a default text editor (like VS Code or Sublime Text) and a tool for diffing and merging (like KDiff3 or P4Merge). See Git‘s first-time setup guide for instructions.

Step 3: Create a GitHub Account

If you don‘t already have one, head over to github.com and sign up for an account. Choose a username you wouldn‘t mind having on your resume, since GitHub can serve as a portfolio of your work.

I also recommend setting up SSH keys for authentication. This will allow you to securely push code to GitHub without entering your password each time. GitHub has a great guide on how to do this.

Step 4: Create Your First Repository

A Git repository, or "repo" for short, is a project folder that Git tracks. You‘ll want to create a new repo for each coding project you start. Here‘s how:

  1. On GitHub, click the + icon in the upper right corner and select "New repository".
  2. Choose a name for your repo (e.g. "my-first-project") and an optional description.
  3. Choose whether to make the repo public or private. I recommend going with public to start, so you can easily share your code.
  4. Click "Create repository".

Congrats, your first GitHub repo is live! But it‘s empty at the moment. Let‘s change that by linking it to your local machine.

The Basic Git Workflow: Add, Commit, Push

Now that you have Git installed locally and a GitHub repo to store your code, let‘s walk through the basic workflow for making and saving changes.

Step 1: Clone the Repo

To create a local copy of your empty GitHub repo, you‘ll need to "clone" it down to your machine. On the repo‘s main page, look for the green "Code" button and copy the URL it provides. Then, open up a terminal, navigate to the directory where you want to store your project, and run:

git clone <paste-the-url-you-copied>

This will create a new folder with the same name as your repo, initialized with Git tracking.

Step 2: Make Changes Locally

Using your favorite code editor, create some files in your project folder and write some code. It can be anything you like—a simple HTML page, a "Hello, World!" script, etc. Save the file(s) when you‘re done.

Step 3: Check Git Status

Back in your terminal, navigate to your project folder (using the cd command) and run:

git status

This will show you all the changes you‘ve made since your last commit. You should see the files you just created listed as "untracked".

Step 4: Stage Your Changes

For Git to include your new files in the next commit, you first need to "stage" them. Do this by running:

git add .

The . adds all changed files in the current directory. You can also specify individual filenames if you only want to stage certain changes.

Step 5: Commit Your Changes

Now that your changes are staged, you‘re ready to store them in Git‘s version history. Do this by running:

git commit -m "Add a short message describing your changes"

The -m flag lets you include a commit message inline. Try to keep these short but descriptive. I like to use the imperative mood (e.g. "Add booking form", not "Added booking form") to match Git‘s auto-generated messages.

Step 6: Push to GitHub

Finally, to sync your new local commit(s) up to GitHub, run:

git push origin main

This says "push my changes to the main branch of the origin remote" (which in this case is your GitHub repo). You‘ll be prompted to enter your GitHub username and password (or SSH passphrase if you set up keys).

And that‘s it! If you revisit your GitHub repo in the browser, you should see that your local changes are now reflected there. You‘ve just completed a full round trip of the basic Git workflow.

Leveling Up: Branches, Merging, and Pull Requests

The basic add-commit-push flow is great for solo projects, but Git really shines when you start using branches to manage multiple streams of development. Here‘s a typical team workflow:

  1. Create a new feature branch: git checkout -b add-user-authentication
  2. Commit changes to the feature branch: git commit -am "Implement basic auth"
  3. Push the feature branch to GitHub: git push -u origin add-user-authentication
  4. Open a pull request on GitHub to propose merging the feature branch into main
  5. Discuss and review the changes with teammates
  6. Merge the pull request and delete the feature branch

By isolating new work on feature branches, developers can work in parallel without stepping on each other‘s toes. Pull requests provide a forum to discuss changes before integrating them into the stable main branch.

Inevitably, merge conflicts will arise when multiple developers touch the same parts of the codebase. Git provides tools for resolving these conflicts. I won‘t get into the details here, but I highly recommend bookmarking this guide for when you need it.

Other common Git operations you‘ll encounter on a team:

  • git fetch: Download new changes from the remote repo without merging them
  • git rebase: "Rebase" your current branch on top of changes from another branch
  • git cherry-pick: Apply specific commits from one branch to another
  • git stash: Temporarily shelve changes you‘ve made to your working copy
  • git blame: See who last modified each line of a file and when

Collaborating on GitHub

GitHub is more than just a place to back up your Git repos—it‘s a powerful collaboration platform. Let‘s explore some of its key features.

Issues

GitHub Issues are a great way to track bugs, enhancements, and other development tasks. You can assign issues to specific team members, label them for searching and filtering, and link them to pull requests.

Forks

A fork is a copy of a repo that belongs to you. Forks allow you to freely experiment with changes without affecting the original project. They‘re commonly used to propose changes to someone else‘s project via pull request, or to use someone else‘s project as a starting point for your own.

Wikis

Each GitHub repo comes with a built-in wiki that you can use to document your project. This is a great place to write setup instructions, troubleshooting tips, or architectural overviews.

GitHub Actions

GitHub Actions allow you to automate development workflows right within GitHub. You can use them to run tests, deploy code, publish packages, and much more. Actions are defined in YAML files that live in your repo, making them version-controlled and portable.

GitHub Pages

GitHub Pages is a free hosting service for static websites. You can use it to host project documentation, your personal portfolio, or any other static content you like. Just create a new branch named gh-pages, push your HTML and CSS files to it, and your site will be live at https://<username>.github.io/<repo>.

Tips for Learning Git and GitHub

  1. Practice the basics: The best way to learn Git is to use it. Make a habit of committing your code every time you complete a meaningful chunk of work. Push your changes to GitHub at least once per day.

  2. Read the docs: Git‘s official documentation is comprehensive and well-written. I recommend starting with the Pro Git book, which you can read online for free.

  3. Embrace the command line: While there are many GUI clients for Git, I strongly recommend learning the command-line interface first. It‘s faster, more flexible, and more portable across systems.

  4. Don‘t be afraid to experiment: Git is designed to make it easy to undo mistakes. Don‘t be afraid to try new commands and workflows—you can always revert back if something goes wrong.

  5. Learn from others: Pay attention to how experienced developers use Git and GitHub. Read through pull requests on open-source projects to see how code reviews work. Ask your mentors for feedback on your workflow.

  6. Integrate with your toolchain: Most code editors and IDEs have built-in support for Git. Learn how to use these integrations to streamline your workflow. Also check out tools like GitHub Desktop, GitKraken, and Sourcetree for a more visual interface.

  7. Use aliases for common commands: Git allows you to define aliases for frequently used commands. For example, you can set git st as a shortcut for git status. See this guide for instructions.

  8. Keep your commit history clean: Aim for a clear, linear commit history that tells a story. Squash minor commits and rewrite unclear commit messages before merging a pull request. Use interactive rebase (git rebase -i) to clean up your branches before submitting them for review.

  9. Write good commit messages: A good commit message should concisely describe what changed and why. Follow the 50/72 rule: 50 characters or less for the subject line, 72 characters or less per line for the body.

  10. Contribute to open source: One of the best ways to learn Git and GitHub is to contribute to open-source projects. Start by fixing small bugs or adding documentation, and work your way up to more complex contributions. You‘ll not only sharpen your skills but also build a public portfolio of your work.

Conclusion

Learning Git and GitHub is a critical part of becoming a professional developer. The sooner you start using these tools in your projects, the more comfortable you‘ll become with them. Don‘t worry if it feels overwhelming at first—like any skill, mastering Git takes practice.

Remember, you don‘t have to learn everything at once. Start with the basics of staging, committing, and pushing code. Use branches to experiment with new features and collaborate with others. Take advantage of GitHub‘s many features to streamline your development workflow.

Over time, these practices will become second nature. You‘ll be able to focus on writing great code, secure in the knowledge that Git has your back. And when you‘re ready to showcase your skills to potential employers, your GitHub profile will speak volumes about your abilities as a developer.

So what are you waiting for? Install Git, create that first GitHub repo, and start versioning your code like a pro. The open-source world awaits!

Similar Posts