Git Basics: 10 Commands Every Beginner Should Know
Programming

Git Basics: 10 Commands Every Beginner Should Know

Git saves every version of your code and makes undoing mistakes easy. These ten commands cover nearly everything you do on a normal day.

Table of contents
  1. 1. git init
  2. 2. git clone
  3. 3. git status
  4. 4. git add
  5. 5. git commit
  6. 6. git log
  7. 7. git branch
  8. 8. git switch
  9. 9. git pull
  10. 10. git push
  11. A normal working day

Git keeps a history of every change in your project. If you break something, you can go back to the last working version in seconds. It is also how teams share code on GitHub and GitLab. You don't need to learn all of Git to start, and ten commands are enough for most days.

Install Git from git-scm.com, then set your name and email once:

bash
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

1. git init

Starts tracking the current folder. Git creates a hidden .git folder to store the history.

bash
git init

2. git clone

Downloads an existing project along with its full history.

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

3. git status

Shows which files are new, changed or ready to be saved. Run it often.

bash
git status

4. git add

Picks the changes that go into the next save. git add . picks every change in the folder.

bash
git add index.php
git add .

5. git commit

Saves a snapshot with a short message about what changed.

bash
git commit -m "Add contact form"

6. git log

Shows the history, one line per commit.

bash
git log --oneline

7. git branch

Creates a separate line of work, so you can try something without touching the main code.

bash
git branch new-design

8. git switch

Moves between branches. Add -c to create a branch and switch to it in one go.

bash
git switch new-design
git switch -c another-idea

9. git pull

Brings in the latest changes from the remote repository.

bash
git pull

10. git push

Uploads your commits.

bash
git push origin main

A normal working day

  1. git pull to get the latest code
  2. Make your changes
  3. git status, then git add .
  4. git commit -m "What you changed"
  5. git push

Set up a .gitignore file early. Files listed there never go into Git, which is where .env files with passwords, log files and big build folders belong.

Once these feel normal, look at git merge, git stash and pull requests on GitHub.

Written by

Bharath

About Sakkath →