Git Utils
This document contains useful Git commands that streamline your workflow and help you manage your repositories effectively.
Count Lines of Code Excluding Certain File Types
git ls-files | grep -v 'lock' | grep -v 'svg' | xargs wc -l
Explanation
This command counts the number of lines of code in your Git repository, excluding files with the extensions .lock and .svg. Here’s a breakdown of the command:
git ls-files: Lists all tracked files in the repository.grep -v 'lock': Filters out any files that contain the string "lock".grep -v 'svg': Further filters out any files that contain the string "svg".xargs wc -l: Passes the remaining files to thewccommand, which counts the number of lines in each file.
Remove Branches That Have Already Been Pushed
git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done
Explanation
This command removes local branches that have already been deleted from the remote repository. Here’s how it works:
git fetch -p: Fetches the latest changes from the remote repository and prunes (deletes) any remote-tracking branches that no longer exist.git for-each-ref --format '%(refname) %(upstream:track)' refs/heads: Lists all local branches along with their upstream tracking information.awk '$2 == "[gone]": Filters for branches whose upstream branches are gone (deleted).sub("refs/heads/", "", $1): Removes therefs/heads/prefix from the branch names.git branch -D $branch: Deletes the local branch.
Undo Last Commit Without Pushing
git reset --soft HEAD~1
Explanation
This command undoes the last commit in your Git repository without removing the changes from the staging area. Here’s a breakdown of the command:
git reset --soft HEAD~1: Moves the current branch pointer back by one commit, retaining all changes made in that commit in the staging area. This allows you to make further modifications or commit again without losing any work.
Undo a Git Add
git reset
Explanation
This command un-stages all files that were added to the staging area using git add. Here’s a breakdown of the command:
git reset: Resets the staging area to match the last commit, effectively removing all files from the staging area while keeping the changes in the working directory. This allows you to modify the files further before staging them again or committing them.
Git Diff Commands
This document provides useful commands for Git to compare differences between branches, file names, and remote references.
Compare Two Branches
git diff branch1..branch2
Explanation
This command shows the full content differences (diffs) between two specific branches. Here’s a breakdown of the command:
git diff: The primary command used to show changes between commits, commit and working tree, etc.branch1..branch2: Specifies the two tips to compare. It shows what you would see if you were looking atbranch2and comparing it back tobranch1.
List Changed Files Only
git diff --name-only branch1..branch2
Explanation
This query suppresses the code diffs and lists only the names of the files that have changed. Below is a breakdown of the command:
--name-only: An option that instructs Git to show only the names of changed files, omitting the contextual diff (the actual lines of code).branch1..branch2: The source and target branches for the comparison.
Show Change Statistics
git diff --stat branch1..branch2
Explanation
This command provides a summary of changes, showing which files changed and how many lines were added or removed. The following components detail the functionality:
--stat: Generates a diffstat. It displays a histogram of insertions and deletions for each file, giving a quick overview of the magnitude of changes.branch1..branch2: The range of branches being compared.
Compare Remote Branches
git diff origin/main..origin/feature/payment-retry
Explanation
This command compares the state of branches as they exist on the remote repository (e.g., GitHub or GitLab) without needing to check them out locally.
origin/main: References themainbranch on the remote namedorigin.origin/feature/payment-retry: References a specific feature branch on the remote.- This is useful for code reviews or verifying what will change before merging a remote branch.
Disable Pager for Output
git --no-pager diff branch1..branch2
Explanation
This command outputs the diff directly to the terminal (stdout) without opening a pagination tool (like less or vi).
git --no-pager: A global option that tells Git not to pipe the output into a pager. This is useful for scripts or when you want to scroll through the output in your terminal history.diff branch1..branch2: The standard comparison command executed after the pager is disabled.
Amending the Last Git Commit Message
git commit --amend -m "SOME_COMMENT"
Explanation
This command modifies the most recent commit in your Git repository by changing its commit message. Here’s a breakdown of the command:
git commit --amend: This option allows you to modify the last commit. It can be used to edit the commit message or to add changes to the commit.-m "some comment": This flag specifies the new commit message. Replace"some comment"with your desired message to reflect the changes made.
Using this command is particularly useful for correcting typos or adding additional information to the commit message without creating a new commit.