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

git status [<options>]

Options

-s or --short : It gives the output in a more concise, short format.

Example short format output:

M file3.txt
?? file4.txt
circle-info

M indicates a modified file. ?? indicates an untracked file.

-b or --branch: Shows the branch and tracking information in a short format.

git status -s
git status -b

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.

circle-info

Status Indication: git status provides hints on what to do next, such as commands to stage files, unstage files, or discard changes.

Performance: git status can be slow in large repositories because it checks the entire working directory. In such cases, using git status -uno (show untracked files only) might improve performance.

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.

circle-info

By default, git diff shows the differences in the command-line interface. However, it is also possible to use tools like graphical diff viewers to make it easier to review changes. For example, git difftool can be used to open a graphical diff viewer, such as Beyond Compare or KDiff3 or meld etc.

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

  1. Stages Changes: Adds changes in specified files or directories to the staging area.

  2. New and Modified Files: Tracks new files and stages changes in modified files.

  3. Does Not Affect Unchanged Files: Does not change the state of files that haven't been modified.

circle-info

Staging Area: The staging area (also called the index) is an intermediate area where changes are listed to be included in the next commit. This allows to build up a commit incrementally, adding and reviewing changes before committing them.

Removing Files: To remove a file from the staging area, use:

git restore --staged filename.txt

Interactive Mode: Using git add -p is especially useful for selectively staging parts of files.

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

  1. Removes Files from the Working Directory: Deletes the specified files from the working directory.

  2. Stages the Removal: Stages the deletion of the specified files, so the changes will be included in the next commit.

  3. 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

  1. Creates a Commit: Saves the staged changes as a new commit in the repository.

  2. Includes a Commit Message: Each commit requires a message describing the changes.

  3. Associates Metadata: Each commit includes metadata like the author, date, and a unique hash identifier.

circle-info

Commit Messages: Commit messages should be descriptive and concise, explaining the rationale for the changes. It's good practice to follow conventions such as the 50/72 rule: a short summary of 50 characters or less, followed by a blank line, and then a detailed description wrapped at 72 characters.

Atomic Commits: Each commit should represent a single logical change. This helps in understanding the project history and makes it easier to identify when specific changes were introduced.

Amending Commits: Be cautious with --amend if we've already pushed the original commit to a shared repository, as it rewrites history and can cause issues for others working with the same repository.

Revert Commits: If we have committed some changes and now wanted to undo the commit and bring back changes to staging or unstaging area then refer to git reset command.

Common Use Cases

Example Workflow

Example Output

When running git commit -m "Initial commit"

Last updated