Useful git tips

Bash utility to trim whitespace from the ends of a string

Added 1/25/2015

#http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433
alias trim="sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'"

Example use (also used by the below gitf Bash function):

param=$(echo "   Hello!" | trim)
echo "$param"                     #Outputs "Hello!"

Bash function to search for all commits containing a file that has *changed* (whose name matches a glob)

Added 1/24/2015

:<<COMMENT
	Searches all commits in the current git repository containing a file
	that has *changed*, whose name matches a glob. If the glob does not
	contain any asterisks, then it is surrounded by them on both sides.

	Usage:
		gitf "05"     #Equivalent to "*05*"
		gitf "05_*"

	Parameter is required, and must be at least one non-whitespace
	character.

	See:
	- http://stackoverflow.com/questions/28119379/bash-function-to-find-all-git-commits-in-which-a-file-whose-name-matches-a-rege/28120305
	- http://stackoverflow.com/questions/28094136/bash-function-to-search-git-repository-for-a-filename-that-matches-regex/28095750
	- http://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory/372654#372654

	The main "git log" line is based on this answer
	- http://stackoverflow.com/a/28119940/2736496
	by Stack Overflow user Greg Bacon
	- http://stackoverflow.com/users/123109/greg-bacon

	With thanks to SwankSwashbucklers
	- http://stackoverflow.com/users/2615252/swankswashbucklers

	Short description: Stored in GITF_DESC
COMMENT
#GITF_DESC: For "aliaf" command (with an 'f'). Must end with a newline.
GITF_DESC="gitf [glob]: Searches all commits in the current git repository containing a file	that has *changed*, whose name matches a glob.\n"
gitf()  {
	#Exit if no parameter is provided (if it's the empty string)
		param=$(echo "$1" | trim)
		echo "$param"
		if [ -z "$param" ]  #http://tldp.org/LDP/abs/html/comparison-ops.html
		then
		  echo "Required parameter missing. Cancelled"; return
		fi

	#http://stackoverflow.com/questions/229551/string-contains-in-bash/229606#229606
	if [[ $param != *"*"* ]]
	then
	  param="*$param*"
	fi

	echo "Searching for \"$param\"..."

	git log -p --name-only --oneline --diff-filter=AMD --branches --tags -- "$param"
}

Which commit is currently checked out?

[source]

$ git show --oneline -s
cb96da8 Changed Sublime project desktop/temps to desktop.

A better git log: git lol

[source]

Install with

git config --global --add alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"

Use:

$ git lol
* 9f0349d (HEAD, origin/master, origin/HEAD, master) Now finally does stinking pull first.
* 1cc98b0 Added auto-update.
* 405970a Initial import.

(An alternative: git log --oneline --graph --decorate --all)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s