Git-NumberedAdd for PowerShell

Git-NumberedAdd for PowerShell

posted in dev-setup on  •  • last updated on

After copying file paths from git status output and pasting them after a git add quite a few times by now, I’ve written a small PowerShell script to manipulate the working directory and staging area with fabricated indexes.

PowerShell Gallery

Now available on the PowerShell Gallery!

Install-Module -Name Git-NumberedAdd

Git-NumberedHelp (alias: gnh)

All these aliases to save time but who can remember all that?
Use gnh to display all actions available by Git-NumberedAdd with description and alias.

Git-NumberedStatus (alias: gs)

Displays the output of git status --short together with fabricated indexes like so

Git-NumberedStatus

Working directory color codes:

  • Yellow: Modified (file0 to file3)
  • DarkMagenta: Deleted (file4)
  • Blue: Added (file5)

Git-NumberedStatus accepts extra CLI arguments. Example: gs -u to see all --untracked-files.
Configure the color output by modifying $global:gitStatusNumbers.

With Numstat

If $global:gitStatusNumbers.includeNumstat is true (=by default), Git-NumberedStatus will also execute a git diff --numstat and add lines added/deleted to the output.

Git-NumberedStatus with --numstat

Git-NumberedAdd (alias: ga)

All set to stage some files!

PS> Git-NumberedAdd 0 1 2
add 'file0'
add 'file1'
add 'file2'

The same can be achieved with:

  • Git-NumberedAdd 0-2
  • Git-NumberedAdd -3
  • Git-NumberedAdd 012 (as long as index 12 does not exist!)

Git-NumberedAdd +3 would add file4 and file5 in the above scenario.

And Commit

Provide a final string argument to continue and commit the staged files:

gs
ga 0 1 2 "commit: files 0, 1 and 2"

Git-NumberedDiff (alias: gd)

Works like Git-NumberedAdd but can also be called without arguments to see the diff of all files. It will call git add -N for all newly created files so that they also show up in the diff.

git diff --cached is currently not implemented in this script.

Git-NumberedReset (alias: grs)

This only works for files already staged. Example output:

Git-NumberedStatus

To, for example unstage file0 and file4

PS> Git-NumberedReset 02
Unstaged changes after reset:
M       file0
M       file1
M       file2
D       file4

All actions

Wrappers for all your basic git commands

# Manipulate working directory:
Git-NumberedAdd # Alias: ga
Git-NumberedAddPatch # Alias: gap
Git-NumberedDiff # Alias: gd
Git-NumberedDiffCached # Alias: gdc
Git-NumberedCheckout # Alias: gco

# Manipulate the staging area:
Git-NumberedReset # Alias: grs

Git-Assuming

Unfamiliar with “assume unchanged”? Check the docs on git update-index --assume-unchanged.

After a Git-NumberedStatus, assume a file in the working directory with Git-NumberedAssumed (alias: gas)

Unassume files later on:

# List all currently assumed files
Git-ListAssumed # alias: gasl

# and follow with:
# git update-index --no-assume-unchanged
Git-NumberedUnassumed # alias: gnoas

There are now also aliases for git update-index --skip-worktree with Git-NumberedHidden (alias: ghide)

Git-ListHidden  # alias: glh

# and follow with:
Git-NumberedHidden # alias: ghide

Utilities

# Get the full path of a file
Git-GetFileNameByIndex 2 # alias: gn

# Delete file with index 2
rm (gn 2)


# Change current path to single index location
Git-NumberedSetLocation # Alias: gsl
# Use Pop-Location (alias: popd) to go back to where you came from

Alternatives

Sometime similar is actually built into Git itself

git add --interactive # or -i for short

Once in interactive mode:

git add --interactive

And then use 2: Update to stage files.


Also, see below for similar implementations in Bash and Perl.


Stuff that came into being during the making of this post
Other interesting reads
Updates
  • 16 January 2020 : Published to the PowerShell Gallery
  • 12 January 2020 : More actions and utilities + bugfixes
Tags: git powershell product