- 1 The essential git commands map to six jobs: create, inspect, record, branch, synchronize, and recover.
- 2 The staging area is a review boundary, not an extra chore.
- 3 Fetch is best for inspection; pull fetches and then integrates remote work.
- 4 Reset, restore, clean, and rebase solve different problems and carry different risks.
- 5 Small commits and clean working trees make conflicts easier to understand and reverse.
A misplaced destructive option can erase an afternoon. A clean commit can preserve the reason behind a change. This article explains the git commands used in daily development, what each one changes, and which option is safer when several commands seem to overlap. Readers will learn how to create repositories, stage and commit work, compare changes, manage branches, sync remotes, resolve conflicts, use stashes, and undo mistakes without guessing.
Git is still the shared record behind modern software work. That includes browser-based AI builders and coding agents. Our coverage of AI app-building workflows shows that generated code still moves through files, branches, reviews, and deployments. Git supplies the audit trail. Its command line makes each state change visible. A file moves from the working tree to the index, from the index to a commit, and from a local branch to a remote repository.
Git began on April 7, 2005, and reached its twentieth anniversary in 2025. Creator Linus Torvalds said, “I saw it as a solution to my problems, and I obviously thought it was superior” (Blau, 2025). The official project listed Git 2.54.0 on April 20, 2026 (Git, 2026a). This guide uses stable ideas and favors clear modern forms such as git switch and git restore.
The Mental Model Behind Everyday Git
Git is easier to learn when every command is tied to a state. The working tree is the set of files in the project folder. The index, often called the staging area, is the proposed content of the next commit. The commit graph is saved history. A remote-tracking branch such as origin/main records what Git saw during the last fetch.
git add does not upload a file. It copies selected content into the index. git commit does not save every file in the folder. It creates a commit from the index. git push cannot send edits that have never been committed.
The index is a quality gate. Use git diff to inspect worktree edits. Use git diff –staged to inspect the exact patch prepared for the next commit. That split supports focused commits and safer recovery.
Essential Commands by Workflow
The table compares the core command set by task. The final column states the main purpose or warning.
| Task | Command | Purpose or caution |
| Setup | git init | Create a repository in the current folder. |
| Setup | git clone <url> | Copy an existing repository and its history. |
| Track | git status | Show branch and file state. |
| Track | git add <file> | Stage one file or path. |
| Track | git add . | Stage matching changes below this folder. |
| Track | git commit -m “message” | Commit the staged snapshot. |
| Track | git commit -am “message” | Stage tracked edits, then commit. New files stay untracked. |
| Inspect | git log | Show detailed commit history. |
| Inspect | git log –oneline | Show compact history. |
| Inspect | git diff | Show unstaged changes. |
| Inspect | git diff –staged | Show changes prepared for commit. |
| Branch | git branch | List local branches. |
| Branch | git branch <name> | Create a branch without switching. |
| Branch | git switch -c <name> | Create and switch to a branch. |
| Branch | git checkout -b <name> | Older form of create and switch. |
| Branch | git switch <name> | Switch to an existing branch. |
| Branch | git checkout <name> | Older multi-purpose switch form. |
| Branch | git merge <branch> | Integrate a branch into the current branch. |
| Branch | git branch -d <name> | Delete a merged local branch. |
| Remote | git remote -v | List remote names and addresses. |
| Remote | git remote add origin <url> | Add a remote named origin. |
| Remote | git push origin <branch> | Send commits and update a remote branch. |
| Remote | git pull | Fetch and integrate remote work. |
| Undo | git reset <file> | Unstage a path and keep its edits. |
| Undo | git reset –hard <commit> | Reset branch and tracked files. Destructive. |
| Undo | git restore <file> | Restore a worktree file from the index. |
| Undo | git stash / git stash pop | Store tracked changes, then reapply the latest entry. |
| Undo | git clean -nd / -fd | Preview, then remove untracked files. |
Setup and Repositories
Use git init when local files need a new repository. It creates the hidden .git database. Use git clone <url> when the repository already exists elsewhere. Clone copies history, checks out files, and normally creates an origin remote (Git, n.d.-a).
Tracking and Committing
Run git status before and after major actions. It shows the branch, staged changes, unstaged changes, untracked files, and branch divergence. Use git add <file> for a focused commit. Use git add . only when every relevant change below the current folder belongs in the same commit.
git commit -m “message” records the staged snapshot. git commit -am “message” is narrower than it looks. The -a flag stages changes and deletions only for files Git already tracks. It does not include new files (Git, n.d.-b).
Our desk tested this in a local Git 2.47.3 repository. A tracked edit was committed. A new file stayed untracked. The lesson is simple: a speed flag never replaces git status.
History and Differences
Use git log for full history and git log –oneline for a compact list. Use git diff for unstaged work and git diff –staged for the next commit. The staged diff often catches debug output, generated files, and unrelated edits.
Prompt-to-pull-request tools shorten the time between an idea and a patch. Our V0 production workflow review shows why local inspection still matters. Generated changes need the same tests and review as human-written code.
Branches and Merging
git branch lists local branches. git branch <name> creates one without switching. git switch -c <name> creates and switches in one step. Older guides use git checkout -b <name>. It still works, but switch states the intent more clearly.
Use git switch <name> to move to another branch. Use git merge <branch> while standing on the branch that should receive the work. After integration, git branch -d <name> removes a merged topic branch. The uppercase -D form forces removal, so use it with care.
Remote Repositories
git remote -v shows remote names and addresses. git remote add origin <url> adds a remote named origin. The name is common, not required. git push origin <branch> sends local commits and asks the server to update that branch.
git fetch downloads objects and updates remote-tracking branches. It does not change the current branch. git pull runs fetch and then integrates remote work by fast-forward, rebase, merge, or squash, based on options and settings (Git, n.d.-d). Fetch is safer when incoming commits need review first.
Remote work also creates a security boundary. Credentials, hooks, build scripts, and repository settings can affect what happens after code arrives. Our report on coding-agent sandbox security explains why automated tools need tight file and network limits.
Undo and Cleanup
git reset <file> unstages a path but keeps the worktree edit. git restore –staged <file> states the same intent in modern Git. git restore <file> replaces the worktree file from the index by default. That can erase an uncommitted edit.
git reset –hard <commit> moves the branch and makes tracked files match the target. Official documentation says tracked worktree changes are discarded (Git, n.d.-e). Use it only after checking status and saving anything that matters.
git stash stores selected changes and rolls the worktree toward HEAD. git stash pop applies the newest entry and removes it when the apply succeeds. A normal stash skips untracked files. Use git stash push -u -m “message” when those files matter (Git, n.d.-f). Our local test confirmed this limit.
git clean removes untracked files. Preview with git clean -nd. Remove only after the list is correct, usually with git clean -fd. The dry run changes nothing (Git, n.d.-g). A stash is not a backup. For work that must survive device loss, use a temporary branch and commit.
A Safe Everyday Workflow
This sequence creates a clear checkpoint at each boundary. It also makes recovery easier.
- Synchronize with inspection: Run git fetch, then inspect the remote branch or graph.
- Create a focused branch: Use git switch -c feature/short-name.
- Make one coherent change: Run the relevant tests before staging.
- Review the worktree: Check git status and git diff.
- Stage with intent: Use git add <file> or interactive staging for mixed files.
- Review the commit: Run git diff –staged.
- Commit the outcome: Write a message that explains the result.
- Push for review: Push the topic branch and open a pull request.
- Integrate and clean up: Merge after checks pass, then delete the topic branch.
git fetch origin
git switch -c feature/short-name
git status && git diff
git add <file>
git diff –staged
git commit -m “Describe the outcome”
git push origin feature/short-name
Fetch, Pull, Merge, and Rebase
Fetch Before Pull When Inspection Matters
Fetch updates knowledge of the remote repository without changing the current branch. Pull adds an integration step. This is the main trade-off: pull is faster, while fetch gives the developer a review point.
How to Resolve Merge Conflicts
A merge conflict means Git could not combine overlapping edits. Start from a clean worktree. The official merge guide warns that major uncommitted changes can make an abort hard to rebuild (Git, n.d.-h).
Run git status to list unmerged paths. Edit each file, understand both sides, and remove the conflict markers. Run tests. Stage the fixed files. Then continue or commit the merge. Use git merge –abort when the integration should stop.
A file can be free of markers and still be wrong. Tests, type checks, and a final staged diff are part of the fix.
When Rebase Helps
Rebase replays commits on a new base. It can update a private topic branch and create a linear history. It also creates new commit IDs because each parent changes (Git, n.d.-i).
Use rebase for local, unpublished work when the team prefers a straight history. Avoid rebasing shared commits unless everyone has a clear plan. A merge often keeps context with less surprise.
A common pattern is git fetch origin, then git rebase origin/main. Resolve one conflict at a time and run git rebase –continue. Use git rebase –abort to stop. After rebasing a previously pushed private branch, prefer git push –force-with-lease over a plain force push.
Risks and Trade-Offs
The worst Git mistakes are often category errors. A staging problem gets a history command. A preview problem gets a delete command.
Three limits deserve attention. First, commit -am skips new files. Second, a normal stash skips untracked files. Third, pull combines download and integration. One command can change both remote-tracking state and the current branch.
Security adds another cost. GitHub reported 39 million secret leaks in 2024. A commit can preserve a credential after the working file is deleted (Havens, 2025). Repository automation can also turn trusted settings into an execution path. Our coverage of repository-level supply-chain risks shows why new configuration files need the same review as code.
The safer pattern is clear: inspect before integration, preview before deletion, scan before push, and prefer reversible actions while the goal is still uncertain.
Git at Software Scale
Current platform data shows why basic version-control discipline now supports large, automated systems.
| Indicator | Official figure | Period | Practical meaning | Source |
| Git source release | 2.54.0 | April 20, 2026 | Stable command concepts still gain new fixes and options. | Git, 2026a |
| Commits on GitHub | Nearly 1 billion | 2025 | High change volume makes small commits and review controls vital. | GitHub, 2025 |
| New repositories | More than 230 per minute | 2025 | Repository creation is highly automated and distributed. | GitHub, 2025 |
| Merged pull requests | 43.2 million per month | 2025 average | Branch and integration habits operate at large scale. | GitHub, 2025 |
| Detected secret leaks | 39 million | 2024 | Scanning and credential hygiene must sit beside Git basics. | Havens, 2025 |
The Future of Git Workflows in 2027
By 2027, more patches will come from coding agents, dependency bots, and automated repair systems. GitHub’s 2025 Octoverse report recorded nearly one billion commits, more than 230 new repositories per minute, and an average of 43.2 million merged pull requests each month (GitHub, 2025). That scale will drive more policy checks, provenance records, and automatic review.
The command line is unlikely to vanish. More commands may run through an IDE or agent, but developers will still need to inspect the result. Status, diff, commit, fetch, merge, and restore expose the state beneath the interface.
Some choices will remain unsettled. Teams differ on merge versus rebase, signed commits, AI access, and history rules. The durable skill is knowing what a command reads, what it changes, whether it rewrites history, and how to verify the outcome.
Takeaways
- Status and diff are the best safety checks because they expose state before a change.
- The index helps create smaller, clearer commits and should be reviewed before every commit.
- Fetch separates observation from integration, while pull combines both actions.
- Restore, reset, stash, and clean are not interchangeable recovery tools.
- Rebase is useful for private history but risky when other people depend on existing commits.
- Secret scanning and branch rules now belong beside basic version-control habits.
Conclusion
Git rewards teams that slow down at the right boundaries. A safe workflow checks status, reviews a diff, stages a clear patch, reviews the staged result, and commits one outcome. Branches isolate work. Fetch separates observation from integration. Restore, reset, stash, and clean are useful only when the user knows which layer each command changes.
Convenience flags carry assumptions. commit -am assumes every needed file is already tracked. A normal stash assumes untracked files can stay behind. Pull assumes the chosen integration method is acceptable. Cleanup assumes the preview was checked.
As agents and specialized tools create more code, version control becomes more important as an evidence system. Our review of reproducible data science toolchains reaches the same broad point: automation works best when it keeps work inspectable, portable, and reviewable. Git can do that, but only when commands are used with clear intent.
Frequently Asked Questions
Which Git commands should a beginner learn first?
Start with git status, git add, git diff, git diff –staged, git commit, git log –oneline, git switch, git fetch, git pull, and git push. Add restore, stash, and merge after the three-state model is clear. Status and diff matter most because they show what the next action will affect.
How do you undo a commit without losing changes?
For an unpushed local commit, git reset –soft HEAD~1 moves the branch back while keeping changes staged. git reset HEAD~1 keeps them in the worktree but unstages them. For a shared commit, git revert <commit> is usually safer. It creates a new reverse commit instead of rewriting published history.
What is the difference between git fetch and git pull?
Fetch downloads new objects and updates remote-tracking branches. It does not change the current branch. Pull runs fetch and then integrates remote work. Use fetch when you want to inspect first. Use pull when you understand the configured fast-forward, merge, rebase, or squash behavior.
What are the best practices for git stash?
Give each stash a message. Inspect it with git stash show -p. Use git stash push -u when untracked files matter. Prefer apply when the entry should remain until the result is checked. Do not treat stashes as long-term storage. A temporary branch and commit are easier to find, share, and back up.
How do you resolve merge conflicts safely?
Run git status, edit each unmerged file, and understand both sides before removing markers. Run tests and inspect the final diff. Stage the fixed files and continue the merge. Use git merge –abort when the integration should stop. A clean worktree lowers the risk of mixing unrelated edits into the fix.
What is the purpose of git rebase?
Rebase replays commits on a different base. It can update a private topic branch and create a linear history. It rewrites commit IDs, so it is best for work that has not become shared history. Merge is safer when the original branch link matters or other people already use the commits.
Should developers use git switch or git checkout?
Use git switch for branches and git restore for files when the installed version supports them. Their narrow roles make intent clear. Checkout remains valid and is common in scripts and old guides. Teams should favor consistency and test any change to established automation.
References
Blau, T. (2025, April 7). Git turns 20: A Q&A with Linus Torvalds. GitHub.
Git. (2026a, April 20). Install Git on Windows: Version 2.54.0.
Git. (n.d.-a). Git user manual: Cloning a repository.
Git. (n.d.-b). git-commit documentation.
Git. (n.d.-c). git-switch documentation.
Git. (n.d.-d). git-pull documentation.
Git. (n.d.-e). git-reset documentation.
Git. (n.d.-f). git-stash documentation.
Git. (n.d.-g). git-clean documentation.
Git. (n.d.-h). git-merge documentation.
Git. (n.d.-i). git-rebase documentation.
Havens, E. (2025, April 1). GitHub found 39M secret leaks in 2024. GitHub.
Methodology
Our desk reviewed the official Git reference and current documentation for commit, switch, pull, reset, stash, clean, merge, and rebase. We used GitHub’s official anniversary interview, Octoverse report, and security reporting to validate dates, scale, and security context. The five internal links were checked as live publication pages and used only where they add useful context.
We also ran a small local repository test with Git 2.47.3. It checked commit -am, default stash behavior, stash pop, restore, status, staged diff, and git clean -nd. This test was narrow. It does not replace checks on Windows, macOS, hosted CI, or enterprise servers.
Settings can change behavior. Pull strategy, default branches, hooks, aliases, line endings, and server policy may alter results. Readers should check local configuration and current help before destructive actions. Merge and rebase choices depend on team policy.