Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
3 min read
Git for Beginners: Basics and Essential Commands
  1. 1. What is Git?

    Git is a distributed version control system. Think of it as a time machine for your code:

    • It tracks every change you make to your files.

    • It lets you save different versions of your project.

    • It helps teams work together without overwriting each other’s work.

Key Point: Git is not GitHub/GitLab—those are online platforms that host Git repositories.


2. Why Use Git?

  • Track Changes: See who made what change and when.

  • Collaborate: Work with others on the same project without conflicts.

  • Backup: Never lose code—restore old versions anytime.

  • Experiment: Try new ideas in branches; discard or merge them later.


3. Git Basics: Core Terms

TermWhat It Means
Repository (Repo)Your project folder where Git tracks changes.
CommitA saved snapshot of your project at a point in time.
BranchA parallel version of your project (e.g., main, feature/login).
HEADA pointer to your current branch/commit (like a bookmark).
StagePreparing changes to be committed (using git add).
RemoteA copy of your repo on a server (e.g., GitHub).

4. Essential Git Commands

Start a Project

    git init          # Turns your folder into a Git repo

Check Status

    git status

Save Changes

    git add file.txt  # Stage a file for commit
    git add .         # Stage all changes
    git commit -m "Fixed login bug"  # Save changes with a message

View History

    git log           # See all commits (press 'q' to exit)
    git log --oneline # Compact view

Work with Branches

    git branch        # List all branches
    git branch new-feature  # Create a new branch
    git checkout new-feature  # Switch to that branch

Connect to Remote (GitHub/GitLab)

    git remote add origin https://github.com/your-repo.git
    git push -u origin main  # Upload your code

5. Basic Developer Workflow (Step-by-Step)

  1. Start a Project:

     mkdir my-project
     cd my-project
     git init
    
  2. Make Changes:

    • Edit index.html and style.css.
  3. Stage & Commit:

     git add .
     git commit -m "Added homepage styling"
    
  4. Create a Branch (for new features):

     git branch add-login
     git checkout add-login
    
  5. Push to Remote (GitHub):

     git push origin add-login
    
  6. Merge Changes (after review):

     git checkout main
     git merge add-login
    

Image of git-story: Create mp4 Video Animations of Your Git Commit History with 1 Command

6. Pro Tips for Beginners

  • Commit Often: Small, frequent commits are easier to track.

  • Write Good Messages: Use clear, descriptive commit messages (e.g., "Fix login button alignment" instead of "fixed bug").

  • Pull Before Push: Always run git pull before pushing to avoid conflicts.

  • Use .gitignore: Tell Git to ignore files (like node_modules/ or .env).


7. Common Mistakes & Fixes

MistakeFix
Forgot to stage a filegit add forgotten-file.txt then commit again.
Wrong commit messagegit commit --amend to edit the last commit.
Accidental commit to mainCreate a new branch and reset main: git branch temp; git reset --hard HEAD~1

8. Summary: Git in One Paragraph

Git is your code’s safety net. It lets you save versions, experiment in branches, and collaborate with others. Start with init, add, commit, and push—everything else builds on these. Practice on small projects first!