Git Basics
Introduction
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)
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-python-shop, thencdinto it by executingcd simple-python-shop. - 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-python-shop. 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-python-shop)). The line should look something likegit remote add origin https://github.com/your-username/simple-python-shop.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.
What's Next
We are finally done with introductions and essential setup so we can start with python lessons. Let's learn.