Hello and welcome to a step by step guide on how to use Git and Github. In this lesson, we are going to learn what is Git, why it’s important to software developers and how to use it. My name is Henry and let’s learn how to use Git.
What is Git?
Git in simple terms, is a piece of software that helps you keep track of your source code, files, and the changes that are made in those files over time. But what makes Git powerful, is the fact that it allows you to collaborate with other developers on a program, code or simply a file.
Importance of Git
Git was created by Linus Torvalds back in 2005 for tracking Linux Kernel development. Git designed for:
- Creating Back up of your source code
- Collaboration – any number of people, no limits.
- Easy code deployment on a server.
- Tracking History and changes of a file over time
- Cloud storage of your source code.
- Time traveling within your commits.
What is GitHub?
Github is a web-based hosting platform for collaboration, and version control, which is mostly used for code.
How to use Git?
Before we can use Git, we need to install it in our machines. I am using Mac Os but I believe installation is not hard. Learn how to install on various Operating System. I have already installed Git on my machine and to check the version of my computer, I will open my terminal and run the following command:
Here is the output:
Now that we have installed Git in our machines, let’s understand some basic Git commands.
Understanding Git Basic Commands
- git init – create a new repository on your machine
- git add * – adding files to staging
- git commit –m ”commit message” – this command commit changes to the head on local repository.
- git push origin master – this send changes to your remote repository on master branch.
- git remote add origin <server> – this command connects your local repository to remote repository.
How to use Git?
We are going to create a folder on our desktop called Learn Git by Example. Inside this folder, we are going to create a file called README.md, then we will track this file using git. We will also create an account with Github, create a repository and push our README.md file to Github repository.
Create an Account with Github
To create an account with Github, visit Github. You will get the following screen:
After sign up, login to your Github account. To create a repository on Github you will see the following screen.
Click on New and you see the following screen
I have given a name to my repository, make sure you do the same and hit create repository button. You get the following screen
The next step is to create Learn Git by Example folder on my desktop. On my terminal, I have navigated inside this folder. The next step is to create README.md file in this folder, make sure that your README.md file has the following content
# Learn How to use Git Git in simple terms, is a piece of software that helps you keep track of your source code, files, and the changes that are made in those files over time. ## Getting Started The following instruction will help you set up the project as a developer. ### Prerequisites Before you clone this project you need to: ``` Install Python 3.7 ``` ``` Create a virtual environment ``` ``` Make sure your virtualenvironment is activated everytime you work on this project ``` ### Managing Dependencies After cloning the project, on your terminal navigate to project base folder (where requirements.txt is) To install dependencies run the following command: ``` pip install -r requirements.txt ``` In case you add dependencies to the project, make sure you update requirements.txt by running the following command. ``` pip freeze > requirements.txt ``` Make sure you use git flow, the latest code will be on develop branch. ### Configuration File Django uses settings.py files for configuration. Do not edit this file, instead create a file named local_settings.py in the folder where settings.py is located and copy everything that is located inside settings.py apart from the last lines which start with try. Every time you want to change configuration use local_settings.py ### Version Control Since we are using git - make sure you setup git flow on your local machine. We will be using feature development structure. ### Development After developing a specific feature - merge your code to your local develop and push to online develop. ### Running Migrations Before you can run the project in development environment, you need to set up the migration. To create initial migration run the following command: ``` python manage.py makemigrations qed ``` To sync you migration with the database use the following command: ``` python manage.py migrate ``` ### Built With * [Python 3.7](https://www.python.org/downloads/release/python-370/) - Programming Language * [Django Web Framework](https://www.djangoproject.com/) - The web framework used * [Git Flow Cheatsheet](https://danielkummer.github.io/git-flow-cheatsheet/) - Version Control ### Removing All Migrations Files Sometimes in the course of development you find yourself at point where you need to clear migrations. You can use the following commands: ``` find . -path "*/migrations/*.py" -not -name "__init__.py" -delete ``` ``` find . -path "*/migrations/*.pyc" -delete ``` ### Authors * **Henry Mbugua** ## Company * [Hlab](http://blog.hlab.tech)
Now that we have a file to track, on our terminal we are going to:
Initialise repository by running the following command:
We are going to add README.md file to staging by using the following command:
git add .
We are going to add a commit message by running the following commit:
git commit –m”Initial commit”
Here is the output of our terminal so far.
We need to connect our local repository to the repository we created on Github. Go back to Github and you see the following screen:
We need to copy the highlighted command which has git remote add origin <server> and paste that on our terminal.
Now that our local repository is connected to remote repository, we need to push our README.md file to our remote repository. To do that, we use the following command:
git push origin master
Now Git has already pushed readme.md file to the remote repository. Go back to Github and refresh, you see the following.
- We have learned about Git.
- We have learned about Github.
- We have created Github account.
- We have created Repository.
- We have used Git to track Readme.md file.
We that we conclude this lesson. Learn more about Git Usage.