Skip to content

pmachart/g-man

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

G-Man

A simple bash script to accelerate your daily git workflow.

Description

Gman builds upon git status to generate a numbered file list.

You can then simply refer to the files by their numbers instead of copy/pasting file names and paths.

Pre-configured commands and actions can be applied to the selected file(s) with various shorthands, such as a for git add.

A few quick examples :

g 2 a    # `git add` on the file listed second in your git status. Yep, just three characters.
g 3 4 t  # run tests on the third and fourth files
g 2-5 d  # display diff for files 2 3 4 and 5

Installation

Installing Gman is fairly simple, all you have to do is save the file and set up an alias to run it. Using "g" as a shorthand for git-related stuff seems like the obvious thing to do.

Just paste the following line wherever you store your aliases (.bashrc .bash_aliases etc.)

alias g='. ~/gman.sh'

Then make sure it is executable with chmod :

chmod +x ~/gman.sh

Adjust the path according to where you saved the script.

Note: Gman has to be run in source mode (.) to enable some actions like cding to the directory of a file.

Usage

Calling the script without arguments makes it run a git status. The output is displayed as a numbered list and stored.

The list is stored in a text file in a dedicated folder in the /tmp directory. This allows you to use Gman simultaneously in multiple repositories without interference. Also, saving it in the /tmp folder doesn't pollute your local repository, or any folder.

Upon calling the script again, with arguments, that stored list is then used.

The list is only regenerated by calling the script without arguments. If there were changes (new files, commits...) in your repo, call the script without any arguments again to refresh the list and make sure you are not performing actions on the wrong files.

CLI Details
Basic usage : specify a file's number, followed by the desired action.
Number range : in case you are too lazy.
One could also have typed g 3 4 5 6 a for the same outcome.
You can chain instructions, following the "file numbers - action" pattern.

In this example, files 1 2 and 3 are staged, file 6 is checked out, and we cd to the folder containing file 5.

(Prompt on these screenshots is with MagicMonty's bash-git-prompt)

Available commands

This is just a short list of some of the available actions. For more details, and to customize them or add your own, take a look at the script : I have tried my best to keep my coding style as clean, functional-y and expressive as possible. Contributions for more custom commands are very welcome.

Git related

Command and shorthand Description
add, a git add
ap git add -p
oops adds file to the last non-pushed commit
r, reset git reset
rm git rm || rm (does a simple rm if the file isn't tracked) adding f to the command adds the -f parameter (eg: g 3 rmf)
checkout, co, u reverts changes to staged or unstaged file
diff, d shows a diff of the file
ds "short" version : compact, less verbose diff output
dc shows a diff --cached of the file
dsc, dcs short and cached
sp git stash push
hist git log -u

Eslint / Jest related

Command and shorthand Description
lint run eslint on the file
lintfix run estlint --fix on the file
t Runs jest on the file, or runs the file itself if it is a test file.
It is configured for file.jsfile.test.js file naming.
You might want to change this.
tc jest --coverage
tw jest --watch
tu jest --updateSnapshot
tcwu yep, you can combine !
twuc yep, in any order !
tco runs coverage, opens html report in default browser

Pro tip for the html report : use a bookmarklet or a browser extention to auto-refresh it every few seconds and use tcw in your terminal. Voilà ! You have a live visualization of your line coverage !

Filesystem related

Command and shorthand Description
cat, c cat of the selected file
bat, b bat awesome cat alternative. Check it out !
cd cd to the folder containing the file
bak create a duplicate .bak of the file
vim, vi open the file with vim
nano, n open the file with nano
vscode, vs open the file with vscode
atom, at open the file with atom
l ls of the folder containing the file
ll ls -l
la ls -a
lla | lal ls -la

Misc.

Command and shorthand Description
view, v The default command when none is specified. Outputs a human readable list of the selected files for a quick review before doing something regrettable afterwards. It also copies the file paths to the clipboard (by default with xclip) for easy pasting.
print, p simple output of the file(s) for piping xargsing and $(more)... ;)

Customization

Actions

You can of course add new actions to the script. Look at the existing actions and inspire yourself from them. If you come up with some cool stuff, please contribute :)

Configuration

You can configure some of the actions of the script to suit your needs. Find the user configuration variables at the beginning of the script and fill in your testing or linting commands, typical file extensions, etc.

Arguments order /!\ Experimental

You can use Gman with reversed arguments order if you feel more comfortable with it. For example you could type g a 2 n 3 to stage file number 2 and nano file number 3. Try it out by supplying the -r parameter in first position (g -r a 2 n 3) If you want this to change permanently, you can edit the line REVERSED=0 in the user config part of the script.

Contributions

I have built this for my personal use, and to teach myself bash scripting. I am a front-end web developer and it is not my usual language, but I somehow enjoy it.

Many former colleagues have seen me use this tool and asked me to share it with them, and I gladly did. It's more convenient to share over github so... here we are :)

I have improved it and refactored it a few times and am always looking for advice and constructive criticism, so please feel free to contribute or comment about anything.

About

a simple bash script to accelerate your daily git usage

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages