HosterQ

Git for Beginners: Essential Commands and Workflows

20 Sep, 2026 HosterQ Editör Ekibi
A laptop, file folder, coffee cup, and plant alongside a branched Git commit history graph and cloud icons are shown.

Git for beginners becomes easier when each command answers a specific question: what changed, what belongs in the next saved revision, and where should that revision be shared? Git records project history locally. This guide uses a small practice repository so that learning the workflow does not put a production website at risk.

What problem does Git solve?

Git is a distributed version-control system. It records snapshots and their relationships, lets you compare changes, and supports parallel work through branches. A useful commit explains one coherent change, such as correcting a broken navigation link; it is more informative than a folder named “final-new-2”. Most history and comparison operations can run without a network connection because a normal clone contains local history.

Version control is not a complete backup system. Uncommitted files, ignored files, databases and external services may be absent from the repository. A remote copy is helpful, but access control, retention and independent backups still matter. The official Git book’s introduction to version control explains the underlying model.

Separate the repository, working tree, index and commit

Layer Purpose Useful inspection
Working tree The checked-out files you edit. git diff shows tracked changes relative to the index.
Index / staging area The content selected for the next commit. git diff --cached shows staged changes relative to HEAD.
Commit history Recorded snapshots, metadata and parent relationships. git log and git show.
Repository metadata Objects, references and configuration, normally in .git. git status summarizes the current work.

HEAD normally identifies the currently checked-out branch’s latest commit. Saving a file in an editor does not stage it. Staging does not commit it. Committing does not push it to a server. If you edit a file again after git add, the index still contains the version staged earlier until you add the newer changes.

Three blue icons representing Git repository, working directory on a laptop, and a commit chain, illustrating their relationship.
Conceptual illustration: Repositories, working trees, and commits.

Git and GitHub provide different services

Git is the version-control software. GitHub, GitLab and similar platforms host Git repositories and add collaboration features such as pull requests and issue tracking. You can use Git with no hosting account, or with your own server. Creating a local commit is therefore different from publishing a branch or opening a pull request.

A private repository is still a shared system with its own access rules. Keep passwords, API tokens, customer exports and private keys out of commits. Repository access is not a substitute for a secret manager or an application’s authorization checks.

Install Git and set the author identity

Install Git using the official distribution for your operating system or its trusted package manager, then check the installed version. The examples use a modern Git version with switch, restore and init -b. Replace the example identity with the name and email you intend to record in commits.

git --version
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global --get user.name
git config --global --get user.email

These values are commit metadata, not remote authentication credentials. The global scope affects this operating-system user’s repositories. For a work-specific identity, run the corresponding git config user.name and git config user.email commands inside that repository without --global.

Start a practice repository or clone an existing one

For a new exercise, choose a new empty directory. Do not initialize inside an unrelated repository or a live website tree.

mkdir git-practice
cd git-practice
git init -b main

For an existing project, copy its approved clone URL from the hosting platform and use git clone REPOSITORY_URL, replacing the placeholder before running it. Then enter the directory that Git created. Cloning and initializing are alternative starting points; you do not need to run git init inside a normal clone. A cloned project’s default branch may have a name other than main.

Use status, diff, add and commit as an inspection loop

Create README.md in the practice directory with one sentence, then inspect and record it:

git status --short
git add -- README.md
git diff --cached
git commit -m "Add project description"
git status

Before staging, the new file appears as untracked. Ordinary git diff does not display an untracked file’s contents; open it directly, or stage it and inspect git diff --cached. After a successful commit with no other changes, status reports a clean working tree.

Now edit the sentence. Run git diff to inspect the unstaged edit, stage the intended file, inspect the cached diff and commit a meaningful message. Prefer explicit filenames while learning: git add . can include unrelated changes or secrets. The official tutorial demonstrates this separation, and git-commit defines how staged content becomes a commit.

Read history and choose the correct undo operation

git log --oneline --graph --decorate -10
git show HEAD
git diff -- README.md
git diff --cached -- README.md

Undo depends on what you want to preserve. With an existing HEAD commit, git restore --staged -- README.md restores that path in the index from HEAD by default, leaving working-tree edits in place. It unstages changes rather than deleting your edited file.

git restore -- README.md discards unstaged edits by copying from the index by default. That is not necessarily the last commit: staged content may already differ from HEAD. Inspect the diff and save anything needed before using it. An explicit --source=HEAD chooses HEAD as the source, but still overwrites the selected working-tree content. These defaults are documented in git-restore.

To reverse a normal commit already shared with others, consider git revert COMMIT_ID, replacing the identifier after inspecting it. Revert records a new inverse commit; it does not erase the old one. Start with a clean working tree. If conflicts appear, inspect and resolve them before continuing, or use git revert --abort to abandon that revert. Merge commits require additional decisions and are outside this introductory example. Avoid treating reset --hard, clean -fd or force push as routine fixes.

Use branches to separate a change

After the first practice commit, create a branch for one task:

git switch -c docs-introduction
git branch --show-current
git status

Edit, review and commit on this branch. A branch names a line of history; it is not a separate backup of every local file. Uncommitted changes can follow a branch switch when compatible, and Git may refuse a switch that would overwrite them. Inspect status before switching and preserve work deliberately.

In a team, push the branch and follow its review/merge process. In the isolated practice repository, after committing you can return to main and use git merge --ff-only docs-introduction while main has no divergent commits. A fast-forward moves the branch pointer forward. A refusal calls for history inspection, not an automatic force operation.

Distinguish fetch, pull and push

Command Effect
git fetch origin Retrieves remote objects and updates remote-tracking references; it does not integrate them into your checked-out branch.
git pull --ff-only Fetches and integrates the configured upstream only when a fast-forward is possible.
git push Requests that a remote accept the selected local commits/references; authentication and server rules apply.

origin is a conventional remote name, not a special server. Check git remote -v and git branch -vv. A newly initialized local repository has no remote until one is added. On a configured project, publishing the example branch may use git push -u origin docs-introduction; the -u sets its upstream after a successful push.

Run pull with a clean working tree on the intended branch. If --ff-only fails because histories diverged, inspect the graph and use the team’s merge or rebase policy. The git-pull manual documents the integration choices. A rejected push can also mean permission or branch-protection failure; read its message before changing history.

Keep appropriate files out with .gitignore

A repository’s .gitignore can exclude untracked local secrets, generated output and caches. Adapt patterns to the application instead of copying a large list without understanding it:

.env
node_modules/
*.log

Commit the ignore file itself so collaborators share the rules. The gitignore manual explicitly says that already tracked files are unaffected. If a secret was committed, adding an ignore pattern does not remove it from history or other clones. Revoke/rotate the exposed credential and follow the repository’s incident and history-cleanup process.

Follow a repeatable beginner workflow

  1. Confirm the repository, branch and clean starting state; fetch shared work and update using the agreed policy.
  2. Create a focused branch and make one coherent change.
  3. Inspect the working diff, stage intended paths and inspect the staged diff.
  4. Run the project’s relevant checks, then commit with a message that explains the change.
  5. Push to the correct remote and request review when the project uses it.
  6. Deploy through the application’s release process and verify behavior.

A commit alone does not deploy a website. Test application changes in a WordPress staging environment where appropriate. A VPN connection to a private server can provide network reachability but does not grant Git repository permissions or validate a deployment.

Frequently asked questions

Why does Git say there is nothing to commit?

The index may contain no new change. Check the directory, branch, ignored status and whether edits were saved. A remote upload is not triggered by repeating commit.

Why does a file appear both staged and modified?

You staged one version and edited it again. Inspect both diffs; add again only if the newer edit belongs in the same commit.

Can a commit recover every deleted file?

Only content Git actually recorded is available from its history. Do not assume an untracked or ignored file can be recovered through Git.

Sources

Leave a Reply

Your email address will not be published. Required fields are marked *