Programming

12 Git commands every beginner should know

The Git commands you will actually use every day, explained in plain English with examples, plus how to fix the most common beginner mistakes.

Git has well over a hundred commands, but day-to-day work uses about a dozen. Learn these well and you can work confidently on your own projects and in a team. Every command below is something you can try in a folder on your computer in a few minutes.

Setting up

1. git config

Tell Git who you are. This name and email appear on every commit you make.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

2. git init and git clone

git init turns the current folder into a new repository. git clone downloads an existing repository, with its full history, from a site such as GitHub.

git init
git clone https://github.com/user/project.git

The everyday loop

3. git status

Shows which files have changed, which are staged for the next commit and which Git is not tracking. Run it often; it is the quickest way to understand where you are.

4. git add

Stages changes, which means choosing what goes into the next commit. git add file.txt stages one file; git add . stages everything in the current folder.

5. git commit

Saves a snapshot of the staged changes with a message. Write messages that say what changed and why, such as “Fix crash when quiz has no questions”.

git commit -m "Add dark mode toggle"

6. git log

Lists past commits. git log --oneline gives a compact one-line-per-commit view that is easier to scan.

7. git diff

Shows exactly what changed, line by line. On its own it shows unstaged changes; git diff --staged shows what you are about to commit.

Branches

8. git branch and git switch

A branch lets you work on a feature without touching the main code. Create one and move to it in a single step:

git switch -c feature/login
git branch            # list branches
git switch main       # go back to main

Older tutorials use git checkout for this. It still works, but git switch is clearer.

9. git merge

Brings the changes from one branch into the current branch. To merge a finished feature, switch to main and run git merge feature/login. If both branches changed the same lines, Git asks you to resolve a conflict by editing the file and committing the result.

Working with a remote

10. git pull

Downloads new commits from the remote repository and merges them into your branch. Pull before you start work so you build on the latest version.

11. git push

Uploads your commits to the remote. The first time you push a new branch, use git push -u origin feature/login so Git remembers where it belongs.

Undoing mistakes

12. git restore and git commit --amend

Everyone makes mistakes; Git makes most of them easy to undo.

  • git restore file.txt throws away unstaged changes to a file. Be careful: those changes are gone.
  • git restore --staged file.txt unstages a file but keeps your changes.
  • git commit --amend lets you fix the message of your last commit or add a forgotten file to it. Only amend commits you have not pushed yet.

A good habit to start with

Commit small and often. A commit that does one thing is easy to understand, easy to review and easy to undo. Combine that with a quick git status before every commit and you will avoid most beginner problems.

Keep learning

Once these commands feel natural, explore git stash, git rebase and pull requests. Our Learn Git: Full Masterclass app teaches all of this with animations that show what each command does to your history, plus quizzes and interview practice. When you are ready to package and ship your projects, Learn Docker is a natural next step.