Essential Everyday Git Commands and Workflows¶
Your guide to the fundamental Git operations you'll use in daily development work.
The Basic Git Workflow¶
Git operations follow a three-step workflow that moves changes through different areas:
graph LR
A[Working Directory] -->|git add| B[Staging Area]
B -->|git commit| C[Repository]
C -->|git checkout| A
- Working Directory: Where you modify files
- Staging Area (Index): Where you prepare changes for commit
- Repository (.git): Where Git permanently stores changes as commits
This workflow allows you to:
- Work on multiple changes simultaneously
- Craft fine-grained commits that group related changes
- Review and refine changes before committing them
Working with Files¶
Checking Status¶
Check the status of your working directory:
See status with detailed information¶
See a compact status display¶
Or:
Status codes in short format:
??= Untracked filesA= Files added to stageM= Modified filesD= Deleted files- Left column = staging area, Right column = working directory
Adding Files¶
Stage files for the next commit:
Add a specific file¶
Add multiple files¶
Add all files in the current directory¶
Add all files in the entire repository¶
Or:
Add files interactively¶
Review each change:
Or:
Add all tracked files with modifications¶
Or:
Ignoring Files¶
Use a .gitignore file to exclude files from being tracked:
Create a .gitignore file¶
Common patterns for .gitignore:
# Ignore specific file
specific-file.txt
# Ignore file type
*.log
*.tmp
# Ignore entire directory
logs/
temp/
node_modules/
# Ignore all except specific files
*.json
!package.json
!package-lock.json
# Ignore files with tilde at the end (backup files)
*~
💡 Tip: Find pre-configured
.gitignoretemplates for various project types at github.com/github/gitignore
Moving and Renaming Files¶
Git treats renaming as a delete and add operation, but provides a shortcut:
Rename a file and update the index¶
Move a file to a different directory¶
Deleting Files¶
Remove files from both the working directory and the index:
Remove a file¶
Remove a file that has already been deleted from the filesystem¶
Remove a directory recursively¶
Making Commits¶
Creating Commits¶
Record staged changes as a commit:
Create a commit with a message¶
Create a commit with a multiline message¶
Opens editor:
Add and commit tracked files in one command¶
Add all changes and commit with a message¶
Add only specified files and commit in one step¶
Writing Good Commit Messages¶
Structure your commit messages for better readability and history:
Short summary (50 chars or less)
More detailed explanatory text, if necessary. Wrap it to about 72
characters. The blank line separating the summary from the body is
critical (unless you omit the body entirely).
- Bullet points are okay
- Typically a hyphen or asterisk is used, preceded by a space
Relates to: #123
💡 Tip: Follow the "50/72" rule: Keep the first line under 50 characters and other lines under 72 characters.
Amending Commits¶
Update the most recent commit:
Change the commit message of the latest commit¶
Add missed changes to the latest commit without changing the message¶
Amend author information for the latest commit¶
git commit --amend --author="New Name <[email protected]>"
⚠️ Warning: Never amend commits that have been pushed to a shared repository unless you're absolutely certain no one else has based their work on them.
Commit Best Practices¶
- Commit Atomic Changes: Each commit should represent a single logical change
- Commit Frequently: Smaller, more focused commits are easier to understand and review
- Write Meaningful Messages: Explain what changed and why (not how)
- Use Present Tense: "Add feature" instead of "Added feature"
- Reference Issues: Include issue/ticket numbers in commit messages when applicable
Viewing Changes¶
Viewing Uncommitted Changes¶
Compare your working directory or staging area with the repository:
Show unstaged changes¶
Show staged changes¶
Or:
Show all changes (staged and unstaged)¶
Show changes for a specific file¶
Viewing Commit History¶
View the commit history of your repository:
Show commit history¶
Show commit history with a graph¶
Show compact commit history (one line per commit)¶
Show commits with patches (diffs)¶
Show statistics for each commit¶
Show commit history for a specific file¶
Show commit history with date ranges¶
Examining Specific Commits¶
Inspect specific commits in detail:
Show details of a specific commit¶
Show changes in a specific file from a commit¶
Show the content of a file at a specific commit¶
Undoing Changes¶
Discarding Working Directory Changes¶
Discard uncommitted changes:
Discard changes to a specific file in working directory¶
Or (older syntax):
Discard all changes in working directory¶
Or (older syntax):
Unstaging Changes¶
Remove changes from the staging area:
Unstage a specific file¶
Or (older syntax):
Unstage all files¶
Or (older syntax):
Reverting Commits¶
Create new commits that undo previous commits:
Revert the most recent commit¶
Revert a specific commit¶
Revert multiple commits¶
Revert a merge commit¶
💡 Tip: Revert is the safest way to undo changes that have been shared with others since it doesn't alter history.
Resetting¶
Move branch pointer to a different commit (use with caution):
Reset staging area to match most recent commit¶
Leaves working directory unchanged:
Reset staging area and working directory to match most recent commit¶
Move current branch to a specific commit, keeping changes in working directory¶
Move current branch to a specific commit, discarding all changes¶
Move current branch to a specific commit, keeping changes as unstaged¶
⚠️ Warning:
git reset --hardpermanently discards changes. Be absolutely certain before using it.
Working with References¶
HEAD and Other References¶
Git has several ways to refer to commits:
HEAD: Points to the latest commit in the current branchHEAD^: Parent of HEAD (previous commit)HEAD~2: Grandparent of HEAD (2 commits back)main: Points to the latest commit in the main branchtag_name: Points to the commit associated with a specific tag
Show where HEAD points¶
Show the parent of HEAD¶
Show a commit 3 commits before HEAD¶
Using Commit Hashes¶
Every commit in Git has a unique hash identifier:
Show the full hash of a commit¶
Use the short form of a commit hash¶
💡 Tip: You only need the first few characters of a commit hash (usually 7+) to uniquely identify it.
Command Summary¶
| Command | Common Options | Description |
|---|---|---|
git status | -s, --short | Check repository status |
git add | ., -p, -A, -u | Stage changes |
git commit | -m, -am, --amend | Record changes |
git diff | --staged, --name-only | View changes |
git log | --oneline, --graph, -p | View history |
git show | commit_hash, HEAD | Inspect specific commit |
git restore | --staged, --source=HEAD | Undo changes |
git rm | --cached, -r | Remove files |
git mv | Move/rename files |