Learn2Code
learn2code
← Back to Blog
Tools10 min read

Git for Beginners: The Essential Commands You Actually Need

Learn the Git commands that matter most for beginners. This practical guide covers init, add, commit, push, pull, branch, and merge with clear examples and common workflows.

Learn2Code Team

January 24, 2026

Why Every Developer Needs Git

Git is the version control system used by virtually every software team in the world. GitHub alone hosts over 200 million repositories. Whether you are building a personal project or working at a Fortune 500 company, you will use Git daily.

Version control solves a fundamental problem: how do you track changes to your code over time, collaborate with others, and undo mistakes without losing work? Before Git, developers emailed zip files to each other and named folders "project-v2-final-FINAL." Git replaced that chaos with a system that tracks every change, who made it, and why.

If you are learning to code and have not learned Git yet, now is the time. It is a core skill that every job posting expects.

The Mental Model: Snapshots, Not Diffs

The easiest way to understand Git is to think of it as a camera for your code. Every time you "commit," Git takes a snapshot of all your files at that moment. You can look back at any snapshot, compare snapshots, or restore your project to any previous snapshot.

Your project has three main areas in Git:

  1. Working directory -- the files you see and edit on your computer
  2. Staging area -- files you have marked to include in your next snapshot
  3. Repository -- the collection of all snapshots (commits) you have made

The typical workflow is: edit files, stage the changes, then commit the snapshot. Each commit has a message describing what changed and why.

Setting Up Git

Before using Git, configure your identity. This information appears on every commit you make.

code.js
1git config --global user.name "Your Name"
2git config --global user.email "your.email@example.com"

Check your configuration:

code.js
1git config --list

The 10 Commands You Need

1. git init -- Start a New Repository

code.js
1# Create a new project folder and initialize Git
2mkdir my-project
3cd my-project
4git init

This creates a hidden .git folder that stores all version history. You only run this once per project.

2. git clone -- Copy an Existing Repository

code.js
1# Clone a repository from GitHub
2git clone https://github.com/username/repository-name.git

This downloads the entire project and its history to your computer. Use this when joining an existing project.

3. git status -- Check What Has Changed

code.js
1git status

This is the command you will run most often. It shows which files have been modified, which are staged for commit, and which are untracked (new files Git does not know about yet).

4. git add -- Stage Changes

code.js
1# Stage a specific file
2git add index.html
3 
4# Stage multiple files
5git add index.html style.css
6 
7# Stage all changes in the current directory
8git add .

Staging does not save anything permanently. It prepares files for the next commit. Think of it as loading film into the camera before taking the photo.

5. git commit -- Save a Snapshot

code.js
1# Commit staged changes with a message
2git commit -m "Add navigation bar to homepage"

Each commit is a permanent snapshot. The message should describe what you changed and why. Good commit messages make it easy to understand your project's history months later.

Good commit messages:

  • "Fix login button not responding on mobile"
  • "Add user profile page with avatar upload"
  • "Remove deprecated API endpoint for v1 users"

Bad commit messages:

  • "fix stuff"
  • "update"
  • "asdf"

6. git log -- View History

code.js
1# See commit history
2git log
3 
4# Compact one-line format
5git log --oneline
6 
7# Show last 5 commits
8git log --oneline -5

Each commit shows its unique ID (a hash like a1b2c3d), the author, the date, and the message.

7. git branch -- Work on Features Separately

code.js
1# List all branches
2git branch
3 
4# Create a new branch
5git branch feature-login
6 
7# Switch to a branch
8git checkout feature-login
9 
10# Create and switch in one command
11git checkout -b feature-login

Branches let you work on new features without affecting the main code. When the feature is ready, you merge it back. This is how teams work on multiple features simultaneously without breaking each other's code.

8. git merge -- Combine Branches

code.js
1# Switch to main branch
2git checkout main
3 
4# Merge feature branch into main
5git merge feature-login

This takes all the commits from feature-login and applies them to main. If both branches changed the same lines, Git will flag a "merge conflict" that you resolve manually.

9. git push -- Upload to Remote

code.js
1# Push commits to the remote repository
2git push origin main
3 
4# Push a new branch to remote
5git push -u origin feature-login

This sends your local commits to a remote server (usually GitHub, GitLab, or Bitbucket) where others can see and pull your changes.

10. git pull -- Download Updates

code.js
1# Pull latest changes from remote
2git pull origin main

This downloads and merges any new commits from the remote repository into your local branch. Run this before starting new work to make sure you have the latest code.

The Daily Workflow

Here is the workflow you will use 90% of the time:

code.js
1# 1. Pull latest changes
2git pull origin main
3 
4# 2. Create a branch for your work
5git checkout -b feature-new-button
6 
7# 3. Make your changes (edit files)
8 
9# 4. Check what changed
10git status
11 
12# 5. Stage your changes
13git add .
14 
15# 6. Commit with a descriptive message
16git commit -m "Add submit button to contact form"
17 
18# 7. Push to remote
19git push -u origin feature-new-button
20 
21# 8. Create a pull request on GitHub (done in the browser)
22 
23# 9. After the PR is merged, switch back to main
24git checkout main
25git pull origin main

Common Mistakes and How to Fix Them

"I committed to the wrong branch"

code.js
1# Undo the last commit but keep the changes
2git reset HEAD~1
3 
4# Now switch to the correct branch
5git checkout correct-branch
6 
7# Stage and commit again
8git add .
9git commit -m "Your message"

"I want to undo my last commit"

code.js
1# Undo commit, keep changes staged
2git reset --soft HEAD~1
3 
4# Undo commit, keep changes unstaged
5git reset HEAD~1
6 
7# Undo commit and discard changes (dangerous!)
8git reset --hard HEAD~1

"I have merge conflicts"

Merge conflicts happen when two branches change the same lines. Git marks the conflicts in your files:

code.js
1<<<<<<< HEAD
2Your version of the code
3=======
4Their version of the code
5>>>>>>> feature-branch

To resolve: edit the file to keep the code you want, remove the conflict markers (<<<<<<<, =======, >>>>>>>), then stage and commit.

"I want to see what changed before committing"

code.js
1# See unstaged changes
2git diff
3 
4# See staged changes
5git diff --staged

.gitignore -- Files Git Should Ignore

Create a .gitignore file in your project root to tell Git which files to ignore:

code.js
1# Dependencies
2node_modules/
3 
4# Environment variables
5.env
6 
7# Build output
8dist/
9build/
10 
11# OS files
12.DS_Store
13Thumbs.db

Never commit node_modules, .env files, or build output. These are either too large, contain secrets, or can be regenerated.

Git vs GitHub

Git and GitHub are not the same thing:

  • Git is the version control tool that runs on your computer
  • GitHub is a website that hosts Git repositories and adds collaboration features (pull requests, issues, code review)

You can use Git without GitHub (with GitLab, Bitbucket, or even just locally). But GitHub is where most open-source projects live and where employers look for your portfolio.

Next Steps After the Basics

Once you are comfortable with the commands above, explore these topics:

  • Pull requests -- the standard way to propose changes on teams
  • Rebasing -- an alternative to merging that creates a cleaner history
  • Stashing -- temporarily save changes without committing
  • Cherry-picking -- apply a specific commit from one branch to another
  • Git hooks -- automate tasks before or after commits

Start Using Git Today

If you are writing code without Git, you are one bad edit away from losing everything. Git is not just a collaboration tool -- it is a safety net for your work.

Start by initializing Git in your current project (git init), making your first commit, and pushing to a GitHub repository. From there, the daily workflow becomes second nature within a week.

Related Reading

#git#version-control#beginners#github#developer-tools

Ready to practice what you learned?

Apply these concepts with our interactive coding exercises.

Start Practicing