Tag Archives: Git

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)

How I backup, from the point of view of (the incredible) XYplorer

Posted in XYplorer‘s user forum, under the topic “Other Software > How I backup”: http://www.xyplorer.com/xyfc/viewtopic.php?f=11&t=10614&p=115366#p115366

A month or so ago, I had my first true hard drive failure (terabyte! luckily recovered everything), followed a few days later by my five-year-old computer, which just wouldn’t turn on anymore…not even the initial BEEP. So much for the recovery disk.

I now have a four year contract with crashplan, which is only around four bucks a month. Once a day, or whenever I press a button, it backs up my computer to both an external hard drive and to their servers. It also backs up my wife’s laptop to MY computer…which is then backed up to those other two places. Pretty neat. All included in the four bucks. My wife’s only got a couple gigs of critical data, I’ve got several hundred gigs. Much of it music…including 38 gigs of Billy Joel *audio*, thank you very much :)

I also backup individual critical things to bitbucket, which is a Git repository, which is normally used for version control and branching of programming projects. For example, my XY configuration (C:\Users\aliteralmind\AppData\Roaming\XYplorer\) is a Git repository. Whenever I make any significant change, I save its configuration, shut down XY (not sure if shutting down is critical, but I’m nervous something might not be fully saved…), then in a shell/command prompt, I go to that directory and run either the following script with something like

git_add_commit_push_master.bat "Added custom toolbar button for custom layouts"

REM For use in all project sandboxes.
REM Save this in the project's root directory,
REM with the name:
REM    git_add_commit_push_master.bat

;set branch=%1
;set commit_msg=%2
set branch=master
set commit_msg=%1

REM ECHO The commit message is the one and only command-line parameter.
REM ECHO About to do
REM    THE FOLLOWING LINE SHOULD BE colon-upright slash!
REM    NOT an *EMOJI* <span>!!!
REM    DARN YOU WORDPRESS!!!
REM ECHO 1.  git add --all :/
REM ECHO 2.  git commit -m &quot;%commit_msg%&quot;
REM ECHO 3.  git push -u origin %branch%
REM PAUSE Press a key to proceed.

git add --all :/
REM PAUSE
git commit -m %commit_msg%
REM PAUSE
git push -u origin %branch%

Or more often just call the following script with no command-line parameters:

git_add_commit_push_master_quick.bat

call git_add_commit_push_master.bat &quot;Quick save (no message)&quot;

To quickly get to any repository’s directory I have an item in my XY catalog to copy it:

For example:

I am starting to use Listary in place of these catalog items (Although it’s annoying in Listary, to have to find the directory, then press the right arrow, then type “cpc” to copy-path-to-clipboard, and then enter to confirm. The author says the upcoming version will make this easier.)

Even though I likely don’t need versioning (and definitely don’t need branching!) for my XY configuration, it’s nice to know older versions are out there in case I backup something that’s truly messed up. Every now and then I just completely recreate the repository, to obliterate old versions (keeping track of every version, git repositories can balloon in size).

What’s beautiful about this is that everything is offline. Dropbox and mediafire and similar cloud tools are always online…always attached to your computer, so if you’re hit with something like crypto locker or some bad virus, then your dropbox–all of your backups!!–are immediately infected as well. For me this is an obvious dealbreaker, and I would be surprised if a “disconnect every time” version wasn’t eventually offered. With crashplan and bitbucket (and github, etc.), they’re backed up and then immediately disconnected, each time, which is so much safer.