Developer Guides

Git Tutorial: A Step-by-Step Guide for Beginners

Master the fundamentals of Git with this step-by-step tutorial, covering add, commit, push, and token-based authentication.

3 min read

Git Tutorial: A Step-by-Step Guide for Beginners

Git is the backbone of modern version control, essential for collaborative and remote software development workflows. This guide is designed as a crash course to help beginners master the basics of Git, including repository initialization, adding and committing changes, pushing to GitHub, and using token-based authentication.


Prerequisites

Before diving into Git, ensure you have the following:

prerequisites


Steps to Initialize a Repository

Follow these steps to start a version-controlled project using Git:

steps

  1. Open a terminal and navigate to your project directory:
    bash
    cd /path/to/project
  2. Initialize a Git repository:
    bash
    git init
  3. Verify the initialization by checking for Git’s hidden .git folder:
    bash
    ls -a  # Lists hidden files

How to Stage Changes

After editing or creating files in your project, stage them for tracking:

steps

  1. Use the following to stage all changes:
    bash
    git add .
  2. Alternatively, stage specific files:
    bash
    git add filename
  3. Verify staged changes:
    bash
    git status

Committing Changes

Save staged changes to your repository with descriptive messages:

steps

  1. Commit staged changes with a message:
    bash
    git commit -m "Initial commit with basic setup"
  2. Confirm the commit history:
    bash
    git log --oneline

Token-Based Authentication with GitHub

GitHub now requires Personal Access Tokens (PATs) for HTTPS authentication instead of passwords.


Common Mistakes in Git Workflow

Avoid these pitfalls for smoother Git usage:


Verification

Ensure Git actions are successful:


FAQ

FAQ

How do I delete a local Git repository?

Run rm -rf .git inside the repository folder to remove the Git tracking entirely.

How do I resolve merge conflicts?

Open the conflicted file, review conflicting sections marked by Git, edit to resolve, then stage and commit the changes.

Why is Git asking for my username and password during git push?

If HTTP authentication is enabled, Git will prompt for credentials. Generate and replace your password with a Personal Access Token.

What does git clone do?

git clone copies a remote repository (e.g., from GitHub) to your local machine, allowing you to work offline.