GitHub GitGitHub

Article & Tutorial

Essential Git and GitHub Commands: Practical Cheat Sheet with Examples

A practical Git and GitHub command guide covering repositories, branches, commits, remotes, pull, push, undo, recovery, tags, stashes and troubleshooting.

Git and GitHub command workflow with repositories, branches, commits and remotes
Git and GitHub command workflow with repositories, branches, commits and remotes
Contents

Essential Git and GitHub commands

Git is a distributed version-control system. GitHub hosts Git repositories and adds collaboration features such as pull requests, reviews and automation. This practical command reference uses modern commands and calls out destructive operations.

Configure and initialize Git

git-setup.sh
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --list

git init
git clone <repository-url>

Inspect changes

git-inspect.sh
git status
git diff
git diff --staged

git log --oneline --graph --decorate --all
git log --stat
git show <commit-id>

Stage and commit

git-commit.sh
git add <file>
git add .
git commit -m "Describe what changed"
git commit -a -m "Commit tracked changes"
git commit --amend

Review staged changes before committing. Amend only when you understand whether the commit has already been shared.

Create and switch branches

git-branches.sh
git switch -c <branch>
git switch <branch>
git branch
git branch -a

git merge <branch>

The older checkout command can also create or switch branches, but git switch communicates the intent more clearly.

Work with remotes

git-remotes.sh
git remote -v
git fetch origin
git pull
git pull --rebase
git push -u origin <branch>
git push origin <branch>

Stash unfinished work

Use Git's temporary work-saving feature to set aside uncommitted changes, list saved work and restore it when the working tree is ready.

Undo and recover changes safely

Choose the recovery command based on whether changes are committed, shared or still only in the working tree.

Use the restore command for selected working-tree changes, revert for a safe inverse commit on shared history, and reflog to locate earlier commit positions.

  • git restore discards or unstages selected working changes.
  • git revert creates a new commit that reverses an earlier commit and is appropriate for shared history.

History rewriting and selected commits

Use interactive rebase only on private history, and use cherry-pick when a reviewed commit must be applied to the current branch.

Interactive rebase rewrites commit history. Do not rebase commits that teammates already depend on unless the team has coordinated the change.

Delete branches

Delete merged local branches normally. Force deletion or remote deletion should be performed only after confirming that no required work will be lost.

Use the lowercase -d option first because it protects unmerged work. The uppercase -D option forces deletion.

Create and apply patches

Validate a patch before applying it, then review the resulting working-tree changes before committing.

Tags

Create annotated version tags for releases, publish the selected tag to the remote repository, and list existing tags before choosing a new version.

Common troubleshooting

  • Push rejected: fetch the remote branch, review changes, then merge or rebase before pushing.
  • Merge conflict: edit conflicted files, stage the resolutions and continue the merge or rebase.
  • Detached HEAD: create a branch at the current commit before doing more work.
  • Accidental commit on the wrong branch: create the correct branch at the commit, then repair the original branch carefully.
  • Large or generated files committed: remove them from tracking and add the correct pattern to .gitignore.

Recommended daily workflow

  1. Create a focused feature branch.
  2. Review the working tree and stage only intended files.
  3. Inspect the staged difference.
  4. Commit with a clear message.
  5. Synchronize with the base branch.
  6. Publish the feature branch and open a review.

Official references

Git documentation

GitHub: About Git

Did this help?

Yes No

Join the discussion

0 Comments

Ask questions, add context, or share what worked.

Sign in to join the discussion

No comments yet. Start the discussion.

More from Melochip Editorial

Related articles

Stay in the loop. Get new Melochip articles

Receive Melochip article updates when new articles are available. Explicit opt-in only.

No pre-checked boxes. Unsubscribe support is part of the subscriber foundation.