Git provides commands that help developers track changes, prepare files for commits, save project history, and synchronize work with remote repositories.
This guide explains the essential Git commands used in everyday development and demonstrates how they work together in a typical workflow.
The basic Git workflow consists of three main areas:
- Working Directory: The project files where changes are made.
- Staging Area: The area where selected changes are prepared for the next commit.
- Local Repository: The area where committed snapshots and project history are stored.
A remote repository, such as a GitHub repository, allows developers to share and synchronize their work.
flowchart TD
A["Working Directory"] -->|"git add"| B["Staging Area"]
B -->|"git commit"| C["Local Repository"]
C -->|"git push"| D["Remote Repository (GitHub)"]
D -->|"git fetch / git pull"| C
The git status command displays the current state of the working directory and staging area.
git statusIt helps identify:
- Modified files.
- New untracked files.
- Staged changes.
- The current branch.
- Whether the local branch is synchronized with its upstream branch.
git statusPossible output:
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
This means there are no pending changes.
The git add command stages changes for the next commit.
git add README.mdgit add file1.txt file2.txtgit add .Important: Staging a file does not create a commit. It only prepares the selected changes for the next commit.
The git diff command displays differences between versions of files.
git diffDisplays changes in the working directory that have not yet been staged.
git diff --cachedDisplays changes that have been staged and are ready to be committed.
git diff --checkThis checks for common whitespace errors in changes, such as trailing whitespace.
A commit records the staged changes in the local repository.
git commit -m "Commit message"git commit -m "Add basic Git workflow documentation"- Keep messages clear and meaningful.
- Describe what the commit changes.
- Avoid vague messages such as
updateorchanges. - Use a consistent commit message style.
A commit records a snapshot of the staged content, not automatically every change in the working directory.
The git log command displays the repository's commit history.
git loggit log --onelineExample:
a1b2c3d Add workflow documentation
e4f5g6h Update README
The identifiers above are illustrative examples.
git log --oneline --graph --decorateDisplays a compact history with a graph and branch references.
Note: If Git opens a pager, press q to exit.
The git push command sends local commits to a remote repository.
git push <remote> <branch>git push origin mainThis pushes the local main branch to the main branch on the remote named origin.
For a newly created branch that does not yet have an upstream:
git push -u origin feature-branchThe -u option sets the upstream tracking relationship.
The git pull command fetches changes from a remote repository and integrates them into the current local branch.
git pull <remote> <branch>git pull origin mainGit normally performs a fetch followed by an integration step. The integration method depends on Git configuration and command options.
Important: Before pulling, check your working tree and understand any local changes that might be affected.
The git fetch command downloads updates from a remote repository without integrating them into the current local branch.
git fetch <remote>git fetch originFetching allows you to inspect remote updates before deciding how to integrate them.
git fetch --pruneFetches updates from the remote and removes stale remote-tracking references for branches that have been deleted from the remote repository.
It does not delete your local branches.
The git remote -v command displays configured remote repositories and their URLs.
git remote -vExample:
origin https://github.com/username/project.git (fetch)
origin https://github.com/username/project.git (push)
origin is the conventional name assigned to the default remote when cloning a repository.
The following example demonstrates a common workflow for updating a project.
git statusMake the required changes in your editor.
git diffgit add README.mdgit diff --cachedgit commit -m "Update project documentation"git push origin mainThe changes are now uploaded to the remote repository.
Note: This example assumes you are working on main. In collaborative projects, changes are often pushed through feature branches and pull requests instead.
- Forgetting to stage changes before committing.
- Committing without reviewing staged changes.
- Using unclear commit messages.
- Confusing
git fetchwithgit pull. - Pushing to the wrong branch.
- Assuming
git addcreates a commit. - Forgetting to check the repository status.
- Assuming
git pushuploads uncommitted changes.
- Check
git statusregularly. - Review changes using
git diff. - Stage only the files required for a commit.
- Review staged changes before committing.
- Write meaningful commit messages.
- Fetch remote changes when you need to inspect updates.
- Understand the target branch before pushing.
- Keep commits focused on related changes.
- Check the status of a Git repository.
- Create or modify a text file.
- View unstaged changes using
git diff. - Stage the file using
git add. - View staged changes using
git diff --cached. - Commit the changes with a meaningful message.
- View the commit using
git log --oneline. - Check configured remotes using
git remote -v. - Practise fetching updates using
git fetch. - Push a commit to a practice branch.
| Command | Purpose |
|---|---|
git status |
Check repository status |
git add |
Stage changes |
git diff |
View unstaged changes |
git diff --cached |
View staged changes |
git diff --check |
Check whitespace errors |
git commit |
Record staged changes |
git log |
View commit history |
git push |
Upload commits |
git pull |
Fetch and integrate remote changes |
git fetch |
Download remote updates |
git fetch --prune |
Remove stale remote-tracking references |
git remote -v |
View remote URLs |
Previous Guide: 01 — Git and GitHub Fundamentals
Next Guide: 03 — Branching and Pull Requests