DEV · Guide · Git and GitHub

The 30 most important Git and GitHub commands

A practical guide to the 30 git and GitHub CLI (gh) commands to clone, version, branch, sync, review pull requests, use Actions and undo mistakes.

Visualization of GitHub Actions jobs from 2024 to 2026 on the GitHub Universe 2026 stage
Screenshot: GitHub Universe 2026 keynote (© GitHub)

The GitHub Universe 2026 keynote made one thing clear: there are more commits, more pull requests and more repositories than ever because AI agents now work on GitHub (read the summary). All that avalanche of changes needs someone to review it, merge it and, sometimes, undo it. This guide brings together the 30 git and GitHub CLI (gh) commands you will use most to do that, with your own code and with your agents' code.

Each command shows where it is typed (the prompt) and what it is for. The examples use a fictional repository, mi-org/tienda-cafe, and the main branch; replace the names with yours. You need git 2.23 or later (for git switch and git restore) and the GitHub CLI (gh) installed.

Prompt: $ terminal (Bash, Zsh or PowerShell) · git commands work with your repository · gh commands work with GitHub: pull requests, issues and Actions.

Download the PDF guide. The same guide, ready to print or keep handy while you work in your repository.

Download the PDF guide

Setup and getting started

Before the first commit: your identity, GitHub authentication and a repository to work in.

#CommandWhat it's for
1$git config --global user.name "Ana Pérez"
git config --global user.email "ana@ejemplo.com"
Sets who signs your commits. Use the email you verified on GitHub so commits are linked to your profile.
2$git init -b mainCreates a new repository in the current folder with main as the initial branch.
3$git clone https://github.com/mi-org/tienda-cafe.gitCopies a GitHub repository with its full history and sets up the origin remote.
4$gh auth loginAuthenticates the GitHub CLI with your account, through the browser or a token. Afterwards, gh auth setup-git lets git use those credentials.
5$gh repo create tienda-cafe --private --cloneCreates the repository on GitHub, private in this case, and clones it. Use --public to make it public.
$ git config --global user.name "Ana Pérez"
$ git config --global user.email "ana@ejemplo.com"
$ gh auth login
$ gh repo create tienda-cafe --private --clone
$ cd tienda-cafe

The daily work cycle

See what changed, choose what goes in the commit and review the history. It is the loop you repeat dozens of times a day.

#CommandWhat it's for
6$git status -sbShort status summary: the current branch and the modified, new and staged files.
7$git add -pPicks chunks of the change (hunks) for the next commit. You review each part before confirming it, ideal for auditing what an agent generated.
8$git commit -m "feat: agrega pasaporte de café"Saves a snapshot of what is staged. A clear message, for example following Conventional Commits, makes the history easier to read.
9$git diff --stagedShows what will go into the next commit. Without --staged it shows what you have not added yet.
10$git log --oneline --graph --decorate -10The last 10 commits on one line each, with the branch graph and labels.
$ git status -sb
$ git add -p
$ git diff --staged
$ git commit -m "feat: agrega pasaporte de café"
$ git log --oneline --graph --decorate -3

Branches and merges

One branch per task, including one per agent. That way parallel work does not collide.

#CommandWhat it's for
11$git switch -c feature/pasaporteCreates the branch and switches to it in one step.
12$git branch -vv
git branch -d feature/pasaporte
Lists branches with their remote and latest commit. With -d you delete a branch that has already been merged.
13$git merge --no-ff feature/pasaporteMerges the branch into the current one and leaves a merge commit that preserves the branch history.
14$git rebase mainReplays your commits on top of main to update the branch without merge commits. Do not rewrite branches others already use.
15$git stash push -m "wip login"
git stash pop
Saves uncommitted changes so you can switch branches; pop brings them back.
$ git switch -c feature/pasaporte
$ git stash push -m "wip login"
$ git switch main
$ git stash pop
$ git switch feature/pasaporte
$ git rebase main
$ git switch main
$ git merge --no-ff feature/pasaporte
$ git branch -d feature/pasaporte

Remotes and syncing

Connect your local repository to GitHub and keep it up to date without overwriting other people's work.

#CommandWhat it's for
16$git remote -v
git remote add origin https://github.com/mi-org/tienda-cafe.git
Shows the configured remotes; remote add connects your local repository to one on GitHub.
17$git fetch --pruneDownloads what is new from the remote without touching your work and removes references to deleted branches.
18$git pull --rebaseBrings in the remote changes and replays your commits on top, for a linear history.
19$git push -u origin feature/pasaportePublishes the branch and links it to its remote; from then on a plain git push is enough.
20$git push --force-with-leaseForces the push after a rebase, but refuses if someone else updated the branch. Safer than --force.
$ git remote -v
$ git fetch --prune
$ git switch feature/pasaporte
$ git pull --rebase
$ git push -u origin feature/pasaporte
# after a local rebase:
$ git push --force-with-lease

Pull requests and issues with gh

The GitHub CLI brings the collaboration flow to the terminal: fork, open, review and merge.

#CommandWhat it's for
21$gh repo fork mi-org/tienda-cafe --cloneCreates your fork and clones it, with origin pointing to your fork and upstream to the original repository.
22$gh pr create --fill --draftOpens a draft pull request with the title and description taken from your commits.
23$gh pr checkout 42
gh pr diff 42
Checks out the branch of pull request 42 on your machine and shows its changes for review.
24$gh pr merge 42 --squash --delete-branchMerges the pull request by squashing its commits into one and deletes the branch. It respects branch protection rules; with --auto it waits for the checks to pass.
25$gh issue create --title "Bug en el pedido anticipado" --label bugCreates an issue from the terminal. Add --body or --assignee as needed.
$ gh repo fork mi-org/tienda-cafe --clone
$ gh pr create --fill --draft
$ gh pr checkout 42
$ gh pr diff 42
$ gh pr merge 42 --squash --delete-branch
$ gh issue create --title "Bug en el pedido anticipado" --label bug

GitHub Actions and secrets

Trigger and follow your workflows without opening the browser, and keep credentials out of the code.

#CommandWhat it's for
26$gh run list --limit 5
gh run watch
Lists the latest workflow runs; run watch follows one live until it finishes.
27$gh workflow run ci.yml --ref mainManually triggers a workflow that declares the workflow_dispatch event.
28$gh secret set API_KEYStores an encrypted secret in the repository: the CLI asks for the value without echoing it. Never write it in the code.
$ gh workflow run ci.yml --ref main
$ gh run list --limit 5
$ gh run watch
$ gh secret set API_KEY

Undoing mistakes

Choose the tool by where the mistake is: in your folder, in staging or already published.

#CommandWhat it's for
29$git restore --staged src/app.ts
git restore src/app.ts
The first removes the file from staging; the second discards your local changes to it and cannot be recovered.
30$git revert 3f2a9c1Creates a new commit that undoes one already published, without rewriting history. If you get lost, git reflog lists where HEAD has been.
$ git restore --staged src/app.ts
$ git restore src/app.ts
$ git revert 3f2a9c1
$ git reflog

What you take away

  • A small commit with a clear message makes any change, human or agent, reviewable.
  • One branch per task: switch -c, push -u and a draft pull request with gh pr create --draft.
  • Prefer pull --rebase and push --force-with-lease: a linear history that does not overwrite other people's work.
  • gh lets you review, merge and follow Actions workflows without leaving the terminal.
  • Undoing is safe if you choose well: restore for local changes, revert for published ones and reflog to rescue.

Practice in a test repository: create a branch, open a draft pull request and squash-merge it.

Learn with The House of Routing

AI agents speed up development, but you are the one who defines, reviews and decides. At The House of Routing you will find tutorials, courses, masterclasses, learning paths and career plans to build those foundations: Git, HTML, CSS, JavaScript and good practices. Also read the GitHub Universe 2026 keynote summary.

Explore the Software and web development path

Sources

Screenshots from the GitHub Universe 2026 keynote (© GitHub), used for informational and educational purposes. Text by Jesús Espinoza, prepared with AI assistance and reviewed by him.

↑ Start of the article