Rails Girls Presentation - Basic Intro to Git & GitHub

Post on 10-May-2015

402 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

A very basic introduction to Git & GitHub. Presented at Rails Girls Canberra

Transcript

Git BasicsDionne Saunders

What Git isn’t…

Dionne Saunders

What Git isn’t…

Dionne Saunders

• The insult seen at least once every three pages in a Harry Potter novel.

What Git isn’t…

Dionne Saunders

• The insult seen at least once every three pages in a Harry Potter novel.

• Git is not a pre-requisite to writing code. You can accomplish everything covered in this workshop without any knowledge of Git.

So… what is Git?

Dionne Saunders

So… what is Git?

Dionne Saunders

• Backup

So… what is Git?

Dionne Saunders

• Backup

• Tracked Changes

So… what is Git?

Dionne Saunders

• Backup

• Tracked Changes

• Peace of mind

Git Branches

Dionne Saunders

Git Branches

Dionne Saunders

• Branches provide a means of isolating new code which is currently in development or testing without affecting other branches, or the current working state of your application.

Git Branches

Dionne Saunders

• Branches provide a means of isolating new code which is currently in development or testing without affecting other branches, or the current working state of your application.

This is useful for both individual, and collaborative development.

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Imagine for a moment that you are developing an app called Railsbook. You’re working on a new feature called ‘relationship statuses’. Part way through, someone reports a bug. Your users are no longer able to change the privacy settings of their profiles!

Git Branches

Dionne Saunders

• Here’s an example of the benefit of branching, even when you’re coding solo:

Imagine for a moment that you are developing an app called Railsbook. You’re working on a new feature called ‘relationship statuses’. Part way through, someone reports a bug. Your users are no longer able to change the privacy settings of their profiles!

You need to fix this fast, but you spent hours working on implementing ‘relationship statuses’. You don’t want to lose the work you’ve done for that, but it certainly isn’t ready to be deployed onto the server.

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug.

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug. When you’re done, you merge the ‘privacy-fix’ branch into the ‘master’ branch and deploy your code.

Git Branches

Dionne Saunders

Let’s assume you did all that hard work on a separate branch called ‘relationships’.

You go back to the ‘master’ branch, create yet another branch called ‘privacy-fix’, and write some code to fix that bug. When you’re done, you merge the ‘privacy-fix’ branch into the ‘master’ branch and deploy your code.

Now, you can switch back to the ‘relationships’ branch and continue to code this new feature.

Create an account

Dionne Saunders

Create an account

Dionne Saunders

• Go to http://github.com and create an account

Create an account

Dionne Saunders

• Go to http://github.com and create an account

• Accounts are free, unless you want private repositories

Cloning your repo

Dionne Saunders

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

• cd /somewhere

Cloning your repo

Dionne Saunders

• Open a terminal (or “command prompt” for you windows peeps)

• cd /somewhere

• git clone git@github.com:lenoretres/fakeapp.git #replace with correct URL

This is what the repository looks like after creation via GitHub, then cloning to your computer.

Basic Workflow

Dionne Saunders

Basic Workflow

Dionne Saunders

• It’s a matter of personal preference. I’ll show you a simple strategy I use to help you get started.

Branch off of ‘master’

Dionne Saunders

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

• git checkout –b feature

Branch off of ‘master’

Dionne Saunders

• The master branch is generally the source of truth. It reflects the current state of your deployed application.

• Code which is not production ready shouldn’t be on your master branch.

• To create a new branch:

• git checkout –b readme

• In this command, we are essentially saying: git go to a new branch called readme

Now that we’ve branched off, we see our new local branch called ‘readme’.

Notice that there isn’t a corresponding ‘origin’ branch. We have not yet pushed the branch to the remote repository.

Pushing to origin

Dionne Saunders

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

Pushing to origin

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

• In this command, we are essentially saying: git upload my changes to a remote branch called readme

Now that we’ve pushed to the remote repository, we see that there is a branch called origin/readme

Write some code, commit, and push

Dionne Saunders

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

• git commit –a –m ‘Modified readme to demonstrate a commit’

Write some code, commit, and push

Dionne Saunders

• I have edited README.md and I want to push those changes to the remote repo

• First, I need to ‘commit’ the files locally:

• git commit –a –m ‘Modified readme to demonstrate a commit’

• In this command, we are essentially saying: git commit all changed files with a message saying ‘Modified readme to demonstrate a commit’

Yay! We can now see that the changes have been commited to our local readme branch.

Pushing to origin… again

Dionne Saunders

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

Pushing to origin… again

Dionne Saunders

• To push your branch to the remote repository:

• git push origin readme

• In this command, we are essentially saying: git upload my changes to a remote branch called readme

Our changes are now on origin/readme

Merging back to master

Dionne Saunders

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

• git checkout readme• git go to the branch called readme

Merging back to master

Dionne Saunders

• We’re done with this feature. It’s ready to be merged back into master, there are a couple of things we need to do for this:

• git checkout master• git go to the branch called master

• git fetch• git are there any changes?

• git pull origin master• git grab changes from the remote branch called master

• git checkout readme• git go to the branch called readme

• git rebase master• git make sure this branch is on top of the branch called master

Merging back to master

Dionne Saunders

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

• git checkout master• git go to the branch called master

Merging back to master

Dionne Saunders

• We have now ensured that the readme branch includes changes made to the master branch while we were off doing our own thing.

• We’re ready to merge the branch into master

• git checkout master• git go to the branch called master

• git merge readme• git merge the changes from readme into the current branch (master)

Merging back to master

Dionne Saunders

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

• git –d readme• git delete the branch called readme

Merging back to master

Dionne Saunders

• Master now has all the code we wrote in the readme branch. Let’s delete both the local and remote readme branches:

• git –d readme• git delete the branch called readme

• git push origin :readme• This command is non-intuitive and weird. What it’s doing is deleting the

remote branch. The colon is necessary. Don’t ask me why they did it this way =)

We only have a master branch now, but we still have all the changes that were made on the readme branch.

What now?

Dionne Saunders

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

• Go to GitHub, find an open source project, and make a code contribution.

What now?

Dionne Saunders

• There are plenty of git resources on the internet. Here are a few examples:

• http://stackoverflow.com is a Q&A site, if you’re feeling a bit lost with git or coding this is a great resource to check out.

• http://try.github.io is an interactive tutorial courtesy of GitHub & CodeSchool. You can try git commands online completely risk free.

• http://gitweekly.com/gitcasts is a git tutorial site.

• Go to GitHub, find an open source project, and make a code contribution.

• Open source contributions are a great way of giving back to the community, and showing off your code kung-fu to potential employers. Most of the gems you will end up using are open source projects.

The End

Dionne Saunders

Visit http://ihacked.it to download this presentation.

top related