Top 15 Git Commands Every Developer Needs to Know

Michal Staszkiewicz
3 min readSep 6, 2024

--

Introduction

Git is the most popular and widely used version control system. Understanding how Git works and how to use it is a fundamental skill that every developer should possess. That being said, we’ll delve into the 15 most important Git commands.

Table of contents

  1. Basic Commands
  2. Remote Repositories
  3. Branching and Merging
  4. Undoing Changes

1. Basic Commands

  1. git config — allows you to get or set repository or global configuration options
  • git config –global user.name “name”
  • git config –global user.email “email address”

2. git init — initializes a new Git repository.

  • git init “repository name”`

3. git clone —creates a local copy of a remote repository

  • git clone [repository url] —clone a repository from a repository url

4. git add — adds files to the staging area

  • git add [file] — adds file with a specific name to the staging phase
  • git add * — stages every file that has been changed

5. git commit — records changes to the repository.

  • git commit -m “message of the commit” — records a commit with a message

2. Remote Repositories

  1. git remote add origin —adds the local repository to a remote repository, named origin.
  • git remote add origin [remote repository url> -adds the local repository to a remote repository located at the specified [remote repository URL], and names it origin.

2. git push — uploads local commits to a remote repository.

  • git push origin master (pushes to the "master" branch on the remote named "origin")

3. git pull — downloads the latest changes from the remote repository and merges them into your local repository.

  • git pull origin master (pulls from the "master" branch on the remote named "origin")

4. git fetch — downloads the latest changes from the remote repository without merging them into your local branch.

  • git fetch origin

3. Branching and Merging

  1. git branch — allows us to create, list, switch between, or delete branches.
  • git branch [branch] — creates a branch with the given name
  • git branch -d [branch]` — delete branch with the given name

2. git merge — combines changes from one branch into another branch

  • git merge [branch] — merges the given branch into a branch that we are currently in

3. git checkout — allows us to switch between branches

  • git checkout branch-name — switches from the current branch to the branch with the given name

4. git-rebase —reapplies commits on top of another base tip.

  • git rebase master

4. Undoing Changes

  1. git status — shows the current status of your working directory and staging area.
  • git status

2. git stash — temporarily saves your uncommitted changes away, allowing you to switch to a different branch or work on something else without losing your progress.

  • git stash — saves your uncommitted changes to a temporary stack
  • git stash pop — applies the most recent stashed changes and removes them from the stack

And that’s it. Thanks for reading, hope you learned something new, leave a clap if you enjoyed it.

Happy Coding❤️

--

--

Michal Staszkiewicz

I am a self-taught web developer | Passionate about technology.