This article walks you through how to use
git as your version control tool for your projects. It is assumed that you have already installed
git on your local machine. If you have not, this is how you do it via
MacBook-Pro:~ bobthedude$ brew install git
Please note that this guide is for beginners.
Why did I write this guide? Because the first time I used
git, I started from my local MacBook, not knowing about
git clone. So, I thought someone else might do the same thing and this guide would help.
Initialise a Repository From Local MacBook (without git clone)
Again, this is for the case where you already started a local
git repo before you create a repo on
GitHub ~ you do not use
1. Create a project folder
MacBook-Pro:~ bobthedude$ mkdir projectdude
2. Initialise local git repo
MacBook-Pro:~ bobthedude$ cd projectdude
MacBook-Pro:projectdude bobthedude$ git init
3. Add remote git repository
After you initialise your local repo above, you remembered that you have not created a repo on
GitHub, so you go to github.com and created one called
projectdude. Grab the repo url and replace
MacBook-Pro:projectdude bobthedude$ git remote add origin <remote_repo_url>
Alternative - You can actually skip step 1, 2, 3 above if you have already created a repository on github.com and do a
git clone <remote_repo_url> instead. After this, you can follow the below steps.
4. Create a new local branch
Below command will create a local branch named
bob-first-branch. You can replace it with whatever name you want for your local branch.
MacBook-Pro:projectdude bobthedude$ git checkout -b bob-first-branch
5. Start developing your project
You can check whether you are on the right branch or not by executing below command.
MacBook-Pro:projectdude bobthedude$ git status
On branch bob-first-branch
Now, you can start adding files, codes, etc. that you need for your project.
6. Add all files you want to commit
You can add multiple files and directories all in one go. Separate them by an empty space. Below examples would add the following:
- a directory named
modulesalong with the contents in it
- 2 files, which are
MacBook-Pro:projectdude bobthedude$ git add modules/ app.py execute.sh
Then, commit them. The flag
-a indicates you want to commit all the files you added previously, where
-m lets you add a commit message/ comment to describe what this commit is about.
MacBook-Pro:projectdude bobthedude$ git commit -am "initial commit"
7. Specify git credentials
Two things to specify here:
- First is your
GitHub‘s account email; the email you used to sign up on
- Second is your
GitHub‘s user name; this is the name you display when you login to your
MacBook-Pro:projectdude bobthedude$ git config --global user.email "email@example.com"
MacBook-Pro:projectdude bobthedude$ git config --global user.name "bobgit"
8. Push your local changes to remote branch
This command will create a remote branch on your remote repository so you can create a
pull request and later (once happy),
merge the branch to your
-u flag here indicates you are pushing your local changes in a local branch called
bob-first-branch upstream to
origin (which you specify in step 3 above).
MacBook-Pro:projectdude bobthedude$ git push -u origin bob-first-branch
That’s it! After this command is executed, there’ll be a link for you to click on that will take you to the page where you can create a