Committing

Get Number of Git Commits to Squash

Here's the easiest way to get the number of commits is by running the following:

git log | grep ABC-123 | wc -l

You can then use that number in your git rebase -i HEAD..n command. Or if you're feeling adventurous, you can merge the two into a single command that counts and squashes at the same time.

git rebase -i HEAD.."`git log | grep ABC-123 | wc -l | sed -e 's/ //g'`"

The use of sed here is to remove the spaces that appear in wc's output.

Remove All Untrack Files

To remove all untracked files in a particular repository:

git clean -fd

Supported flags:

  • -f: force

  • -d: directories too

  • -x: remove ignored files too

Selectively Apply Changes From Patch

This can be used to select the hunks you want to apply from a patch file.

  1. Apply the patch using git apply

  2. Enter git add interactive mode using git add --patch. This will allow you to select the hunks to apply. See man page.

Once done, changes will be staged but not reflected in the working directory. To apply the staged changes to the working directory:

  1. Stash them using git stash save --keep-index --include-untracked

  2. Then run git stash drop

Sources: here, and here

Last updated