Skip to main content

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 the wc command, 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 the refs/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.