Skip to main content

Search stories

No stories found. Try another word.

Tech

Git & GitHub CLI Cheat Sheet: Daily Commands from Commit to Pull Request

Muhammad Fikri Gunawan · · 8 min read
Git & GitHub CLI Cheat Sheet: Daily Commands from Commit to Pull Request
Photo: Francesco Tosoni, CC BY-SA 4.0, via Wikimedia Commons

Every Git and GitHub CLI command you need in a normal workday, from the first commit to branches, undoing mistakes and opening a pull request, with the risky ones clearly flagged.

Short answer: the Git commands you use every day are git status to see what changed, git add . to stage it, git commit -m "message" to save it, git pull and git push to sync with GitHub, git switch -c <branch> to start a new branch, and gh pr create to open a pull request from the terminal. Git and the GitHub CLI are standalone programs, so the commands are identical on Linux, macOS, Windows CMD, PowerShell and Git Bash.

This cheat sheet follows the order of a normal working day: set up, commit, branch, sync, undo, and collaborate on GitHub.

How do I set up Git for the first time?

  • git --version: shows the installed Git version.
  • git config --global user.name "Your Name": sets the name used on your commits.
  • git config --global user.email "you@example.com": sets the email used on your commits.
  • git config --list: shows all current Git settings.
  • git init: creates a new repository in the current folder.
  • git clone <url>: copies a repository from a remote.

Which commands save my changes?

  • git status: shows the current state of your files.
  • git add <file>: stages one specific file.
  • git add .: stages all changes.
  • git commit -m "message": saves the staged changes.
  • git commit -am "message": stages tracked files and commits in one step.
  • git diff: shows changes that are not staged yet.
  • git diff --staged: shows changes that are already staged.
  • git rm <file>: deletes a file and records it in Git.
  • git mv <old> <new>: renames or moves a file and records it in Git.

How do I work with branches?

Branches let you build a feature or fix a bug without touching the main line of work. Modern Git prefers switch over checkout.

  • git branch: lists branches.
  • git branch <name>: creates a branch.
  • git switch <branch>: moves to a branch. The older equivalent is git checkout <branch>.
  • git switch -c <new_branch>: creates and moves to a new branch. The older equivalent is git checkout -b <new_branch>.
  • git merge <branch>: merges a branch into the one you are on.
  • git rebase <branch>: replays your commits on top of another branch.
  • git branch -d <branch>: deletes a branch that is already merged.
  • git branch -D <branch>: force-deletes a branch.

How do I sync with GitHub?

  • git remote -v: lists remote repositories.
  • git remote add origin <url>: connects your local repository to a remote.
  • git fetch: downloads updates without merging them.
  • git pull: downloads and merges updates.
  • git push: sends your commits to the remote.
  • git push origin <branch>: sends a specific branch.
  • git push -u origin <branch>: sends a branch and sets it as the default upstream.
  • git push --force: overwrites the remote history. Use it with great care, and never on a shared branch.

How do I undo mistakes in Git?

  • git log: shows commit history. Add --oneline --graph --all for a compact graph.
  • git show <commit>: shows the details of one commit.
  • git blame <file>: shows who changed which line.
  • git stash: temporarily stores uncommitted changes. git stash pop brings them back and git stash list lists them.
  • git reset --soft HEAD~1: undoes the last commit but keeps your changes staged.
  • git reset --hard <commit>: returns the repository to a commit and discards changes.
  • git revert <commit>: creates a new commit that cancels an earlier one, the safe choice on shared branches.
  • git cherry-pick <commit>: applies one commit from another branch.
  • git tag <name>: marks the current commit, for example a release.
  • git submodule add <url>: adds another repository as a submodule.

Rule of thumb: use git revert on code others have pulled, and keep git reset --hard and git push --force for your own private branches.

What can the GitHub CLI do from the terminal?

The GitHub CLI (gh) brings repositories, pull requests and issues into your terminal so you do not have to switch to the browser.

  • gh auth login: signs in to GitHub from the terminal.
  • gh repo create <repo_name>: creates a new GitHub repository.
  • gh repo clone <owner/repo>: clones a GitHub repository.
  • gh repo view --web: opens the current repository in the browser.
  • gh pr create: opens a new pull request.
  • gh pr list: lists pull requests.
  • gh pr checkout <number>: downloads and switches to a pull request branch.
  • gh pr merge <number>: merges a pull request.
  • gh issue create: creates a new issue.
  • gh issue list: lists issues.

How do I connect to GitHub with an SSH key?

  1. Create a key pair: ssh-keygen -t ed25519 -C "you@example.com".
  2. Show the public key: cat ~/.ssh/id_ed25519.pub in Bash, type %USERPROFILE%\.ssh\id_ed25519.pub in CMD, or Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub in PowerShell.
  3. Add the public key in your GitHub account settings.
  4. Test the connection: ssh -T git@github.com.

Only share the public key (the file ending in .pub). The private key stays on your computer.

Frequently asked questions

What is the difference between git fetch and git pull?

git fetch only downloads new commits so you can inspect them. git pull downloads and then merges them into your current branch.

Should I use git switch or git checkout?

Use git switch for changing branches. It is the newer, clearer command, while checkout does several unrelated jobs.

How do I undo my last commit without losing work?

Run git reset --soft HEAD~1. The commit disappears, but your changes stay staged and ready to commit again.

Is git push --force ever okay?

Only on your own branch, when you intentionally rewrote history, for example after a rebase. Never use it on a branch other people work on.

Start of this series: Web developer commands for Node.js, NPM, PHP and WP-CLI, and file and folder commands in Bash, CMD and PowerShell.