For developers
Git basics for beginners: commits, branches and the commands you need
Git tracks every change to your code so you can experiment without fear. Learn the core ideas, the ten commands you will use daily, and how to undo mistakes.

Git is a version control system: it records snapshots of your project over time so you can see what changed, go back to an earlier version, and work on several ideas at once without breaking the main code. Learning Git basics is one of the first practical skills every developer needs, whether you work alone or in a team.
This beginner guide explains the core ideas (repository, commit, branch, merge, remote), walks through the commands you will use every day, and shows how to undo the most common mistakes safely.
What problem does Git solve?
Without version control, people end up with folders full of files called 'final', 'final-2' and 'final-really'. It is hard to know what changed between them, and harder still for two people to work on the same files. Git replaces that with a complete history of the project, stored alongside your files in a hidden folder.
Every time you save a meaningful step, called a commit, Git records exactly which lines changed, who changed them, when, and why (in the commit message). You can compare any two points in history, restore a deleted file, or find the change that introduced a bug.
Git is free, open-source software originally written to manage the Linux kernel. It is distributed, which means every copy of a repository holds the full history. You can work offline and sync with others later.
What are the core ideas you need to know?
A handful of terms cover most of what you will do. Once these click, the commands make sense.
- Repository (repo): a project folder tracked by Git, including its full history.
- Working directory: the files as they are on your disk right now.
- Staging area: a holding area where you choose which changes go into the next commit.
- Commit: a saved snapshot with a message and a unique ID (a hash).
- Branch: a movable pointer to a line of commits, so you can work on something separately.
- Merge: combining the changes from one branch into another.
- Remote: a copy of the repository on another machine or hosting service, often called origin.
A history of focused commits is easy to read and easy to undo.
Which Git commands will you use every day?
You can do most daily work with about ten commands. Run them in a terminal inside your project folder.
A typical first session looks like this: git init to start tracking a folder, git status to see what has changed, git add followed by a file name to stage it, then git commit -m with a short message to save the snapshot. Run git log to see the history.
- git init: start a new repository in the current folder.
- git clone <url>: copy an existing repository to your machine.
- git status: show changed, staged and untracked files.
- git add <file>: stage a change for the next commit.
- git commit -m "message": save staged changes as a commit.
- git log: list previous commits.
- git diff: show line-by-line changes not yet staged.
- git switch -c <name>: create and move to a new branch.
- git merge <name>: bring another branch's commits into the current one.
- git pull and git push: fetch changes from, and send changes to, a remote.
How do branches work in practice?
A branch lets you try something without touching the main line of work. Say your main branch is working and you want to add a contact form. You create a branch called contact-form, make your commits there, and the main branch stays exactly as it was. If the idea fails, you delete the branch. If it works, you merge it.
Branches in Git are cheap: creating one takes a fraction of a second and almost no space, because a branch is just a pointer to a commit. Teams usually create a short-lived branch for every feature or bug fix, then merge it after review. Keeping branches small and short-lived makes merging much easier.
When two branches change the same lines differently, Git cannot decide which to keep and reports a merge conflict. It marks the conflicting section in the file with special markers. You edit the file to keep the right version, remove the markers, then add and commit the result.
How do you undo mistakes in Git?
Git is forgiving, as long as you have committed your work. Different mistakes need different tools, and it helps to know which ones change shared history and which do not.
- Discard changes to a file you have not staged: git restore <file>.
- Unstage a file you added by mistake: git restore --staged <file>.
- Fix the message or add a forgotten file to your last commit (before pushing): git commit --amend.
- Undo a commit that is already shared: git revert <commit>, which adds a new commit that reverses it.
- Find a commit you thought was lost: git reflog shows where your branches have pointed recently.
What habits make Git easier?
Commit small, related changes often, with messages that explain why you made them. A history of focused commits is easy to read and easy to undo. Huge commits that mix several changes are hard to review and painful to roll back.
Add a .gitignore file early, listing files Git should not track, such as build output, dependency folders, editor settings and anything with passwords or API keys. Secrets committed to a repository stay in its history even after you delete the file, so it is much easier never to add them. Before every commit, run git status and git diff to check exactly what you are about to save.
Frequently asked questions
What is the difference between Git and a code hosting site?
+
Git is the version control tool that runs on your computer. Code hosting services store Git repositories online and add features such as pull requests, issue tracking and permissions. You can use Git without any hosting service.
What is the difference between git pull and git fetch?
+
git fetch downloads new commits from the remote but does not change your files. git pull does a fetch and then merges (or rebases) those commits into your current branch.
What is the difference between merge and rebase?
+
Both bring changes from one branch into another. Merge keeps the history as it happened and adds a merge commit. Rebase rewrites your commits so they appear on top of the other branch, giving a straight line of history. Avoid rebasing commits others have already pulled.
Should I use main or master as my default branch name?
+
Either works technically. Many projects and tools now default to main. Follow whatever your team or project already uses.
How long does it take to learn Git?
+
You can learn the everyday commands in an afternoon of practice. Comfort with branching, conflicts and undoing mistakes usually comes after a few weeks of regular use on real projects.
Is there a graphical alternative to the Git command line?
+
Yes. Many code editors include Git panels, and there are standalone graphical Git clients. Learning the basic commands is still worthwhile, because it helps you understand what the buttons do.
Start a repository for your next small project, even a single script, and commit as you go. After a week of git status, add, commit and a branch or two, the basics become second nature.
How we research: Guides are written from established sources and checked for accuracy. They are general information, not professional advice.
Written by
Cursorlane Editorial Team
Software & tools
Our writers are developers and heavy software users who test what they write about and update guides when the tools change.


