Git is It!

cool thing about git…

you have a local repository that you can do whatever you want with… you could use this at your work with no need for a remote repo! You can have your current code checked out from your repo, then create a local git branch to do some small work:

To start:┬ácd to the project’s root folder and… initialize the local repo:

git init

If fresh folder, try this:

touch README.markdown
vi README.markdown
press the letter i (insert mode)
# Introduction to Git
press esc (exit insert mode)
hold shift, press zz (yes, twice, to save)

Or, add all files (you may want to modify gitignore if you have tmp files, and what not)

git add .

Commit all files

git commit -a -m "Initial Commit"

Now lets say you have a new bug to fix…

git checkout -b bug_one master

This creates a new branch named “bug_one” from the existing master branch, and switches to that branch

Check out your branches

git branch

You should see

* bug_one

Do some work, get some tests to pass, commit when things are good.
For example, edit the README.markdown file
Check status

git status

And you should see:

[bug_one*]$ jonsmac2-2:git_fun jon$ git status
# On branch bug_one
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#	modified:   README.markdown
no changes added to commit (use "git add" and/or "git commit -a")

Now commit the file

git commit README.markdown -m "updated"

Do this as often as you like, see where you are at:

git log --graph

If all goes well, you can merge it into your core work:

Switch back to master branch:

git checkout master

Merge in bug_one changes, one at a time (no fast-forward):

git merge --no-ff bug_one

Check out the merge history:

[master]$ jonsmac2-2:git_fun jon$ git log --graph
*   commit 727c28be28b012eab2970a9fe162963e52df4f72
|  Merge: 5ebd94c c24d6b9
| | Author: Jon Kern 
| | Date:   Wed Jun 15 00:26:03 2011 -0400
| | 
| |     Merge branch 'bug_one'
| |   
| * commit c24d6b9be2bffb00ac338d9fc4b0cbc6be35f580
|/  Author: Jon Kern 
|   Date:   Wed Jun 15 00:25:57 2011 -0400
|       Updated
* commit 5ebd94c971cfd17644c0619b60060576e87e8b66
  Author: Jon Kern 
  Date:   Wed Jun 15 00:21:16 2011 -0400
      Initial Commit

If all completely stinks in your bug_one branch, you can discard it and start over
Switch back to master branch:

git checkout master

Delete (should fail with directions on how to FORCE it to delete):

git branch -d bug_one

And start again…

git checkout -b bug_one master

If you liked what you did, push it

git push origin master


Along the way, I picked up various tips and tricks… Not quite sure where I got everything. I know I used some tricks from my friend, Corey Haines.

	name = Jon Kern
	email = jonkern@*****
	recentrepo = /Users/jon/railsprojects/*****
	ui = auto
	editor = mate -w
	excludesfile = /Users/jon/.gitignore
	co = checkout
	# View the SHA, description, and history graph of the latest 20 commits
	l = log --pretty=oneline -n 20 --graph
	# View the current working tree status using the short format
	s = status -s
	# Show the diff between the latest commit and the current state
	d = !"git diff-index --quiet HEAD -- || clear; git diff --patch-with-stat"
	# `git di $number` shows the diff between the state `$number` revisions ago and the current state
	di = !"d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
	# Pull in remote changes for the current repository and all its submodules
	p = !"git pull; git submodule foreach git pull origin master"
	# Clone a repository including all submodules
	c = clone --recursive
	# Commit all changes
	ca = !git add -A && git commit -av
	# Switch to a branch, creating it if necessary
	go = checkout -B
	# Show verbose output about tags, branches or remotes
	tags = tag -l
	branches = branch -a
	remotes = remote -v
	# Credit an author on the latest commit
	credit = "!f() { git commit --amend --author "$1 <$2>" -C HEAD; }; f"
	# Interactive rebase with the given number of latest commits
	reb = "!r() { git rebase -i HEAD~$1; }; r"
	# Undo a `git push`
	undopush = push -f origin HEAD^:master

	# Detect whitespace errors when applying a patch
	whitespace = fix

	# Use custom `.gitignore` and `.gitattributes`
	excludesfile = ~/.gitignore
	attributesfile = ~/.gitattributes
	# Treat spaces before tabs, lines that are indented with 8 or more spaces, and all kinds of trailing whitespace as an error
	whitespace = space-before-tab,indent-with-non-tab,trailing-space
	autocrlf = input

	# Use colors in Git commands that are capable of colored output when outputting to the terminal
	ui = auto
[color "branch"]
	current = yellow reverse
	local = yellow
	remote = green
[color "diff"]
	meta = yellow bold
	frag = magenta bold
	old = red bold
	new = green bold
[color "status"]
	added = yellow
	changed = green
	untracked = cyan
	# Include summaries of merged commits in newly created merge commit messages
	log = true

# Use `origin` as the default remote on the `master` branch in all cases
[branch "master"]
	remote = origin
	helper = osxkeychain
[difftool "sourcetree"]
	cmd = opendiff "$LOCAL" "$REMOTE"
	path = 
[mergetool "sourcetree"]
	cmd = /Applications/ "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
	trustExitCode = true
	site =
	endpoint =
	oauth-token = **********

Git Aliases

# Git shortcuts
alias gst="git status -uno"
alias gm="git merge --no-ff $1"
alias gd="git checkout develop"
alias gcom="git checkout master"
alias gp="git pull"
alias gpod="git push origin develop"
alias gpom="git push origin master"
alias gpto="git push --tags origin"
alias gphm="git push heroku master"
alias gsu="git submodule update"


Leave a Reply