Wednesday, November 27, 2013

Common Git Tasks - So I Don't Forget Them

The title speaks for itself. I spend a lot of time hopping between three different source control systems: TFS (eww), SVN and Git.  As such sometimes my brain gets a little fried remembering some of the sequences to enter in Git to do common tasks. I really should script them out, but I'm going to write them down here so I don't forget them.

Yes I know this has been done 1000 times, but I haven't done it and this is an exercise to help me remember certain things. Also, I hope people correct me and/or this helps others.

Note: Anywhere I mention "GitHub" below I really mean "remote repository". It's just that I feel most people that are new to this stuff will be searching for "GitHub"

Checking in existing code to a new GitHub repository

Create the GitHub repo on their website from your account page. Be sure to add a .gitignore to exclude any directories or files you don't want to commit! In a prompt change directory to the root of your code folder then enter:

# initialize local repository
git init

# add all files to git tracking
git add .

# commit the tracked files to local repository
git commit -m "initial commit"

# connect to the remote repository
git remote add origin

# sync remote and local
git pull origin master

# push files to remote
git push origin master

Get a project from existing GitHub repository

Change directory to the directory you want to be the parent of your project directory. Do not create a new directory for your project, Git will do that for you.

# clone the master branch to initialize your project and local repo
git clone

# change to that project directory
cd some-project

# [OPTIONAL] switch to the branch you care about
git checkout somebranchname

Basic pull from GitHub

# usually you're on the master branch,
# otherwise swap "master" for your branch name.
git pull origin master

# sometimes you want to pull from a specific branch
# so switch to that branch locally
git checkout branchname

# then pull it from the remote
git pull origin branchname

Basic push up to GitHub

It's important to sync first with a pull!

# make sure you've added untracked files
git add .

# commit changes locally
git commit -m "commit message here"

# sync with remote
git pull origin master

# you might have to merge some changes at this point
# if so, generally it will involve editing the actual files
# and following the prompts in the console.

# now push your changes up to the remote
git push origin master

Overwriting all local changes with remote files from GitHub

So this one I've found will very depending on whether or not you have multiple branches you care about. This is also one where the difference between git pull and git fetch counts. For one thing, as of this writing git fetch as an --all flag you can use to pull down all remotes (branches and all), for another thing, pull will merge your files where fetch will not.

#get all files from all remotes without merging
git fetch -all

#reset your local to HEAD
git reset -hard origin/master

#remove all untracked files and directories
git clean -f -d

#[OPTIONAL] you might need reset and clean for other branches you've work on.
git checkout otherbranch
git reset -hard origin/otherbranch
git clean -f -d

Updating a forked repository from the original

When you've forked someone else's code, and you'd like to update it to the latest code base, you're going to need to rebase from an "upstream".

# add the upstream remote repository
# in this case we're naming it "upstream", but you can name it whatever you choose.
git remote add upstream

# fetch all files from all branches from the upstream repository
git fetch upstream

# switch to the branch you're working on
git checkout master

# rebase to replay all of the deltas from your previous commits on top
# of the files you just pulled down with the fetch.
git rebase upstream/master

# push your changes back up to your own remote fork
# the -f flag is to force the push and ignore checks and make sure
# that the remote doesn't have issues reconciling which deltas to trust
git push -f origin master

Changing a tag name

If you need to rename a tag, you need to add a new tag where the old tag was, then delete the old one

git tag newtag oldtag
git tag -d oldtag

Inserting a change at a past commit

When you need to make a change at a prior commit in your history and propagate that change throughout. I've found you don't always have to rebase. It seems like this solution is a lot more kosher as well. Basically, you create a copy of master, then reset the master back to the point where you want to make the change. Make the change and commit it, then merge the copy back on top of your change.

# checkout the branch you need to change
git checkout master

# make a copy of it on a new branch
git branch master_copy

# rewind the branch to the commit you'd like to insert
# a change after where [commit] is a commit # or a tag:
git reset --hard [commit]

# (make your changes here)
# and commit them:
git commit -am "changes have been made"

# now merge your copy back on top of your change
git merge master_copy

# clean up the copy
git branch -d master_copy

Monday, November 11, 2013

Quick 3D Pong Game With Three.JS

I was playing around with Three.js on Plunker last Friday and whipped up a silly little Pong Game.

I put a few notes in the readme, but please feel free to play with and/or alter the code. Just show me what you did if you do anything cool!

(Also, don't judge the code, I was sandboxing for kicks, :P haha)