L32 An Intro To Git and GitHub For Beginners (Tutorial)
L32 An Intro To Git and GitHub For Beginners (Tutorial)
Product
HOME
JOBS
BLOG
OPEN SOURCE
HUBSPOT PRODUCT
AND ENGINEERING
BLOG
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 1/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
In August, we hosted a Women Who Code meetup at HubSpot and led a workshop for
beginners on using git and GitHub. I first walked through a slide presentation on the basics
and background of git and then we broke out into groups to run through a tutorial I created to
simulate working on a large, collaborative project. We got feedback after the event that it was
a helpful, hands-on introduction. So if you're new to git, too, follow the steps below to get
comfortable making changes to the code base, opening up a pull request (PR), and merging
code into the master branch. Any important git and GitHub terms are in bold with links to the
official git reference materials.
The first two things you'll want to do are install git and create a free GitHub account.
Follow the instructions here to install git (if it's not already installed). Note that for this tutorial
we will be using git on the command line only. While there are some great git GUIs (graphical
user interfaces), I think it's easier to learn git using git-specific commands first and then to try
out a git GUI once you're more comfortable with the command.
Join here.
Once you've done that, create a GitHub account our subscribers
(Accounts are free for public ×
repositories, but there's a charge for privateSign
repositories.)
up here and we'll keep you in the loop on all things
product
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 2/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
When creating a new project on your local machine using git, you'll first create a
new repository (or often, 'repo', for short).
To use git we'll be using the terminal. If you don't have much experience with the terminal and
basic commands, check out this tutorial (especially the 'Navigating the Filesystem' and 'Moving
Around' sections).
To begin, open up a terminal and move to where you want to place the project on your local
machine using the cd (change directory) command. For example, if you have a 'projects' folder
on your desktop, you'd do something like:
To initialize a git repository in the root of the folder, run the git init command:
Go ahead and add a new file to the project, using any text editor you like or running a touch
Join our subscribers ×
command.
Sign up here and we'll keep you in the loop on all things
product
Once you've added or modified files in a folder containing a git repo, git will notice that
changes have been made inside the repo. But, git won't officially keep track of the file (that is,
Subscribe me to the blog
put it in a commit - we'll talk more about commits next) unless you explicitly tell it to.
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 3/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
After creating the new file, you can use the git status command to see which files git knows
exist.
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
mnelson.txt
nothing added to commit but untracked files present (use "git add" to track)
What this basically says is, "Hey, we noticed you created a new file called mnelson.txt, but
unless you use the 'git add' command we aren't going to do anything with it."
One of the most confusing parts when you're first learning git is the concept of the staging
environment and how it relates to a commit.
A commit is a record of what files you have changed since the last time you made a
commit. Essentially, you make changes to your repo (for example, adding a file or modifying
one) and then tell git to put those files into a commit.
Join our subscribers ×
Signand
Commits make up the essence of your project up here
allowand we'll
you tokeep you into
go back the loop
the on allofthings
state a
product
project at any point.
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 4/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
So, how do you tell git which files to put into a commit? This is where the staging
environment or index come in. As seen in Step 2, when you make changes to your repo, git
notices that a file has changed but won't do anything with it (like adding it in a commit).
To add a file to a commit, you first need to add it to the staging environment. To do this, you
can use the git add <filename> command (see Step 3 below).
Once you've used the git add command to add all the files you want to the staging
environment, you can then tell git to package them into a commit using the git commit
command.
Note: The staging environment, also called 'staging', is the new preferred term for this, but you
can also see it referred to as the 'index'.
Add a file to the staging environment using the git add command.
If you rerun the git status command, you'll see that git has added the file to the staging
environment (notice the "Changes to be committed" line).
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
To reiterate, the file has not yet been added to a commit, but it's about to be.
Join our subscribers ×
Sign up here and we'll keep you in the loop on all things
product
Step 4: Create a commit
Subscribe me to the blog
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 5/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
Run the command git commit -m "Your message about the commit"
The message at the end of the commit should be something related to what the commit
contains - maybe it's a new feature, maybe it's a bug fix, maybe it's just fixing a typo. Don't put
a message like "asdfadsf" or "foobar". That makes the other people who see your commit sad.
Very, very, sad.
Now that you've made a new commit, let's try something a little more advanced.
Say you want to make a new feature but are worried about making changes to the main project
while developing the feature. This is where git branches come in.
Branches allow you to move back and forth between 'states' of a project. For instance, if you
want to add a new page to your website you can create a new branch just for that page without
affecting the main part of the project. Once you're done with the page, you can merge your
changes from your branch into the master branch. When you create a new branch, Git
keeps track of which commit your branch 'branched' off of, so it knows the history behind all
the files.
Let's say you are on the master branch and want to create a new branch to develop your web
page. Here's what you'll do: Run git checkout -b <my branch name>. This command will
automatically create a new branch and thenJoin our
'check yousubscribers
out' on it, meaning git will move you to
×
that branch, off of the master branch. Sign up here and we'll keep you in the loop on all things
product
After running the above command, you can use the git branch command to confirm that your
Subscribe me to the blog
branch was created:
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 6/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
The branch name with the asterisk next to it indicates which branch you're pointed to at that
given time.
Now, if you switch back to the master branch and make some more commits, your new branch
won't see any of those changes until you merge those changes onto your new branch.
If you only want to keep track of your code locally, you don't need to use GitHub. But if you
want to work with a team, you can use GitHub to collaboratively modify the project's code.
To create a new repo on GitHub, log in and go to the GitHub home page. You should see a
green '+ New repository' button:
When you're done filling out the information, press the 'Create repository' button to make your
new repo.
Join our subscribers ×
GitHub will ask if you want to create a new repo from
Sign up herescratch
and we'llorkeep
if you
youwant
in theto add
loop on aallrepo
thingsyou
product
have created locally. In this case, since we've already created a new repo locally, we want to
push that onto GitHub so follow the '....or push an existing repository from the command
Subscribe me to the blog
line' section:
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 8/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
(You'll want to change the URL in the first command line to what GitHub lists in this section
since your GitHub username and repo name are different.)
Now we'll push the commit in your branch to your new GitHub repo. This allows other people
to see the changes you've made. If they're approved by the repository's owner, the changes
can then be merged into the master branch.
To push changes onto a new branch on GitHub, you'll want to run git push origin
yourbranchname. GitHub will automatically create the branch for you on the remote
repository:
You might be wondering what that "origin" word means in the command above. What happens
Join our subscribers ×
is that when you clone a remote repository to your local machine, git creates an alias for you.
Sign
In nearly all cases this alias is called "origin." upessentially
It's here and we'll keep you infor
shorthand thethe
loopremote
on all things
product
repository's URL. So, to push your changes to the remote repository, you could've used either
Subscribe me to the blog
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 9/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
(If this is your first time using GitHub locally, it might prompt you to log in with your GitHub
username and password.)
If you refresh the GitHub page, you'll see note saying a branch with your name has just been
pushed into the repository. You can also click the 'branches' link to see your branch listed
there.
Now click the green button in the screenshot above. We're going to make a pull request!
A pull request (or PR) is a way to alert a repo's owners that you want to make some changes
to their code. It allows them to review the code and make sure it looks good before putting
your changes on the master branch.
This is what the PR page looks like before you've submitted it:
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 10/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
And this is what it looks like once you've submitted the PR request:
You might see a big green button at the bottom that says 'Merge pull request'. Clicking this
means you'll merge your changes into the master branch.
Note that this button won't always be green. In some cases it'll be grey, which means you're
faced with a merge conflict. This is when there is a change in one file that conflicts with a
change in another file and git can't figure out which version to use. You'll have to manually go
in and tell git which version to use.
Sometimes you'll be a co-owner or the sole owner of a repo, in which case you may not need
to create a PR to merge your changes. However, it's still a good idea to make one so you can
keep a more complete history of your updates and to make sure you always create a new
branch when making changes.
Step 9: Merge a PR
Go ahead and click the green 'Merge pull request' button. This will merge your changes into
the master branch. Join our subscribers ×
Sign up here and we'll keep you in the loop on all things
product
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 11/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
When you're done, I recommend deleting your branch (too many branches can become
messy), so hit that grey 'Delete branch' button as well.
You can double check that your commits were merged by clicking on the 'Commits' link on the
first page of your new repo.
This will show you a list of all the commits in that branch. You can see the one I just merged
right up top (Merge pull request #2).
You can also see the hash code of the commit on the right hand side. A hash code is a unique
Subscribe me to the blog
identifier for that specific commit. It's useful for referring to specific commits and when undoing
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 12/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
changes (use the git revert <hash code number> command to backtrack).
Right now, the repo on GitHub looks a little different than what you have on your local
machine. For example, the commit you made in your branch and merged into the master
branch doesn't exist in the master branch on your local machine.
In order to get the most recent changes that you or others have merged on GitHub, use the
git pull origin master command (when working on the master branch).
This shows you all the files that have changed and how they've changed.
Now we can use the git log command again to see all new commits.
(You may need to switch branches back to the master branch. You can do that using the git
checkout master command.)
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 13/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
Author: Meghan Nelson <[email protected]>
Date: Fri Sep 11 17:43:22 2015 -0400
commit 1e8dc0830b4db8c93efd80479ea886264768520c
Author: Meghan Nelson <[email protected]>
Date: Fri Sep 11 17:06:05 2015 -0400
commit b345d9a25353037afdeaa9fcaf9f330effd157f1
Author: Meghan Nelson <[email protected]>
Date: Thu Sep 10 17:42:15 2015 -0400
You've successfully made a PR and merged your code to the master branch. Congratulations!
If you'd like to dive a little deeper, check out the files in this Git101 folder for even more tips
and tricks on using git and GitHub.
I also recommend finding some time to work with your team on simulating a smaller group
project like we did here. Have your team make a new folder with your team name, and add
some files with text to it. Then, try pushing those changes to this remote repo. That way, your
team can start making changes to files they didn't originally create and practice using the
PR feature. And, use the git blame and git history tools on GitHub to get familiar with tracking
which changes have been made in a file and who made those changes.
The more you use git, the more comfortable you'll... git with it. (I couldn't resist.)
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 14/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 15/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
FIRST NAME
LAST NAME
Subscribing here provides HubSpot with contact information that we will use to email
you HubSpot Product Blog content from time to time. We won’t share your information
with anyone outside of HubSpot. We may keep the information you submitted for up to
three years (don't worry though, spam isn't our thing). If you’d like to know more about
how we use your personal data please visit https://legal.hubspot.com/privacy-policy
SUBSCRIBE
TOPICS
PRODUCT ››
ENGINEERING ››
DESIGN ››
PRODUCT MANAGEMENT ››
UX ››
CULTURE ››
CAREER GROWTH ››
Search
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 16/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
Thanks for the tutorial, but I have one question. I've done to step 8 (Step 8: Create a Pull
Request (PR), but the github page could not find any changes, it says "master and my-branch-
name are identical." I tried several times but still couldn't find the reason. I don't know where
did I do wrong, anyone has the same problem?
REPLY TO JUNE
Hi June! I'm glad you're going through the tutorial. It sounds like what's happening is
you've created your new branch on GitHub, but you haven't made changes (commits) on
that branch or pushed your local changes (commits) onto GitHub.
I hope this helps, let me know if you run into any problems!
REPLY TO MEGHAN NELSON
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 18/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
something really easy to follow like your tutorial is such a great happiness.
Thanks so much!
REPLY TO JUNE
Thanks for the answer. Yesterday I tried it again and finally could make the git push command!
I'm completely new in coding, have just studied it for a few months, and finding something
really easy to follow like your tutorial is such a great happiness.
Thanks so much!
Thanks for the answer. Yesterday I tried it again and finally could make the git push command!
I'm completely new in coding, have just studied it for a few months, and finding something
really easy to follow like your tutorial is such a great happiness.
Thanks so much!
REPLY TO MICHAEL OWEN
Hello. Great post--SO HELPFUL!!! :) I was floundering until I read it. You have the gift of
teaching! Anyways...I followed the above tutorial and merged the branches on gitHub;
however, once I ran `git pull origin master` from the command line, the extra branch (`test-
branch`) that was merged into master on gitHub is still present on my local machine. Is that
expected behavior? I figured the `git pull` command would cause my local machine to mirror
what's in my gitHub repo. Join our subscribers ×
REPLY TO JESSE LIND Sign up here and we'll keep you in the loop on all things
product
Pankaj Sharma 12/3/2019, 1:55:59 PM
Subscribe me to the blog
Simply awesome . very simple and beautiful.
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 19/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
REPLY TO PANKAJ SHARMA
m
REPLY TO MN MM,
Thank you so much! Guides like this are a lifesaver for absolute beginners like me.
REPLY TO CARTER
The reason why people a having the problem with the identical file thing is that after creating
the branch the file was not changed so it was still identical to the master branch
REPLY TO JOSEPH MCCARRON
Joseph, yeah I had to redone this exercise 3 times because the part mentioning about
modifying the working branch before sending it to GitHub was missing.
Otherwise this was a great tutorial!
REPLY TO NEW TOGIT
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 20/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
Awesome tutorial! I will definitely show this to my group to teach them the basics of git and
github!
REPLY TO JAKE
Would you also please post - " How to deploy a website using GitHub" for beginners. Thank
You so much !
REPLY TO REEJA REJIN
Otherwise a great tutorial but as mentioned above you could add a paragraph about first
modifying the mnelson.txt in working branch before sending it to GitHub. This would eliminate
the general confusion why pull request could not be done (at there were not changes in the
updated branch as done per your instructions)
REPLY TO NEW TO GIT
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 21/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
i'm pretty new to this. i found online to fix this error it's necessary to generate an ssh-key. but
the follow up was unclear. what does it mean and how do i go about this.
REPLY TO DAMND BELGER
https://www.youtube.com/watch?v=ouO4BpGvEnE
REPLY TO ALEX
This is the best , cleanest and straightforward explanation on GIT. Reading once I can not
forget for ever in my life. Good Job for creating and sharing this page.
REPLY TO GURU SHARAN
Can you please check the link to the tutorial on "basic commands" in Step 1? I clicked through
and was unable to find the tutorial at the link provided: http://mac.appstorm.net/how-to/utilities-
how-to/how-to-use-terminal-the-basics/
Thank you.
REPLY TO ANDY
This article is on the first page and the first row in the google.
REPLY TO MUHAMMAD ZAWAWI MANJA
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 22/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
It seems a good tutorial, however I stuck on: "check out this tutorial (especially the 'Navigating
the Filesystem' and 'Moving Around' sections).".
This link seems to be broken.
REPLY TO JAN H. KILA
Hi,
It is really nice and helpful document.
I want to copy remote repository from github into new repository in github.
How Can I do it using command line?
Thanks,
Nayana
REPLY TO NAYANA CHAVDA
Repositories
Branches
Commits
Pull Requests
Git (the version control software GitHub is built on)
Note:A a great way to learn GitHub, before working on larger projects, is to open pull requests ×
Join our subscribers
in your own repository and merge them yourself.
Sign up here and we'll keep you in the loop on all things
product
After merging you can delete the branch by clicking a "Delete branch button".
Subscribe me to the blog
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 23/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
more:https://dlightdaily.blogspot.com/p/what-is-github-explained.html
REPLY TO DLIGHT DAILY
Following Step 10, it might also be recommended to delete local branch by using `git branch -d
yourbranchname` to keep local repo clear as well.
REPLY TO FEI YAO
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners
REPLY TO CHANDLER DEV
Comment*
protected by reCAPTCHA
Privacy - Terms
SUBMIT COMMENT
Join our subscribers ×
Sign up here and we'll keep you in the loop on all things
product
What is HubSpot | Our Story | Our Products | Culture Code
Facebook | Twitter | Subscribe me to the blog
Instagram
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 24/25
3/25/2020 An Intro to Git and GitHub for Beginners (Tutorial)
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners 25/25