Git for beginners The definitive practical guide
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git becomes much easier once you stop treating it as a bag of commands and start thinking in terms of three places: your working tree, the staging area, and the repository history. A beginner does not need every advanced feature up front; they need a practical workflow they can trust every day.
The Core Mental Model
Git tracks snapshots of files over time. The usual flow is:
- edit files in the working tree,
- stage the changes you want,
- commit that staged snapshot.
Those three states explain most everyday commands.
Start a Repository or Clone One
To create a new repository:
To copy an existing repository:
Cloning brings down the files, the commit history, and remote-tracking information so you can work locally.
See What Changed
The command beginners should run constantly is:
It answers the most important questions:
- which branch am I on,
- what files changed,
- what is staged,
- and what is still unstaged.
To inspect content changes:
The first shows unstaged changes. The second shows what will go into the next commit.
Stage and Commit
Add files to the staging area:
Then create a commit:
Why stage first? Because it lets you decide exactly what belongs in a commit. Good commits are small, focused, and easy to explain.
Branching Is Cheap and Normal
Branches are lightweight in Git. Use them freely for features or experiments.
That creates and switches to a new branch. When the work is ready, switch back and merge:
This workflow is far safer than editing everything directly on main.
Working With a Remote
Once you have commits locally, send them to a shared remote:
To bring in upstream changes:
In team environments, many developers prefer to fetch first and inspect:
That makes remote changes more visible before merge or rebase steps.
Fixing Common Mistakes
Staged a file by accident:
Discard an unstaged local change:
Amend the most recent commit message or staged content:
These commands are worth learning early because they reduce panic when you make an ordinary mistake.
A Safe Everyday Workflow
For many beginners, this sequence covers most of daily Git use:
Then open a pull request or merge the branch according to your team's process.
Commit Messages Matter
A commit message should explain what changed in a way future readers can understand quickly. Good examples:
- '
Fix null handling in signup validator' - '
Add pagination to orders endpoint' - '
Refactor cache invalidation for clarity'
Bad messages such as stuff, update, or fixes save seconds now and cost time later.
Learn a Few Inspection Commands
These commands pay off quickly:
They help you understand history, branch state, remotes, and the current commit without changing anything.
Common Pitfalls
The biggest pitfall is committing directly to main with unrelated changes mixed together. Small branches and focused commits are safer.
Another mistake is using git add . reflexively without checking git status first. That can stage files you did not intend to commit.
Beginners also often panic at merge conflicts. A conflict is not a repository disaster; it simply means Git needs human help choosing between overlapping edits.
Finally, do not memorize random destructive commands from the internet. Learn the safe inspection and restore commands first so mistakes are recoverable.
Summary
- Think in terms of working tree, staging area, and commit history.
- Use
git statusconstantly; it is the most useful everyday command. - Make small commits with clear messages.
- Use branches for features and experiments instead of editing everything on
main. - Learn safe recovery commands early so ordinary mistakes stay ordinary.
Related reading
- Git for Windows .bashrc or equivalent configuration files for Git Bash shell
- git grep by file extensions
- git hooks is there a clone hook?
- Git, How do I list only local branches?
- Git How do I list only local branches?
- git how to disable push
- git how to move some commits to new branch
- Git How to rebase to a specific commit?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.