Git notes

This is a list of git notes that I use everyday. Hopefully someone else finds it useful. I will update this list over time.

  • The master branch reflects production ready code only. This branch should be deployable at any time and merges to this branch should not break the build process.
  • Feature branches should be merged into the master when they are complete.
  • Feature branches consist of a single feature or discrete unit of work
  • Commit early and commit often!

Generating SSH-Public Key

  1. Switch to your .ssh directory and check if the id_rsa.pub file exists, this is your public key
  2. If it doesn’t exist, generate one with the following commandssh-keygen -t rsa -C "youremail@email.com"Output is something like the following:
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/danielbeard/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /Users/danielbeard/.ssh/id_rsa.
    Your public key has been saved in /Users/danielbeard/.ssh/id_rsa.pub.
    The key fingerprint is:
    43:c5:5b:5h:b1:f1:51:43:ad:20:a6:92:6a:1f:8a:3a danielbeard@iosdev.local
    

Copy the contents of the public key file to your clipboard:
pbcopy < ~/.ssh/id_rsa.pub

Normal Workflow

  • Checkout the remote repository with: git clone git@github.com:AustralianFinanceGroup/SalesTools.git
  • Updating the repository (won’t need to be done the first time): git pull
  • Create a new branch from the master or current development branch git checkout -b featurebranchname master
  • Make changes to local files
  • Check local modifications with git status
  • Stage certain files with git add <file1> <file2> etc or add all changes git add .
  • Commit to you local repository git commit -m 'commit message'
  • Push the local commit to the remote repository and set up for tracking git push -u origin featurebranchname
  • Repeat until feature branch is complete.
  • Merge master or development branch into the working branch
    • git checkout master
    • git pull
    • git checkout featurebranchname
    • git merge master
  • Push to remote repository git push -u origin featurebranchname
  • Submit pull request and wait for code review

Repository Changes

  • Checking which files are in what state: git status
  • Tracking new files: git add README
  • Committing your changes: git commit -m 'Commit message goes here'
  • Push your current master to the remote origin: git push -u origin master
  • Unstaging a staged file: git reset HEAD <file>
  • Stage all untracked files – git add -u
  • Unmodifying a modified file: git checkout -- benchmarks.rb

Notes:

  • The git add command stages a file for a commit. Calling git status shows which files are staged/unstaged or if they are untracked.
  • If you modify a file after calling git add, the git status command will show the file as being both staged and unstaged.
  • If you modify a file after calling git add, you have to run git add again to stage the latest version of the file
    Providing the -a option to git commit makes git automatically stage every file before doing the commit. (Letting you skip the git add part)

Basic repository changes: http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository

Branching

  • Creating a new branch: git checkout -b branchname
  • Branching from a current branch: git checkout -b feature devbranch
  • Pushing branch to remote with tracking: git push -u origin branchname
  • Rename branch git branch -m <oldname> <newname>
  • Delete the local branch: git branch -d branchname
  • Delete the remote branch: git push origin :branchname
  • Listing all branches: git branch -a
  • Just list remote branches: git branch -r
  • Checking out a tracked remote branch: git checkout --track origin/branch_name
  • Merging a branch
    • Must have committed all changes (at least locally) like so: git commit -a -m "Made a change in this branch"
    • Switch to whichever branch you are merging back into, e.g. master git checkout master
    • Merge the branch (ALWAYS USE NO-FF): git merge --no-ff branchname

Notes:

Tracked branches are local branches that have a direct relationship to a remote branch.
If you’re on a tracking branch and type git push or git pull, git automatically knows which branch and server to push/pull from.

Git remote branches :http://git-scm.com/book/en/Git-Branching-Remote-Branches#Tracking-Branches
Git local branches: http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging

Tagging

  • Only used for tagging master branch merges or “releases”
  • You can tag at any point, it doesn’t have to be immediately.
  • Show the commit history like this: git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme
  • To tag a certain commit, specify the commit checksum (or part of it) at the end of the command git tag -a v1.2.1 9fceb02
  • Then to transfer all your tags to the remote repository git push origin --tags

Git tags: http://git-scm.com/book/en/Git-Basics-Tagging

Tracking changes between branches

  • Show the modified files between two branches: git diff --name-status master..branch
  • Show side by side comparison : git difftool -t vimdiff master..branch

Other notes and links

Git aliases: http://git-scm.com/book/en/Git-Basics-Tips-and-Tricks#Git-Aliases
Branching model: http://nvie.com/posts/a-successful-git-branching-model/
GitHub flow: http://scottchacon.com/2011/08/31/github-flow.html

General purpose validation library for objective-c and iOS

This is a simple validation framework that I created for one of my projects that I am releasing under an open source MIT license.

The framework supports a number of different types of validations including string length, number range, email validation, regex validation and equality.

Validation rules can be added to any keypath of any object that inherits from NSObject, as well as having multiple validation rules per object. Here is a screenshot:

Image

And here is the project on Github – DBValidator

Github Gist Support for WordPress

Something I didn’t realise was possible until now, is WordPress natively supports Github Gists. All you have to do to embed a gist in your post is copy and paste the gist url on a new line. Everything else is handled automatically!
Code formatting is something that has been difficult for me up until now, any posts from now on will definitely be using gists.

More info here: http://en.support.wordpress.com/gist/

Example:

Adding a multiple images to a UIScrollView (iOS)

This is the code that I use when setting up a UIScrollView to contain multiple images that can be scrolled through. I add this code in the viewDidLoad or viewWillAppear methods and then set the numberOfViews to the number of images I want to display. This will also scale the images to fit the size of the screen using an aspect fit scaling. The example code loads images named “image_1”, “image_2” etc.

-(void) setupScrollView {
    //add the scrollview to the view
   self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 
                                           self.view.frame.size.width, 
                                           self.view.frame.size.height)];
   self.scrollView.pagingEnabled = YES;
   [self.scrollView setAlwaysBounceVertical:NO];
    //setup internal views
   NSInteger numberOfViews = 3;
   for (int i = 0; i < numberOfViews; i++) {
      CGFloat xOrigin = i * self.view.frame.size.width;
      UIImageView *image = [[UIImageView alloc] initWithFrame:
                                                CGRectMake(xOrigin, 0, 
                                                self.view.frame.size.width, 
                                                self.view.frame.size.height)];
      image.image = [UIImage imageNamed:[NSString stringWithFormat:
                                        @"image_%d", i+1]];
      image.contentMode = UIViewContentModeScaleAspectFit;
      [self.scrollView addSubview:image];
    }
    //set the scroll view content size
   self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 
                                            numberOfViews, 
                                            self.view.frame.size.height);
    //add the scrollview to this view
   [self.view addSubview:self.scrollView];
}