Git is a distributed version-control system for tracking changes to a project. It helps developers understand what changed, preserve meaningful snapshots and collaborate on branches. The most important beginner skill is distinguishing files being edited from files staged for a commit, and local history from changes shared with another repository.
The ten commands below form a useful daily workflow. Each command has a clear purpose, common mistake and safe next action. They are not the entire Git command set. In particular, merging, restoring and undoing commits deserve care beyond an introductory list, because some options can overwrite work.
1. git init — create a local repository
Run git init inside a directory that you want to start tracking. Git creates repository metadata in a .git directory, but it does not automatically stage, commit or upload the files already present. The default initial branch name depends on Git configuration and version.
After initialization, run git status to see what is untracked. Look for local credentials, downloaded files, generated output or confidential data before recording a snapshot. Version control is not a license to publish everything in a folder.
An appropriate .gitignore file can exclude selected untracked paths from routine adds. Once a secret has entered Git history, adding the path to .gitignore does not make the exposure disappear; rotate the secret and follow an approved remediation process.
2. git clone — copy an existing repository
Use git clone https://example.com/team/project.git with the actual URL of a repository you are authorized to access. The command normally downloads Git objects and refs, creates a working directory and configures the source remote as origin. Options such as shallow cloning can limit available history, so a clone is not a guarantee that every earlier commit is present.
Check the URL independently before cloning an unfamiliar project. Downloaded repositories can contain malicious scripts, binaries or instructions, and cloning does not make those files safe to execute. After the clone, examine git status and git remote -v before building or running code.
Usually choose git init for an existing local project that is not yet versioned, and git clone for an already hosted or shared Git project. Running both without understanding their roles creates confusion.
3. git status — understand your local state
git status identifies the checked-out branch, staged changes, unstaged tracked-file changes and untracked files. The shorter git status --short representation is useful once you understand its symbols. Running status before any staging, commit or branch change is a low-risk habit.
A clean working tree means there are no locally outstanding changes under Git’s current rules. It does not mean the program is correct or the remote has no newer commits. Ignored files may also exist without appearing in the ordinary status listing.
When status shows an unexpected configuration file or secret, investigate before staging a broad set of changes. A correct local snapshot starts with knowing which files should and should not belong to the project.
4. git diff — inspect actual modifications
git diff shows unstaged modifications to tracked content. git diff --staged shows changes in the index compared with the current commit. These views are not interchangeable: a file can have some content staged and additional modifications that remain unstaged.
Review the diff for accidental deletions, debugging statements, dependency changes and sensitive values. New untracked files do not normally appear in a plain diff until they have been staged, so always compare diff review with status.
A diff is evidence of textual or other tracked changes, not proof that software behavior remains correct. Tests, reviews and relevant operational checks still matter.
5. git add — prepare the next snapshot
git add src/example.py stages changes to a specified path. It does not publish or commit them; it updates the index so the next commit can include those contents. git add . can be convenient, but may stage unrelated files, so specific paths are safer for beginners.
For finer control, git add -p can interactively stage selected hunks. That helps separate a bug fix from unrelated formatting. After staging, inspect git diff --staged rather than assume that a command succeeded exactly as intended.
Ignored paths may not be staged through ordinary broad adds, but ignore patterns are not security boundaries. A previously tracked file can remain tracked even after a matching ignore rule is written.
6. git commit — record work in local history
After checking the staged snapshot, git commit -m "Validate form input" records a local commit with a message describing the intent. Git may ask for an author identity before the first commit. Configure this according to your organization’s rules; an author field is not proof that a person independently reviewed the code.
A commit stores the staged state, not every modified file on the machine. It is local until pushed or otherwise transferred. A commit message should describe why a change exists rather than say only “updates.” Keep unrelated changes separate so the history remains understandable.
Do not store tokens or passwords in commits. Removing a secret in a later commit does not remove earlier copies automatically. Take appropriate containment and credential-rotation action if exposure occurs.
7. git log — investigate recorded history
git log --oneline -n 5 displays a concise view of recent reachable commits. Add --decorate when branch and tag labels are helpful. Git history is a graph, so merges, rebases and branch pointers affect how it is presented.
Use git log to find a change and git show to inspect a particular commit. A commit message describes the author’s stated intent; it does not replace reviewing the actual diff and tests. A shallow clone or missing remote refs may not expose all historical work.
When investigating a bug, record the affected revision and a reproducible test. Knowing when a line changed is helpful, but it does not alone establish what caused a reported failure.
8. git switch — work on the correct branch
git switch -c feature/notes creates a new branch and switches to it. git switch main changes to an existing branch named main, where such a branch exists. Projects may use another default branch name. The official switch manual explains its handling of the working tree and index.
Git normally refuses a switch that would overwrite incompatible local modifications. Do not immediately use a force option to suppress the warning. Inspect status and decide whether to commit, move or safely preserve work. The goal of a feature branch is to isolate a line of changes for review, not to create a separately copied project directory.
9. git fetch — inspect remote updates without integrating them
git fetch origin retrieves appropriate objects and remote refs from the remote typically called origin. Remote-tracking information is updated, but fetching does not ordinarily merge the remote work into the current local branch or overwrite the working tree. The official fetch reference describes these effects and relevant options.
After fetching, compare your branch with its corresponding remote-tracking branch. git pull performs a fetch followed by an integration step under the repository’s configuration, potentially using merge or rebase. Treat pull as a history-changing workflow rather than an equivalent of status.
If integration produces conflicts, read the affected files and understand both intentions. Resolving a conflict by blindly choosing one side can remove valid work. Coordinate with collaborators and rerun relevant tests.
10. git push — send authorized commits to a remote
git push -u origin feature/notes attempts to publish that local branch to the configured remote and establish an upstream relationship. It requires appropriate access and can be rejected by branch-protection rules, remote history or permissions.
A successful push does not inherently deploy a website or release an application; that only happens if a separate, explicitly configured automation reacts to the push. Review the branch, confirm no secrets were added, and follow the team’s pull-request or review process. Avoid force-pushing a shared branch without coordination because it can replace remote history.
Use the organization’s recommended authentication and credential manager. Do not paste private tokens into a README or a command that becomes visible in shared shell history.
Know the additional commands that require care
Git collaboration also uses git merge to combine histories, git revert to create a new commit reversing an earlier change, and git restore to restore files or unstage content. These operations differ substantially. For example, git restore --staged filename removes the file’s staged state, while git restore filename can discard unstaged tracked changes in the working tree. The Git restore manual explains the options.
git reset --hard and forced branch switches can discard work. Do not use destructive commands merely because Git displays an unfamiliar error. First determine which local edits and commits need to be preserved. For already-shared history, a carefully reviewed revert is often preferable to rewriting a branch in use by others.
The Pro Git documentation gives a fuller explanation of the working tree, staging area and recorded history. Learning these ideas makes the command list easier to apply accurately.
Practice a controlled daily workflow
In an authorized project, start with status, create a suitable branch, make a focused change and inspect the unstaged diff. Stage only intended files, inspect the staged diff, then commit with a descriptive message. Fetch before integrating remote work and run tests after resolving conflicts. Push only the reviewed branch that the team expects.
The most useful Git habit is to inspect before acting. Git preserves and coordinates snapshots, but it does not automatically validate code, prevent secrets from entering history or authorize a release. These ten commands form a foundation for safer collaboration when used with tests, permissions and review.