Git for Beginners: The Commands You Actually Need Every Day

Git is the version control system used by virtually every software project in the world. The git command line has over 100 subcommands, but the daily workflow requires fewer than ten. This is what you need to know.

The Core Mental Model

Git tracks changes to files over time. A repository (repo) is a directory with git tracking enabled. A commit is a snapshot of your files at a specific point, with a message explaining what changed and why. A branch is a separate line of development that diverges from the main line. The remote is a copy of the repository hosted online (GitHub, GitLab, Bitbucket).

Daily Commands

git status — shows which files have changed. Run this constantly; it is cheap and informative. git add filename — stages a file (marks it as ready to include in the next commit). git add -p — stages changes interactively, hunk by hunk. git commit -m “message” — commits staged changes with a message. git push — sends your commits to the remote. git pull — brings remote changes into your local branch. git log –oneline — shows recent commits in a compact format.

Branches

git checkout -b feature-name — creates and switches to a new branch. git checkout main — returns to the main branch. git merge feature-name — merges a branch into the current branch. git branch -d feature-name — deletes a branch after it has been merged.

When Things Go Wrong

git restore filename — discards uncommitted changes to a file. git reset HEAD~1 — un-commits the last commit (keeps the changes). git stash — temporarily stores uncommitted changes so you can switch branches. git stash pop — restores stashed changes. Don’t use git reset –hard or git push –force without understanding what they do — they can cause data loss.

GitHub Workflow

The standard team workflow: create a feature branch, commit your changes, push to GitHub, open a Pull Request, have someone review, merge into main. This workflow protects main from accidental changes and provides a review record for all code.

上一篇 德国报税:如何申报以及可以扣除什么
下一篇 Git入门:你每天真正需要的命令