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 |
| Creates a new Git repository |
2 |
| Shows the state of your working directory |
3 |
| Stages changes for the next commit |
4 |
| Saves a snapshot of staged changes |
5 |
| Shows commit history |
6 |
| Lists, creates and deletes branches |
7 |
| Switches branches or restores files |
8 |
| Combines one branch into another |
9 |
| Manages connections to remote repositories |
10 |
| Uploads commits to a remote repository |
Bonus |
| 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
.gitfolder 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 initSample output:
Initialized empty Git repository in /home/user/my-project/.git/What it does:
Creates a hidden
.gitfolderTurns your current directory into a Git repository
Tips:
Run
git initonly once per project, at the project's root folder.To name the first branch
mainright away, usegit init -b main.Never run
git initinside 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 statusSample output:
On branch main
Changes not staged for commit:
modified: index.html
Untracked files:
style.cssWhat it does:
Shows modified files
Shows untracked files
Indicates what's staged for commit
Tips:
Run
git statusbefore everygit addandgit commitso 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.htmlAdd 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 |
|---|---|
| Stage several specific files |
| Stage all changes in the current folder and below |
| Stage all changes in the whole repository, including deletions |
| 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.envfiles. Use a.gitignorefile 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.htmlWhat 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 --amendlets 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 logSample output:
commit a1b2c3d4e5f6... (HEAD -> main)
Author: Your Name <you@example.com>
Date: Fri Oct 23 15:30:00 2026 +0530
Initial commitWhat 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--onelineshows one compact line per commit.--graph --decorate --alldraws a text diagram of your branches.-n 5limits 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 branchCreate a new branch:
git branch feature-loginWhat it does:
Lists existing branches (the current one is marked with
*)Creates new branches
Useful variations:
Command | Result |
|---|---|
| List local and remote branches |
| Delete a branch that has been merged |
| 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-loginSample 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-logincreates 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-loginswitches branches.git restore index.htmldiscards 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-loginWhat it does:
Merges another branch into the current branch
Integrates feature work into main or master
What to expect:
Fast-forward merge: if
mainhas 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 rungit addandgit committo finish. To cancel the merge, rungit 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 -vAdd a remote:
git remote add origin https://github.com/username/repo.gitWhat it does:
Links your local repository to remote repositories
Enables pushing and pulling code
Good to know:
originis just the conventional name for your main remote. You can pick another name, but almost everyone usesorigin.Typed the wrong URL? Fix it with
git remote set-url origin <new-url>.git remote remove originremoves 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 mainWhat 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-uflag remembers the remote branch, so next time a plaingit pushis 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 pullfirst, then push again.Avoid
git push --forceon 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 mainWhat it does:
Fetches and merges updates from a remote repository
Keeps your local code up to date
Good to know:
git pullis a shortcut forgit fetch(download the changes) followed bygit merge(combine them with your work).If you want to look at remote changes before merging, run
git fetchon its own.git pull --rebasereplays 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 --graphCreate 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 commitonly saves staged changes. Check withgit status.Writing vague commit messages: "fix" and "update" tell your future self nothing.
Committing secrets: passwords, API keys and
.envfiles 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 --abortis 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...