Skip to main content

Command Palette

Search for a command to run...

Git for Beginners

Basic Git Commands for New Users

Updated
4 min read
Git for Beginners

TL;DR: Git is a distributed version control system that helps you track code changes, collaborate safely, and recover from mistakes. In this article, you will learn a few core concepts (repository, commit, branch, HEAD) and these commands: git init, git status, git add, git commit, git log, git branch, git checkout, git push, git pull. Walk through the simple workflow below, and you'll cover ~90% of daily Git tasks.

What is Git?

Git is a tool that records snapshots of your project over time. Think of it like a time machine for files: it lets you save "checkpoints" (commits), go back to previous checkpoints, and work with others without accidentally overwriting each other's work.

Why Git is used

  • History: Every change is stored with who made it and why.

  • Collaboration: Multiple people can work on the same project; conflicts are surfaced and resolved.

  • Safety: You can revert to earlier states and recover lost work.

  • Branching: Experiment in isolation (feature branches) and merge when ready.

Core terminologies

  • Repository (repo): The project folder tracked by Git. Can be local (on your machine) or remote (GitHub/GitLab).

  • Commit: A snapshot of tracked files plus a message describing the change.

  • Working directory: Your current files that you edit.

  • Staging area/index: A place where you collect changes that will go into the next commit (git add).

  • HEAD: Pointer to the current commit (usually the tip of the checked-out branch).

  • Branch: A movable pointer to commits; use branches to isolate features.

  • Remote: A version of the repo hosted elsewhere (e.g., origin on GitHub).

  • Merge / Rebase: Ways to integrate changes from one branch into another.

Common Git commands

# Initialize a new Git repo in the current folder
git init

# See the state of your working directory and staging area
git status

# Stage a file or files for the next commit
git add file.txt
git add -A           # stage all changes (new, modified, deleted)

# Commit staged changes with a message
git commit -m "feat: add login form"

# Show commit history
git log --oneline --graph --decorate

# Create & switch to a branch
git checkout -b feature/login

# Switch to existing branch
git checkout main

# Merge a branch into current branch
git merge feature/login

# Rebase current branch onto main (rewrites history; be careful)
git rebase main

# List remotes
git remote -v

# Push branch to remote
git push -u origin feature/login

# Fetch + merge changes from remote
git pull

# See the differences not yet staged
git diff

# See staged changes
git diff --staged

# Undo last commit but keep changes staged
git reset --soft HEAD~1

Basic developer workflow

  1. Start a project

     mkdir my-app
     cd my-app
     git init
    
  2. Create files and check status

     echo "# My App" > README.md
     git status
    
  3. Stage and commit

     git add README.md
     git commit -m "docs: add README"
    
  4. Create a feature branch

     git checkout -b feat/signup
     # implement code
     git add .
     git commit -m "feat: create signup form + basic validation"
    
  5. Push the branch and open a Pull Request (PR)

     git remote add origin git@github.com:you/repo.git   # one-time
     git push -u origin feat/signup
    

    Open a PR on your remote host (GitHub/GitLab) for review.

  6. After approval, merge to main

    • Use the remote UI to merge (recommended) or:
    git checkout main
    git pull
    git merge feat/signup
    git push
  1. Clean up (delete the branch)

     git branch -d feat/signup
     git push origin --delete feat/signup  # optional - delete remote branch
    

Commit message best practices

Use short, descriptive messages and, when helpful, a longer body:

feat(auth): add JWT-based login

- validate credentials
- issue token with 1h expiry
- add unit tests for token helper

Closes #42

Tip: use imperative tense (add, fix, update) and a type like feat, fix, chore, docs.

Small but powerful habits

  • Commit early, commit often — small commits are easier to review.

  • git status is your friend; run it frequently.

  • Pull (git pull) before you start working to reduce conflicts.

  • Use .gitignore to avoid committing generated files or secrets.

  • Keep PRs small and focused.

Suggested .gitignore starters

  • For Node projects:
node_modules/
dist/
.env
  • For Python:
__pycache__/
venv/
*.pyc
.env