Essential Git Commands That You Must Know

Sep 7, 2022 - 7 minute read | Logan Murphy


Git is a version control system created by Linus Torvalds in 2005. He made Git to develop the Linux kernel. Other programmers contributed to its initial development and its core maintainer is now Junio Hamano. Git is available for free under the GPL-2.0-only license.

Torvalds joked about the name Git which means “unpleasant person” in British English slang: “I’m an egotistical person, and I name all my projects after myself. First ‘Linux’, now ‘Git’“.


  • scm - Source Control Manager
  • vc - Version Control
  • working tree - current content of the repository
  • stage - changes prepared to be committed
  • commit - staged changes that have been recorded in change history


Windows - Chocolatey

choco install git

Mac - Homebrew

brew install git


sudo apt-get install git


If none of these methods work for you check out the Git website for installation steps.

When you are happy with your changes

You want to be able to save your code once you get everything working.


Allows you to stage changes in your working tree.

# Stage all file changes
git add --all
git add -A
git add .
# Add specific files
git add *.html
git add index.html
git add output.log -f # for ignored files
# Stage files chunk by chunk
git add -p


Formally records your staged changes in history with the specified message.

# Commit staged files
git commit -m "a message describing the change"
# Stage and commit changes - only works for tracked files
git commit -a -m "a message describing the change"
# If you did not like your last message
git commit --amend -m "New commit message"

When you are unhappy with your changes

You want to be able to go back to a better state.


Un-stages your files. It can also permanently undo any uncommitted changes as well as delete entire commits. Be careful using this command as you can permanently lose your changes if used improperly.

# Unstage changes
git reset
git reset path/file.ext
# Undo all uncommitted changes
git reset --hard
# Advanced - Pop a commit - You should use revert instead to maintain history
git reset HEAD~
git reset HEAD~2 # Pops the last 2 commits


Removes untracked files from the working tree

# Remove all untracked files and folders
git clean -fd


Allows you to save changes for another time. You can restore these changes at a later time.

# Add files to a stash
git stash
# Apply the latest stash
git stash pop
# Show stashes
git stash list


Creates a commit in history that undoes the changes of another commit.

# Undo a commit
git revert [commit-sha]


Copies a file from another revision. This is a newer command that replaced the previously overloaded checkout command.

git restore --source [branch-name] [path/file.ext]
git checkout [branch-name] -- [path/file.ext]

When you want to know the state of your repository

Use these commands to give you some insight.


Lists all commits on the current branch, including who, when, and what they did.

# List all logs
git log
# List the last 10 logs
git log -10


Lists the names of changed, new, and deleted files in the working tree.

# List all untracked, unstaged, and staged file changes
git status


Lists the specific changes of tracked files in the working tree.

# List all tracked but unstaged changes
git diff

When you want to start a new feature

You do not want to affect the quality of existing code or progress of new code.


Creates, deletes, or lists a new line of history for making commits.

# List all branches available
git branch
# Create a new branch
git branch [branch-name]


Replaces the use of the overloaded checkout command when switching to a new branch.

# Switch to a new branch
git switch [branch-name]
git checkout [branch-name]
# Create and switch to a new branch
git switch -c [branch-name]
git checkout -B [branch-name]

When you have completed a new feature

You need to combine your changes with existing features.


Brings your changes into another branch as a single commit.

# Merge another branch into the current branch
git merge [branch-name]


Brings your changes into another branch using the original commit changes. This command better preserves history.

# Rebase another branch on top of the current branch
git rebase [branch-name]


Let’s you pick each commit you want to pull from another branch. You should use this command rarely.

# Add the specified commit by sha
git cherry-pick [commit-sha]

When you want to sync with a remote repository

You can update both your local and remote repositories.


Gets and applies the latest code on the server.

# Get the latest code from the server and apply them
git pull [remote-name] [branch-name]


Updates the latest code on the server. It may require a pull first.

# Add the latest code to the server
git push [remote-name] [branch-name]
# First time pushing a new branch
git push -u origin main
# Advanced - Overwrite the history of a remote branch - This should almost never be done
git push -f origin main


Caches the latest code on the server. The changes can then be applied later.

# Fetch the latest code from the server
git fetch [remote-name] [branch-name]

When you want to start a new repository

You have a few options for configuring your set-up.


Creates a bare-bones repository that you can immediately start committing to.

# Start a new repository
git init


Creates a repository from a remote server.

# Copy the contents of a remote repository
git clone [clone-url]


Specifies where remote resources should be stored.

# Set the push url
git remote add [remote-name] [remote-url]


This is actually a file, not a command. It is very important in maintaining the size of your repository. This file would list out files and wildcards in order to prevent generated files (like logs and shared packages) from being accidentally committed.

When you get a new computer

You need to do a few things before getting started.


# Set your global author information
git config --global "Logan Murphy"
git config --global ""
# Set your repository author information
git config "Logan Murphy"
git config ""

When you are stuck

The entire Pro Git book, written by Scott Chacon and Ben Straub and published by Apress, is available here. All content is licensed under the Creative Commons Attribution Non Commercial Share Alike 3.0 license.


Can list all of the Git documentation.

# List general help
git help
# List help on a specific command
git help [command-name]

There is much more to Git than these commands, but if you master these you will have not trouble navigating the version control space with Git. Alternatively, you can consider hiring a Git expert like myself using a freelancing hiring platform like Toptal. © 2024
Logan Murphy