DEV Community

Alex Arroyo
Alex Arroyo

Posted on

Learn How to Use Git & GitHub: A Guide for Absolute Beginners

Embarking on Your Coding Journey? You’ve probably heard of Git & GitHub. Dive in, and let’s demystify these essential tools for every developer.

Why Git & GitHub?

Version control is paramount for collaborative projects and individual projects alike. Git helps you manage changes to your code, while GitHub provides an online platform to share, collaborate, and version-control your repositories. Together, they’re an unbeatable combination for any developer.

Getting Started with Git

Setting Up Your Identity

It’s essential Git knows who’s making changes. Let’s set that up:

  1. Open your terminal or command prompt.
  2. Enter:
   git config --global user.name "Your Name"
   git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Tip: Ensure the email used is the same as your GitHub account for a seamless experience.

Creating Your First Repository

Before diving deep, check if a repository exists in your current directory:

ls -a
Enter fullscreen mode Exit fullscreen mode

If you spot a .git directory, a repository already exists!

Otherwise, initialize a new one with:

git init
Enter fullscreen mode Exit fullscreen mode

Syncing with GitHub

Setting Up Your GitHub Repository

Head over to the GitHub website and create a new repository.

Connecting the Dots

Now, let’s link your local repository (on your machine) with the remote one on GitHub:

git remote add origin https://github.com/your_username/repository_name.git
Enter fullscreen mode Exit fullscreen mode

Run into trouble? Don’t fret!

Check if a git repository already exists connected to the directory:

git remote -v
Enter fullscreen mode Exit fullscreen mode

If that’s the case, you can:

  • Modify the URL for origin:
  git remote set-url origin https://github.com/your_username/repository_name.git
Enter fullscreen mode Exit fullscreen mode
  • Or perhaps, you’d like to start afresh by removing origin and adding it again:
  git remote remove origin
  git remote add origin https://github.com/your_username/repository_name.git
Enter fullscreen mode Exit fullscreen mode
  • Alternatively, you can try a different name other than origin:
  git remote add another_name https://github.com/your_username/another_repository.git
Enter fullscreen mode Exit fullscreen mode

Tip: Naming conventions can help streamline your workflow. Choose names that resonate with your project.

Staying Updated with Git

Tracking Changes

Add and commit changes within your repository. This helps Git understand and track your modifications:

  • For all files:
  git add .
Enter fullscreen mode Exit fullscreen mode
  • For a specific file:
  git add file_name
Enter fullscreen mode Exit fullscreen mode
  • Commit the change with a descriptive message:
  git commit -m "Your descriptive message here"
Enter fullscreen mode Exit fullscreen mode

Updating GitHub

Every time you modify your code, it’s a good practice to push changes to GitHub:

  • If it’s your first push:
  git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • For subsequent pushes:
  git push origin main
Enter fullscreen mode Exit fullscreen mode

Quick Cheat Sheet:

git add
Enter fullscreen mode Exit fullscreen mode

or

git add file_name
Enter fullscreen mode Exit fullscreen mode

and

git commit -m "descriptive message"
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Determined to Learn More?

Delve deeper into the world of GitHub with its official documentation.

Extra Help:

If you’re just starting out and only making simple pushes on your own, you might want to check out the VisualGit script, which automates the processes described in this guide.

You can find it here.

Top comments (0)