Git commands move changes through a clear workflow: edit files, review them, stage the intended changes, commit a snapshot, and share commits with a remote repository. The examples below use a small book-store project.
Initialize a Repository
Create a repository with main as its initial branch:
mkdir book-store
cd book-store
git init -b main
Example output:
Initialized empty Git repository in /home/sam/book-store/.git/
Check Repository Status
Run git status before and after staging or committing. It shows your branch and separates staged, modified, and untracked files.
git status --short --branch
Example output:
## No commits yet on main
?? README.md
?? index.html
?? marks an untracked file. Short status uses two columns: the first describes the staging area and the second describes the working tree.
Review Changes
Inspect unstaged changes before adding them:
git diff
Inspect staged changes before committing:
git diff --staged
Add Changes to the Staging Area
Stage one file when the commit should stay focused:
git add README.md
Stage all changes below the current directory:
git add .
Run status again to verify what the next commit will contain:
git status --short
Example output:
A README.md
A index.html
A in the first column means the file is staged as an addition.
Unstage a File
Remove a path from the staging area without discarding its working-tree changes:
git restore --staged index.html
A successful command normally prints no output. Run git status --short to confirm the staging change.
git reset index.html is an older equivalent, but git restore --staged states the intent more clearly.
Commit Staged Changes
Record the staged snapshot with a concise message:
git commit -m "Add project introduction"
Example output:
[main (root-commit) a1b2c3d] Add project introduction
1 file changed, 8 insertions(+)
create mode 100644 README.md
Commit IDs and file counts depend on your repository.
View Commit History
Use a compact graph to understand branch history:
git log --oneline --graph --decorate --all
Example output:
* 7e4c210 (HEAD -> main) Add product search
* a1b2c3d Add project introduction
Create and Switch Branches
Create a branch and switch to it in one command:
git switch -c feature/search
Example output:
Switched to a new branch 'feature/search'
Switch to an existing branch:
git switch main
git checkout remains available, but modern tutorials use git switch for branch changes and git restore for file restoration.
List Branches
git branch --show-current
git branch --all
Example output:
main
feature/search
* main
remotes/origin/main
Merge a Branch
Switch to the destination branch, then merge the completed feature:
git switch main
git merge feature/search
Example output:
Updating a1b2c3d..7e4c210
Fast-forward
search.html | 12 ++++++++++++
1 file changed, 12 insertions(+)
Git may create a merge commit or stop for conflict resolution when a fast-forward is not possible.
Delete a Merged Branch
git branch -d feature/search
Example output:
Deleted branch feature/search (was 7e4c210).
The safe -d option refuses to delete a branch whose commits are not merged.
Connect a Remote Repository
Add a remote named origin, then verify its URLs:
git remote add origin https://github.com/example/book-store.git
git remote -v
Example output:
origin https://github.com/example/book-store.git (fetch)
origin https://github.com/example/book-store.git (push)
Push Commits
Push main and record its upstream branch:
git push -u origin main
Example output:
To https://github.com/example/book-store.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
Later, git push is enough while the upstream remains configured.
Fetch and Pull Remote Changes
git fetch downloads remote updates without changing your current branch:
git fetch origin
git pull fetches and then integrates the configured upstream branch. Use --ff-only when you want to refuse an unexpected merge commit:
git pull --ff-only
Example output:
Updating 7e4c210..9fa1138
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)
Clone a Repository
Clone creates a new directory, downloads the history, and configures origin:
git clone https://github.com/example/book-store.git
Example output:
Cloning into 'book-store'...
remote: Enumerating objects: 42, done.
Receiving objects: 100% (42/42), done.
Restore an Uncommitted File
Discard working-tree changes only when you are sure they are not needed:
git restore README.md
Warning: This replaces uncommitted edits in the file. Git cannot recover changes that were never committed or stashed.
Conclusion
The essential Git loop is status, diff, add, commit, and push. Use git switch for branches, git restore for files or staging, and inspect the repository before every history-changing command.