Devs! Try out Prismic's new onboarding and get $50 on us! 🎁Apply Now
Developer Workflow
·2 min read

Git Cheat Sheet: 20+ Top Git Commands

Git is a free and open-source version control system that helps us write, collaborate on, and build applications. It’s a major part of the development industry, and it’s become the de facto standard for version control in the industry, superseding previous version control systems like SVN and CVS. So, it’s no surprise that nearly all projects use Git for their version control.

If you want to work on these projects, you need to know and understand how to use Git.

This post covers over twenty of the most common Git commands you’ll use and need to know as a developer. Let’s get into it!

Access the Full Git Commands Cheat Sheet!

Get access to all 20+ essential Git commands below in one printable PDF reference guide - perfect for your desk or workspace

Git Commands: Managing Repositories

Command
Explanation

git init

If you have an existing directory and want to start a new Git repository with it

git remote add <alias> <url>

Allows you to add a remote Git URL as an alias for the repository, often this is used to set the origin alias when first configuring a repository

git remote set-url <alias> <url>

If you need to update the URL assigned to an alias like origin to a new URL

git remote -v

Check the current remote destinations configured for a repository, helpful to confirm where the code is being pushed to when using an alias

git clone <url>

Create a clone of a hosted remote repository on your local machine

git config

Used to configure Git on your local machine; can be done on a repository level or on a global level using the --global flag

Git Commands: Collaborating

Command
Explanation

git fetch <alias>

Fetch all of the remote branches from the specified alias

git merge <branch_name>

Merge a branch and any changes into your current branch

git pull

Perform a git fetch and git merge in one command to fetch any changes and update your current branch to include them. You can also pass the --rebase flag if you’d rather use rebase as a merging strategy instead of a merge commit

git blame

Identifies who made the last change to a given line in a project and in what commit

First time here? Discover what Prismic can do!

👋 Meet Prismic, your solution for creating performant websites! Developers, build with your preferred tech stack and deliver a visual page builder to marketers so they can quickly create on-brand pages independently!

Git Commands: Temporary Commits

Command
Explanation

git stash

Saves both unstaged and staged changes without making a commit. Can also pass the --include-untracked flag to include new files that haven’t been committed before

git stash apply

Apply the contents of the last git stash

Git Commands: Rewriting History

Command
Explanation

git reset --hard <commit>

Clears the staging area and any uncommitted changes, reverting your commit history to match the specified commit SHA, deleting any newer commits

git commit --amend

Add any staged changes to the previous commit. If there are no staged changes, it allows you to edit the last commit’s message

git rebase <base>

Allows you to rebase the current branch onto the base specified (commit SHA, branch name, etc). You can also use the -i flag to make it an interactive rebase allowing you to edit the list of commits before the rebase happens. Learn more about rebasing

Git Commands: Logging

Command
Explanation

git log

Displays the entire commit history of the repository using the default formatting. You can pass various flags to specify how you want to filter, format, or change the presentation of the log. See the entire list of flags

Noteworthy flags

Command
Explanation

--graph

Create a visual graph of the git history

--oneline

Format the log as single lines

--format=%B

See just the commit messages

-n <number>

Limit the number of commits returned in the log

--after / --before

Filter the commits based on their commit date

Helpful Git Tools

All these commands can be a lot to remember which is why developers have invested time in making tools to help simplify Git; making it easier to use as well as integrate more into tooling like IDEs. So, here are four particularly noteworthy tools.

  1. Neogit: Integrate Git into Neovim
  2. GitHub Desktop: Use Git via an easy-to-use GUI
  3. Lazygit: Terminal UI for Git
  4. GitLens: Extension for VS Code to build upon the base functionality built into VS Code

Conclusion

At this point, we’ve covered why Git is important and looked at over twenty of the most helpful Git commands as well as some helpful tools to make Git easier and more efficient to use.

But, if you’d like to take your Git knowledge to the next level why not consider checking out the official Git documentation or the GitHub Git learning resources? Or, take the next step and learn which cloud-based Git platform you should be hosting your code on in 2024.

Thank you for reading.

Article written by

Coner Murphy

Fullstack web developer, freelancer, content creator, and indie hacker. Building SaaS products to profitability and creating content about tech & SaaS.

More posts
Coner Murphy profile picture.

Join the discussion

Hit your website goals

Websites success stories from the Prismic Community

How Arcadia is Telling a Consistent Brand Story

Read Case Study

How Evri Cut their Time to Ship

Read Case Study

How Pallyy Grew Daily Visitors from 500 to 10,000

Read Case Study