Git

Git cheatsheet

Git is A LOT easier to work with than you’d believe from listening to the geek zeitgeist.  However, to work with Git you need a passing understanding of the git command-line, as outlined below.

Initialise git

Once you’ve installed git, you need to let it know your username and email address.  You should only need to do this once.

git config --global user.name "Your name"
git config --global user.email "Your email"

Initialise a repo

To start working with git you need to initialise your workspace as a git repo.  The init command will create a master branch in your workspace.

git init

Committing your changes

There are 2 stages to getting your changes under git version control.  You have to first stage or add your changes, similar to SVN.  You can then commit your staged changes into git

#Stage all changes
git add .
#Commit your changes
git commit -m "Commit message"

Branches

Suggested git workflow is to pull/merge into your local master branch, and work off one or more local development branches.

# See all current branches in workspace
git branch
# Create a new branch called dev from master
git branch dev master

Checkout a branch

Once you’ve created a branch you’ll need to check check check it out (oh yeah!) to work on it.  You’ll see the branch name change in the command prompt

#Checking out a branch so we can work on it locally
git checkout dev

Merging a local branch

Once you’ve completed your work you can merge it back into master for sharing with colleagues or the rest of the world on github

# Checkout master as we want to merge dev into here
git checkout master
# Now we want to merge dev into master
git merge master dev

Delete a branch

git branch -D branchname

Working with Remote Repos – ie GitHub

Adding a Remote Repo

You need to let git know the location of the remote repo.  Convention appears to be to name the remote repo origin

# Add remote repo
git remote add origin git@github.com:GitHubUser/GitHubRepo.git
# List current remote repos
git remote

Pushing Changes to Remote Repo

You need to “push” your changes to the remote repo so they are available to others

# Push all updates in master to remote origin branch
git push -u origin master

Checking out code from a remote repo

The easiest way to begin working with code from a remote repo that you own is to clone that repo.  Cloning will checkout the code, set-up the origin and create the master branch.  Couldn’t be easier.

#Clone a remote repo that you own
git clone git@github.com:hunt3ri/AzurePack.git

Pull down the latest version from the remote repo to your local branch

If there’s a newer version on the remote repo, you’ll want to pull down the changes so they are available locally

#Pull down latest changes from remote origin server to your currently checked out branch
git pull origin

d

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s