GeekDas
••11 min read•
GeekdasGeekdas

10 Basic Git Commands Every Beginner Should Know (With Examples)

10 Basic Git Commands Every Beginner Should Know (With Examples)

Git is a powerful version control system, but getting started doesn't require memorizing dozens of commands. By learning a few essential Git commands, you can confidently manage your code, track changes, and collaborate with others.

In this guide, we'll cover 10 basic Git commands, explain what each one does, show simple examples with sample output, and point out the mistakes beginners make most often. We'll finish with a complete workflow that ties everything together, plus a bonus command you will use almost every day.

Before you start: make sure Git is installed on your computer. If it isn't, follow our guide on how to install Git on Ubuntu first (replace # with the link to your install post), and set your name and email with git config --global user.name "Your Name" and git config --global user.email "you@example.com".

Quick Cheat Sheet

#

Command

What it does

1

git init

Creates a new Git repository

2

git status

Shows the state of your working directory

3

git add

Stages changes for the next commit

4

git commit

Saves a snapshot of staged changes

5

git log

Shows commit history

6

git branch

Lists, creates and deletes branches

7

git checkout

Switches branches or restores files

8

git merge

Combines one branch into another

9

git remote

Manages connections to remote repositories

10

git push

Uploads commits to a remote repository

Bonus

git pull

Downloads and merges remote changes

How Git Works: The Basics in 60 Seconds

Understanding four terms makes every command below much easier to follow:

  • Working directory: the project folder where you edit files.

  • Staging area: a holding area where you choose which changes go into the next commit.

  • Local repository: the hidden .git folder that stores your full project history on your machine.

  • Remote repository: a copy of your project hosted online, for example on GitHub, GitLab or Bitbucket.

The everyday flow is: edit files, stage them with git add, save them with git commit, and share them with git push.

1. git init - Initialize a Repository

This command creates a new Git repository in your project directory. It sets up all the necessary Git files to start tracking changes.

mkdir my-project
cd my-project
git init

Sample output:

Initialized empty Git repository in /home/user/my-project/.git/

What it does:

  • Creates a hidden .git folder

  • Turns your current directory into a Git repository

Tips:

  • Run git init only once per project, at the project's root folder.

  • To name the first branch main right away, use git init -b main.

  • Never run git init inside another repository unless you really want a nested one.

  • Already have a project on GitHub? Use git clone <url> instead, which downloads it and sets everything up for you.

2. git status - Check Repository Status

git status shows the current state of your working directory and staging area. It is the command you will run most often, so get used to it early.

git status

Sample output:

On branch main
Changes not staged for commit:
        modified:   index.html

Untracked files:
        style.css

What it does:

  • Shows modified files

  • Shows untracked files

  • Indicates what's staged for commit

Tips:

  • Run git status before every git add and git commit so you know exactly what you are about to save.

  • For a compact view, use git status -s.

3. git add - Stage Changes

This command stages files, preparing them to be committed.

Add a single file:

git add index.html

Add all files:

git add .

What it does:

  • Moves changes to the staging area

  • Does not save changes permanently (that's what commits are for)

Useful variations:

Command

Result

git add index.html style.css

Stage several specific files

git add .

Stage all changes in the current folder and below

git add -A

Stage all changes in the whole repository, including deletions

git add -p

Review and stage changes piece by piece

Tips:

  • Staging lets you split your work into small, focused commits instead of one giant one.

  • Be careful with git add . because it can stage files you did not mean to include, such as passwords or .env files. Use a .gitignore file to keep those out.

  • Staged a file by mistake? Undo it with git restore --staged <file>.

4. git commit - Save Changes

A commit records a snapshot of your staged changes with a message.

git commit -m "Initial commit"

Sample output:

[main (root-commit) a1b2c3d] Initial commit
 1 file changed, 12 insertions(+)
 create mode 100644 index.html

What it does:

  • Saves changes to the repository history

  • Associates changes with a message and author

Tips for writing good commit messages:

  • Keep the first line short (around 50 characters) and in the present tense, for example "Add login form".

  • Describe what changed and why, not just "update" or "fix".

  • Commit often. Small commits are easier to review and easier to undo.

Useful variations:

  • git commit -am "message" stages all modified, tracked files and commits in one step. It does not include new, untracked files.

  • git commit --amend lets you fix the message (or add a forgotten file) on your most recent commit, as long as you have not pushed it yet.

5. git log - View Commit History

This command displays a list of all commits in the repository.

git log

Sample output:

commit a1b2c3d4e5f6... (HEAD -> main)
Author: Your Name <you@example.com>
Date:   Fri Oct 23 15:30:00 2026 +0530

    Initial commit

What it does:

  • Shows commit IDs, authors, dates, and messages

  • Helps track project history

Useful variations:

git log --oneline
git log --oneline --graph --decorate --all
git log -n 5
  • --oneline shows one compact line per commit.

  • --graph --decorate --all draws a text diagram of your branches.

  • -n 5 limits output to the last five commits.

Press q to exit the log view if it fills your screen.

6. git branch - Manage Branches

Branches allow you to work on features without affecting the main code. Think of a branch as a separate line of development that you can later merge back.

List branches:

git branch

Create a new branch:

git branch feature-login

What it does:

  • Lists existing branches (the current one is marked with *)

  • Creates new branches

Useful variations:

Command

Result

git branch -a

List local and remote branches

git branch -d feature-login

Delete a branch that has been merged

git branch -m new-name

Rename the current branch

Note: git branch feature-login creates the branch but does not switch to it. To create and switch in one step, use git checkout -b feature-login (covered next) or git switch -c feature-login.

7. git checkout - Switch Branches

This command switches between branches or restores files.

git checkout feature-login

Sample output:

Switched to branch 'feature-login'

What it does:

  • Changes your working directory to another branch

  • Lets you work on different versions of the project

Useful variations:

  • git checkout -b feature-login creates a new branch and switches to it.

  • git checkout - jumps back to the previous branch.

Modern alternatives: Newer versions of Git split the jobs of git checkout into two clearer commands:

  • git switch feature-login switches branches.

  • git restore index.html discards uncommitted changes to a file.

git checkout still works everywhere and you will see it in countless tutorials, so it is worth knowing both.

Tip: Commit or stash your work before switching branches. If you have uncommitted changes that conflict, Git will refuse to switch.

8. git merge - Merge Branches

git merge combines changes from one branch into another. You first switch to the branch that should receive the changes, then merge.

git checkout main
git merge feature-login

What it does:

  • Merges another branch into the current branch

  • Integrates feature work into main or master

What to expect:

  • Fast-forward merge: if main has not changed since you created the branch, Git simply moves the pointer forward.

  • Merge commit: if both branches have new commits, Git creates a new commit that joins them.

  • Merge conflict: if both branches changed the same lines, Git stops and asks you to decide. Open the marked files, look for the <<<<<<<, ======= and >>>>>>> markers, keep the code you want, then run git add and git commit to finish. To cancel the merge, run git merge --abort.

Conflicts look scary at first, but they are normal in team projects and easy to resolve once you have done it a couple of times.

9. git remote - Manage Remote Repositories

This command manages connections to remote repositories like GitHub.

List remotes:

git remote -v

Add a remote:

git remote add origin https://github.com/username/repo.git

What it does:

  • Links your local repository to remote repositories

  • Enables pushing and pulling code

Good to know:

  • origin is just the conventional name for your main remote. You can pick another name, but almost everyone uses origin.

  • Typed the wrong URL? Fix it with git remote set-url origin <new-url>.

  • git remote remove origin removes the connection.

  • Prefer SSH over HTTPS so you do not have to authenticate every time. Our guide on how to set up SSH in Ubuntu and clone repositories using SSH walks you through it. If you use HTTPS instead, GitHub requires a personal access token rather than your account password.

10. git push - Upload Changes to Remote

git push sends your local commits to a remote repository.

git push origin main

What it does:

  • Uploads commits to GitHub/GitLab/Bitbucket

  • Shares your work with others

Tips:

  • The first time you push a new branch, use git push -u origin main. The -u flag remembers the remote branch, so next time a plain git push is enough.

  • To publish a feature branch, run git push -u origin feature-login.

  • If Git rejects your push because the remote has newer commits, run git pull first, then push again.

  • Avoid git push --force on shared branches, because it can overwrite your teammates' work.

Bonus Command: git pull - Get Latest Changes

While not in the main list, this is one of the most-used commands.

git pull origin main

What it does:

  • Fetches and merges updates from a remote repository

  • Keeps your local code up to date

Good to know:

  • git pull is a shortcut for git fetch (download the changes) followed by git merge (combine them with your work).

  • If you want to look at remote changes before merging, run git fetch on its own.

  • git pull --rebase replays your commits on top of the latest remote commits for a cleaner history.

Make it a habit to pull before you start working each day, and before you push.

Putting It All Together: A Complete Beginner Workflow

Here is how these commands fit into a real workflow, from an empty folder to code on GitHub:

# 1. Create a project and start tracking it
mkdir my-project && cd my-project
git init -b main

# 2. Create a file, check status, stage and commit
echo "<h1>Hello Git</h1>" > index.html
git status
git add index.html
git commit -m "Add homepage"

# 3. Connect to GitHub and push
git remote add origin https://github.com/username/my-project.git
git push -u origin main

# 4. Work on a new feature in its own branch
git checkout -b feature-login
# ...edit files...
git add .
git commit -m "Add login page"

# 5. Merge the feature back into main and share it
git checkout main
git merge feature-login
git push

# 6. Review the history
git log --oneline --graph

Create an empty repository on GitHub first (without a README) so the first push goes through without conflicts.

Common Beginner Mistakes (and How to Avoid Them)

  • Forgetting to run git add: git commit only saves staged changes. Check with git status.

  • Writing vague commit messages: "fix" and "update" tell your future self nothing.

  • Committing secrets: passwords, API keys and .env files should go in .gitignore, never in your history.

  • Working directly on main: use a branch for each feature or fix, then merge.

  • Pushing without pulling: pull first to avoid rejected pushes and surprise conflicts.

  • Panicking at merge conflicts: read the markers, keep the right code, and commit. git merge --abort is always available.

Conclusion

Mastering these 10 basic Git commands gives you a solid foundation for version control. With just these commands, you can:

  • Track changes

  • Save progress

  • Collaborate with teams

  • Manage project history

Once you're comfortable with these basics, you can explore advanced features like rebasing, stashing, and resolving merge conflicts. A good next step is to create a practice repository, try a branch and a deliberate merge conflict, and push it to GitHub. Hands-on practice is the fastest way to make these commands second nature.

Was this article helpful?

Comments

Leave a comment

Loading comments...

Frequently Asked Questions

What are the most important Git commands for beginners?
The ten covered here (init, status, add, commit, log, branch, checkout, merge, remote and push) plus pull cover the vast majority of daily Git use.
What is the difference between git add and git commit?
git add places your changes in the staging area. git commit takes everything in the staging area and saves it permanently in the repository history.
What is the difference between Git and GitHub?
Git is the version control tool that runs on your computer. GitHub is an online service that hosts Git repositories so you can back up and share your code.
What is the difference between git fetch and git pull?
git fetch only downloads new commits from the remote. git pull downloads them and also merges them into your current branch.
Should I use main or master?
Both work. main is now the default on GitHub and in recent Git setups, and you can set it for new repositories with git config --global init.defaultBranch main.
How do I undo my last commit?
If you have not pushed it, git reset --soft HEAD~1 removes the commit but keeps your changes staged. If you have already pushed, use git revert <commit-id>, which safely creates a new commit that undoes the old one.
Do I need to memorize all these commands?
No. Learn the workflow first and you will remember the commands naturally. git help <command> or git <command> --help is always available when you forget an option.

Related Articles