Git Cheatsheet

Published: (March 3, 2026 at 06:07 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

This cheatsheet lists the Git commands commonly used to submit a PR (pull request) to a GitHub repository. It’s mainly for reference.

Branch Management

git checkout -b    # Create and switch to a new branch
git checkout -                  # Switch back to the previous branch

Checking Status

git status   # Show changed files
git diff     # Show code differences in the terminal

Staging and Committing

git add .                     # Stage all files for commit
git commit -m "commit message" # Commit with a message
git commit                    # Open editor (nano or vi) to enter the commit message

Editor shortcuts

  • Nano: type the message, Ctrl+X to exit, then y to save.
  • Vi: press i for insert mode, type the message, Esc, then :wq to save and exit (or :q to quit without saving).

Pushing

git push --set-upstream origin   # Push a new branch and set upstream
git push                                        # Subsequent pushes (branch name not needed)

Reverting Commits (before they are pushed)

Soft reset – keep changes, revert the commit

git log                                 # Find the previous commit hash
git reset --soft 
git reset --soft HEAD^1

Hard reset – discard changes and the commit

git reset --hard 
git reset --hard HEAD^1

Reverting Commits (after they are pushed)

git revert    # Create a revert commit locally
git push                    # Apply the revert on the remote repository

Bisect

git bisect   # Binary search to identify which commit introduced a bug

Resetting Local Branch to Match Remote

git fetch origin                                 # Fetch latest changes from remote
git reset --hard origin/            # Reset local branch to remote state
git clean -f -d                                  # Delete untracked files and directories
git pull                                         # Pull all contents from the repository
0 views
Back to Blog

Related posts

Read more »

Good software knows when to stop

The “New” ls Experience It’s 9 AM, you’re ready to upgrade your favorite Linux distribution and packages to their latest versions. The process goes smoothly, a...