Git Basics
Now that we have connected git with github we can learn some git basics.
Git comes with a lot of commands, and is extremly powerful software. For now, we will focus on a few basic commands that you will use daily.
We belive in learning by doing, so let's go in this direction and prepare yourself for the assignments when we start working with python code.
Lesson Overview
In this lesson you will learn:
- What are
gitspecific terms - How to create a local repository
- How to add files to be tracked by
gitand commit changes to those files - How to create and connect remote repository (from Github) to your local repository, then pushing changes to remote repository.
Learn How Git Speaks
Before starting with commands, it's useful to get to know ourself with some wording git uses:
- Repository: A folder for your project. Each
gitproject is a repository (or repo for short). - Commit: A snapshot of your code at the time of creating commit. Commits are what allow you to undo your changes to any older commit at any time.
- Branch: A parallel codebase for you to experiment with and build new features without affecting the main branch
- Pull Request (PR): When work on the branch is finished, you issue a pull request to your main branch so changes can be merged.
- Remote: A copy of your repository NOT on your local machine (etc.: GitHub)
Do not worry about what these words mean just yet or try to remember it all. We will use git commonly throuout the course, and you will get plenty of exercise in using it.
Assignment
- To keep things organized while you work on projects, choose a directory on your local computer where you will store all of them and
cdinto it. For example create a directory calledProjectsin yourDocumentsdirectory. For the rest of the course, we will assume yourProjectsdirectory has the following path:~/Documents/Projects. - Now we need to create a directory which we will use to store our assigment project which we will evolve after each lesson. Create a new directory by executing
mkdir simple-bookstore, thencdinto it by executingcd simple-bookstore. - We need to tell
gitto start tracking changes in our directory, which is called initialization. Rungit initcommand to letgitknow that you want to start tracking this directory. - Now we will create our first file:
README.md. This file serves as description of your project and usually contains information on running your projects. We will not worry about this now, just open your code editor in that directory by executingcode .command (NOTE: don't forget the dot.at the end of the command). Create a new file with the nameREADME.mdand write some content in it, like:Or whatever contents you want.This is the start of my python journey!
Assignment
- Choose a directory on your local computer where you will store all of your projects and
cdinto it. For example create a directory calledProjectsin yourDocumentsdirectory. For the rest of the course, we will assume yourProjectsdirectory has the following path:~/Documents/Projects. - Create a new directory by executing
mkdir simple-bookstore, thencdinto it by executingcd simple-bookstore. - Initialize
gitrepository by executinggit initcommand. This tellsgitto start tracking for changes in our directory. - Let's create a
README.mdfile as every good repository should have one. Open your code editor in that directory by executingcode .command (NOTE: don't forget the.at the end of the command). Create a new file with the nameREADME.mdand write some content in it, like:Or whatever contents you want.This is the start of my python journey! - Let's check the status of our repository by executing
git statuscommand. You will see yourREADME.mdin the section untracked files. This meansgitknows about your file, but it's not part of the snapshot it will create, so let's fix that in the next step. - By executing
git add README.mdwe say togitthat we want to add that file to staging area which is like a space where git is aware of your files and which will be included in the snapshot (commit). Like saying a photographer to line up and frame the shot. Now we take the actuall snapshot (commit) in the next step. - Execute
git commit -m "Add README file with initial content"command which tellsgitto make the actuall snapshot (or commit).-mstands for message and its required in commit command, and not supplying it will open your default terminal text editor to enter the message. Message should be short but descriptive note explaining what you changed and why. - Now let's upload our files to Github (or push the changes we made). First we need to create a repository in our Github. Go to github.com/new and enter the name for your new remote repository. You can use the same name as your local directory:
simple-bookstore. Then choose visibility setting (public or private). DO NOT create files Github offers: README, .gitignore, license; we will do these manually. Click on the green Create repository button. - Github will offer varius ways of getting things done, but the main thing you should be looking for is the command starting with
git remote add origin. Copy that whole line and paste it in your terminal (be sure you are in our current directory (simple-bookstore)). The line should look something likegit remote add origin https://github.com/your-username/simple-bookstore.git. Execute the command to add our remote repository as remote to our current git repository, so we can push the changes we made to online repository. - Now run
git push -u origin mainto send files to your remote we just added in the previus step. The-uflag is used only first time you push to set up tracking connections so in later pushes, we can just dogit push. - Check your Github for pushed code. You should see your
README.mdsitting there on your repository view. Congratulations! You've just mastered the fundamental workflow of Git and GitHub.
In the above step 6, we executed git add README.md. This is fine when you have a few files to add to staging area, but what if there are dozens, or hundreds?
Git helps us out with git add . (notice the dot .) which will add all unstaged files to staging area.
This may not be what you always want, but it is a nice shortcut.
What's Next
We are finally done with introductions and essential setup so we can start with python lessons. Let's learn.