GIT BASICS AND COMMANDS - FOUNDATIONS COURSE

GIT BASICS AND COMMANDS - FOUNDATIONS COURSE

GIT BASICS AND COMMANDS - FOUNDATIONS COURSE

GIT BASICS AND COMMANDS - FOUNDATIONS COURSE

Welcome back, in this lesson we will cover common Git commands used to manage your projects and to upload your work onto Github. We refer to these commands as the Git workflow. When you are using Git, these commands will be used most of the time and when you are done with this class you will have mastered Git halfway through.

It is important to understand that git is just a tool that works a lot like the command line, it enables you to work with files on Github, a platform for storing code and collaborating with other developers all over the globe.

LESSON OVERVIEW

Let us look at all the course work for this section and get acquainted with what is required of you in this learning session.

  1. How to create a repository on Github
  2. How to clone files from Github
  3. How to add files to Github
  4. How to push files to Github
  5. How to create branches on github
  6. Determine the status of Git repositories
  7. Importance of README files in repositories



HOW TO CREATE A GITHUB REPOSITORY

You have been given instructions on how to install github already but for more clarity please checkout the previous article on installing Github. Once you have Github installed, go to your account and select the option new repository. Give the repository a name (eg full stack) and description then select other options and create the repository, it is advised that you leave your repositories public as a beginner to facilitate learning.

With this you have created a Github repository, it is the easiest thing to do  and should not take you time at all. Once you are done continue the tutorial to learn to clone repositories.

HOW TO CLONE A GITHUB REPOSITORY

Cloning a github repository is like creating  a copy of a repository on your local machine, you can then modify this repository and push it back to Github and save changes to the repository. Let us get started, to clone the repository go back to Github and click on the name of the repository, once you are there you should see a green button named code. Click on this button and copy the code you see there, this code will enable us to clone the repository easily from our Git software.

  1. Git clone - Open your Git bash and change directory to wherever you want the repository to be cloned to like your desktop or a directory. Then type the command “git clone” give one space and then paste the link beside the command.

Example: “git clone https://github.com/vector-10/first.git

With this your repository will be cloned and you have completed this part of the exercise, you may receive a warning saying that you cloned an empty repository, just know that its fine and continue.

  1. Create file - While in the “full stack” repository, let us practise some commands and create a file. It is important to note that you can create files and do all the upcoming activities normally with the GUI but we advise you use the command line to get better acquainted with them. You should change directory into the repository in order to create the file
cd fullstack

The next thing to do now is to create a text file named “first_file.txt” using the touch command like this

touch first_file.txt

With this you have successfully cloned a repository and created a text file inside this repository on your local machine.

HOW TO ADD FILES WITH GIT

When you are done with work on cloned files and it is time to push them back to Github there are a few important steps to take and adding them is the first and very important one. Adding a file stages it and prepares it to be saved to the local repository before it can be pushed. All you have to do is use the command “git add” and then the file name.

Example : “git add first_file.txt” this command adds the file and prepares it to be saved, it is important to always add the extension name like “.txt, .js” etc to enable the computer to execute all commands correctly.

git add first_file.txt

HOW TO COMMIT AND PUSH FILES TO GITHUB

The “commit” and “push” are the two final steps to send files from your local repository to update the repository. They are quite simple to execute and we will study how we can commit files.

COMMIT

 In the command line, the commit statement simply means to save, it is the equivalent of saving a file on the command line. While committing, you will required to provide a short commit message about the file or directory to enable people easily understand your work when they come across it, the syntax is as follows

Example : git commit -m “commit message”, pay attention to the syntax, first of all you say git commit then -m give a space then specify the commit message within quotes. This message is displayed on the file in github so make sure to put explanatory statements to look professional and enable people to understand.

git commit -m “commit message”

PUSH

In the command line push is the last command to finalize sending your files to Github, just remember that your local files will update the ones present on Github so make sure you change directory “cd” into the directory location “fullstack”.

Example ; All you have to type is “git push”. It is that simple, all the files will then be sent to Github and once you head over to your account and refresh you can then see all your changes plus your commit messages.

git push

Well done, if you have reached this section of the lesson then you have gone a long way. We created a repository, cloned it to our local machine, then created a text file. After that we added it, committed/ saved and then pushed it back to Github.



HOW TO CREATES BRANCHES AND MERGE THEM ON GITHUB

As the name suggests, a branch is a subsection of a repository, it is used majorly when more than one person is working on a repository and does not want to disrupt the original code files. The command is simple and we will also learn to merge them back after we are done creating a branch

BRANCH

To create a branch, simply use the command “git checkout -b name of new branch”.

git checkout -b name of new branch

Ensure you provide a very good name of branch to make it easy to remember and work with. Let's say you are working and you want to switch between branches using the command “git checkout name of branch” and it will switch to another branch seamlessly.

git checkout name of branch

MERGE 

Merging simply means recombining the branches you created.To merge a branch all you have to do is “cd” into the repository and type the command “git merge name of branch”. The branch you should merge is the one you branched out in the first place and  with this we have successfully merged two branches back to the original one.

git merge name of branch

HOW TO DETERMINE THE REPOSITORY STATUS

The status of a repository displays the state of the directory and the staging area. In this case, the state a summary of activities that have taken place in the repository like the number of commits, files and the staged components of the repository. This is very important for reference and the command line always keeps record.

Example:  Change directory into the repository and type the command “git status”, this displays all the needed information. 

git status

IMPORTANCE OF A README FILE IN REPOSITORIES

The readme file is used to explain what is uploaded to a repository and how we can install or use it. It even allows the uploader to add images and videos to help the reader navigate through the project. A well written readme file is more important if you intend to show these projects in your resume for job applications.

SUMMARY OF ALL THE COMMANDS USED

  1. Git clone - To clone repositories into your local machine so that they can be worked on before they are pushed back to Github.
  2. Git add - To add files before you can commit them.
  3. Git commit - To save files on your repositories.
  4. Git push - To push files finally to your repositories.
  5. Git status - This shows the status of the files like the number of insertions,commits and so much more.
  6. Git checkout - To create a new branch for efficient work flow.
  7. Git merge - To merge back all the branches to one repository.

With this we have come to the end of git basics and if you go to the beginning of this lesson you can see that all the course content has been covered.

Remember to keep practicing so that you can get very good at working with Git and the command line interface.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow