Git

Posts filed under Git

Beginning Git Cheat Sheet

Filed in Web DevelopmentTags: Git, Oenology, Themes, WordPress

Since Theme developers don't (at least, not yet) have SVN-commit access for the WordPress Theme Repository, I decided to host Oenology on GitHub. Git is an entirely different beast from Subversion, so it took me a bit to figure out exactly how to use it.

So, for the most part, this post is my own personal cheat sheet, though perhaps someone else new to Git might also find it helpful.

Initial Setup

Generate SSH Keypair:

ssh-keygen -t rsa -C "[email protected]"

Setup local system's global Git username and password:

git config --global user.name "User Name"
git config --global user.email [email protected]

Setting Up a Local Repository

One major difference between SVN and Git that I quickly discovered is that SVN defaults to setting up subdirectories for the working copy (/trunk) and previously tagged versions (/tags), but Git does not. So, whereas with SVN, my local repository checkout (i.e. the directory tracked by SVN) was simply a directory called /oenology, with Git, it made more sense for me to create my own subdirectories: /master for the working copy and /tags for tagged versions, and then do my Git checkout in /oenology/master.

Change path to working directory:

cd ~/<path>/oenology/master

Initialize local directory as a Git repository:

git init

Checkout the remote repository:

git remote add origin [email protected]:<username>/<repository>.git

Working With Git Repositories

Add (stage) all files in the local directory to the repository:

git add .

Commit (snapshot) all file changes in the local directory:

git commit -m "Commit Message"

Push all snapshots to the remote repository:

git push origin master

Working With Tags

Tag the current snapshot:

git tag -a "Version Number"

Push all tags to the remote repository:

git push origin --tags

Delete a tag:

git tag -d "v12345"
git push origin :refs/tags/v12345

Working With Branches

List Branches:

git branch

Create a Branch:

git branch {branch}

Switch to a Branch:

git checkout {branch}

Push a Branch:

git push origin {branch}

Merge a Branch:

git merge {branch}

Delete a Branch:

git delete -d {branch} // only deletes the branch if all changes are merged
git delete -D {branch} // deletes branch, regardless of merge state

For More Information

For more detailed help, refer to GitHub Help, and to the Git Reference.