my-git

Git Basic Commands

English 中文

Create or clone

git init
git clone <url>

Check status

git status
git log --oneline --decorate

Stage and commit

git add <file>
git commit -m "docs: add git basics"

Sync

git fetch
git pull --rebase
git push

Branch

<integration-branch> is the team’s actual integration branch name. Common values include main, master, and develop. Confirm it from repository rules before switching; do not treat main as a fixed default.

git switch -c feat/my-task
git branch --show-current
git switch <integration-branch>
git branch

Undo

git restore overwrites the Working Tree for the named path. Before running it, confirm that the path belongs to you and that its current version may be discarded:

git status --porcelain
git diff -- <owned-path>
git restore --worktree -- <owned-path>

To unstage the named path while keeping its Working Tree edit:

git restore --staged -- <owned-path>

To make a reverse commit for an already committed change:

git revert <commit-sha>

git restore --staged adjusts only the Index and keeps the Working Tree edit; git revert creates a reverse commit for an already committed change.

  1. Use git status to understand the current state.
  2. Use git add and git commit to complete one local commit.
  3. Use git switch -c to create a task branch.
  4. Use git diff to check changes before committing.
  5. Use git push to push the branch and create a PR.
  6. Use git restore to undo local changes.

Don’t Rush to Use High-Risk Commands

First check for staged, unstaged, or untracked content that must be retained, and only preview untracked files:

git status --porcelain
git diff
git diff --cached
git clean -nd

If git status --porcelain prints anything, stop and do not run git reset --hard or git clean. git clean -nd only lists candidate paths; it does not establish that they may be deleted.

After confirming ownership and authorization to dispose of local edits, verifying the reset target, and confirming remote permission, branch rules, and collaborator impact, the owner can decide whether to use git reset --hard, git clean -fd, or git push --force. They can respectively discard tracked edits, delete untracked paths, and rewrite remote history.

If you just want to undo ordinary workspace changes, prioritize learning Undo Anything.

Extended Reading