How to Use Git: A Practical Guide for Beginners

Git is the industry-standard version control system that tracks changes in your code. Whether you’re working solo or collaborating with a team, mastering Git boosts productivity and prevents chaos. This guide walks you through the essential commands and workflows in under 300 words.

First, ensure Git is installed on your machine. On Windows, download from git-scm.com; on macOS/Linux, use your package manager. After installation, open a terminal and set your identity with two commands: git config --global user.name "Your Name" and git config --global user.email "your@email.com". This metadata is attached to every commit.

Article illustration

1. Initialize and Track Changes

Navigate to your project folder and run git init. This creates a hidden .git directory. Now you can track files:

  • git add <file> stages a file for commit. Use git add . to stage all changes.
  • git commit -m "Your message" saves a snapshot of the staged changes. Write clear, concise messages.

Check the status anytime with git status and view commit history with git log.

2. Work with Remote Repositories

To collaborate, connect your local repo to a remote like GitHub or GitLab. First, create an empty repository on the platform, then link it:

  • git remote add origin https://github.com/username/repo.git
  • git push -u origin main uploads your local commits. After that, just git push works.
  • git pull downloads and merges changes from the remote into your current branch.
  • git clone <url> copies an existing remote repo to your machine.

3. Branching and Merging

Branches let you work on features without disturbing the main codebase. Create a new branch with git branch feature-name and switch to it using git checkout feature-name (or git switch feature-name). Merge changes back after testing:

  • Switch to the target branch (e.g., git checkout main).
  • Run git merge feature-name. Resolve any conflicts manually.
  • Delete the merged branch: git branch -d feature-name.

4. Undo and Recover Mistakes

Git offers safety nets:

  • git checkout -- <file> discards unstaged changes.
  • git reset HEAD <file> un-stages a file.
  • git revert <commit-hash> creates a new commit that reverses the given commit.

These commands keep your history clean and recoverable.

Mastering Git takes practice, but starting with these basics will make you confident in managing versions. Next, explore branching strategies and rebasing. Happy coding!

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *