Tracking Changes
git status
Description
It is used to display the state of the working directory and the staging area. It shows which changes have been staged, which haven't, and which files aren't being tracked by Git. This command is very useful for getting a quick overview of the current status of the repository.
Usage
Options
-s
or --short
: It gives the output in a more concise, short format.
Example short format output:
-b
or --branch:
Shows the branch and tracking information in a short format.
What It Shows
Untracked Files: Files in your working directory that aren't tracked by Git.
Tracked Files: Files that are tracked by Git and have changes that haven't been staged yet.
Staged Changes: Files that have changes staged for the next commit.
Branch Information: The current branch you are on and whether your local branch is ahead, behind, or has diverged from the remote branch.
Example Output
Here’s an example of what git status
might display
Or
Breakdown of Example Output
Branch Information:
On branch main
: Indicates the current branch.Your branch is up to date with 'origin/main'.
: Shows synchronization status with the remote branch.
Staged Changes (Changes to be committed):
modified: file1.txt
:file1.txt
has been modified and staged.new file: file2.txt
:file2.txt
is a new file that has been staged.
Changes Not Staged for Commit:
modified: file3.txt
:file3.txt
has been modified but not staged.
Untracked Files:
file4.txt
:file4.txt
is a new file that isn't being tracked by Git.
Common Workflow
git diff
Description
The git diff
command is used to show changes between commits, commit and working tree, etc. It is a powerful tool for inspecting the differences between various states of a repository.
Usage
Options
--staged
or--cached
: Compare the staged changes with the last commit.--name-only
: Show only the names of changed files.--name-status
: Show the names and status of changed files.-p
or--patch
: Generate patch (standard output format).--stat
: Show a summary of changes-U<n>
or--unified=<n>
: Generate diffs with <n> lines of context.--color
: Colorize the diff output.
Basic Use Cases
Example Output
git add
Description
It is used to add changes in the working directory to the staging area. This command prepares the changes to be included in the next commit.
Usage
<pathspec>
: Specifies the files or directories to be added. This can be a specific file, a directory, or a pattern.[--]
: Used to separate paths from options, useful if the paths might be mistaken for options.
Options
-A
or --all
: Stages all changes (modifications, deletions, and untracked files).
-p
or --patch
: Interactively stage changes, allowing you to review each hunk before staging
-u
or --update
: Stages modifications and deletions, but not new untracked files.
-n
or --dry-run
: Shows what would be staged without actually staging the changes.
-v
or --verbose
: Shows the files as they are being added.
What It Does
Stages Changes: Adds changes in specified files or directories to the staging area.
New and Modified Files: Tracks new files and stages changes in modified files.
Does Not Affect Unchanged Files: Does not change the state of files that haven't been modified.
Common Use Cases
Example Output
When running git status
after staging changes with git add
git rm
Description
It is used to remove files from the working directory and the staging area (index). This command stages the removal of files so that they will be deleted in the next commit.
Usage
<file>
: Specifies the file(s) to be removed.
Options
-f
or --force
: Forces the removal of files. This is required if the files have been modified or if they are staged for commit.
-r
or --recursive
: Allows recursive removal of files in directories.
--cached
: Removes the file only from the staging area, not from the working directory. This stops Git from tracking the file.
-n
or --dry-run
: Shows what would be removed without actually removing the files.
What It Does
Removes Files from the Working Directory: Deletes the specified files from the working directory.
Stages the Removal: Stages the deletion of the specified files, so the changes will be included in the next commit.
Does Not Remove Directories: Only removes files, not directories.
Common Use Cases
Example Workflow
Example Output
When running git status
after using git rm
git commit
Description
It is used to record changes in the repository. It captures a snapshot of the project’s currently staged changes, making them part of the project’s history.
Usage
<file>
: Optionally specify files to commit. If omitted, all staged changes will be committed.
Options
-m <msg>
or --message=<msg>
: Specifies the commit message directly in the command
-a
or --all
: Stages all tracked, modified files and commits them in one step
-v
or --verbose
: Shows the diff of changes in the editor during commit message writing.
--amend
: Modifies the previous commit with new changes and/or a new commit message
--dry-run
: Shows what would be committed without actually committing the changes.
-q
or --quiet
: Suppresses commit output
What It Does
Creates a Commit: Saves the staged changes as a new commit in the repository.
Includes a Commit Message: Each commit requires a message describing the changes.
Associates Metadata: Each commit includes metadata like the author, date, and a unique hash identifier.
Common Use Cases
Example Workflow
Example Output
When running git commit -m "Initial commit"
Last updated
Was this helpful?