Since its creation, Git has become an immensely popular Distributed Source Control System. It allows you to track changes in your files while simplifying collaborative work on files and projects both for individuals and companies.
People often confuse Git and GitHub, but they are quite different things. Github is a centralized cloud server that allows you to store your code in repositories. It also allows you to interact with other developers on Open Source Projects.
The main focus of this lesson is to not only give you a proper foundation in Git, but also to serve as an inspiration for further learning. By the end of this session, we should be able to push a simple hangman python sketch to github from our computer terminal.Now that we know a basic concept of Git, lets move on to the next part: Installing Git
Unless you are a windows user, getting git setup on your device is very easy from the command line:
sudo apt-get install git
git version
you will get the option to download x code or to just install git, for now, let's just install git. Go ahead and click the install button
python-hangman
seems decent enough
Now that we have an Account on Github lets update our credentials in the command line locally. To do that, we
use the git config
command for that. We want these configurations to be saved on our computer
so that we don't have to set it up everytime we want to use git. So we'll pass it the --global
option.
git config --global user.name "yourUserNameHere"
This command will set your username gobally. Next, let's setup our email globally too.
git config --global user.email your@email.com
With that done, let's navigate into our hangman project directory and manage it with git. Click here to download the hangman project file.
Git doesn't automatically determine which files or projects to manage on your computer. In order to manage your
project with git, you need to initialize an instance of git in the project directory.
To do that, we'll use the init
command.
git init
With git initialized, let's check the status of our project directory. This will tell us about the files in our project directory and their state in relation to git.
git status
Let's add our hangman.py to git locally.
git add hangman.py
Next, we'll record the state of our repository now ("Which in this case is the state of our hangman.py file"). We will add a message to describe the state of our repo or what changes we made. We will use the commit
command in git, and give it the message -m
option.
git commit -m "added main project file"
Now, let's try to push our repository to Github.
First we need to set our remote. A remote is git's fancy way of saying The Place where your code is stored. Copy the link to the repositor you created on github.
git remote add origin https://github.com/yourUsername/repository.git
With our remote added, we can now push the repository to github.
git push -u origin master
Command | Meaning |
---|---|
git diff |
Shows differences that haven't been staged |
git reset [filename] |
Unstages the file, but preseves it contents |
git rm [filename] |
Deletes the file from our working directory and removes it from git too. Note: you will have to run git commit afterwards to delete the file |
git pull |
Downloads all commits that are in your Github repository. |