Introduction to Git,GitHub and Version Control

What is Git?

Git is a popular version control system used for tracking code changes,tracking who made the changes,and coding collaboration.

What is GitHub?

Github is a code hosting platform for version control and collaboration,it helps you and others work on projects from anywhere.

What is Version Control?

Version control,also known as source control,is the practice of tracking and managing changes to files.

How to get started with Git:

You will have to go to their website(git-scm.com/downloads) to download and install the software.

How to get started with GitHub:

You will go to their website(github.com) and signup an account

Git Configuration

      git --version

By running the above command on your terminal, tells you the version of git installed on your system.

   git config --global user.name "your name"

   git config --global user.email "your email"

Running the above command tells git who you are.

git config --global --list

The above command displays the details you initially entered.

Git Commands-The following are git commands:

git init

The above command makes git aware of the code,it also initializes git on a project in your text editor.

  git status

The above command tells us the status of git at every point in time. It show you things like the branch you are using,which is master branch by default,tells you whether any commit has been made or not. Every file in a git repository has two states: The tracked and untracked. So basically the git status command also tells you which state the files are in.

To further elaborate on two states of files in git repository:

Tracked files are files that git monitors the changes that happen in them while untracked files are files that are already in the folder but git does not yet track the changes that happen in them.

git add index.html

The above command makes git to start tracking changes in a file(tracked state). I assumed the file name above to be index.html.

git add .

The command makes git to start tracking changes ,but it is used in a case where there are more than one file,It will save you the stress of adding files one after the other.

git commit -m ""

The above command keeps track of, or saves different stages in a project. For instance you are working on a landing page, you could save different aspects of it,like the text or some buttons,so that for some reason the file is mistakenly lost,you could go back to the last commit. Note that it very important to use a descriptive message when commiting,to know what point you will need to go back to.

 git branch (name of branch)

For instance in a collaboration,and your team are indecisive of maybe the background color to use or other functionalities,the git branch command will help you make copies of your source code and make changes to them independent of each other.

 git add .

 git commit -m ""

 git  branch (name of branch)

The above commands are the sequential methods of creating a new branch.

 git checkout (name of branch)

The above code makes a copy of your files on default master branch to whichever new branch you have created.

You could have as many branches as possible,You could this when you want to have different modifications of your source code. The command below shows you how to go through with this.

   git checkout (name of branch)

   git push origin (name of branch)

If you refresh the online repository after running the above commands you would notice that you have a new branch.

Now lets talk about GitHub,

How to Get Started on GitHub:

After you have successfully signed up,

  • Login into your account
  • click on new on the repository section of the page
  • Give your new repository a name
  • When you are done going through the other options,click on create repository

When you are done creating the repository, it takes you to a blank repository page that has various commands,commands used for things like:Creating a new repository on command line,pushing an existing repository from command line,import code from another repository.

Considering we have already gone through the process of creating a repository,we would have to do the command for pushing an existing repository from command line

   git remote add origin https://github.com/yourgithubname/yourrepositoryname

The above command links your local repository to github

  git remote -v

  git branch -M main

  git push -u origin main

The git remote -v command show you the remote links available. You could set up different remote links,especially if you want to move your code to different repositories,you could do that by changing the default remote name which is origin to any other name of your choice.

The git branch -M main command renames your branch from master to main.

The above command pushes your code from the local git to the internet.

The origin is like a placeholder that holds the url that you are pushing code from.

If you run the three commands above and refresh your page,you would notice your online repository is no longer empty,it will contain all the files in the main branch that you just pushed. As long as your repository is in public,your code can be accessed by anyone you send your repository link to. This helps so much in collaboration.

Push & Pull in github,

push command on github as we know is a process of sending your code from your local repository to github(online).

pull is the exact opposite of push,which could be described as retrieving your code from the online repository to your local repository.

You could also pull your code from one branch to the other,This can only be possible when you have more than one branch

  git checkout main

  git pull origin (name of the other branch)

The above command actually pulls code for your other branch to the main branch.

So in a collaboration,you and your colleague could actually work on different branches and in the end one person pulls from the other persons branch and the code is updated.

Cloning a repository

Cloning on github could be defined as replicating a project on someone else's repository.

There are several ways of cloning: HTTPS,SSH,GitHub CLI,or you could download the zip file of the repository which is not really recommendable. The HTTPS method is more advisable,you could do that by copying the url link there (which is the link to the repository).

How to go on with Cloning a repository

   git clone (repository link)

So by opening a terminal on a new folder and running the above command will replicate the exact files on the repository you cloned,plus it retains the name of the folder of repository you cloned.

GitHub work flow

Github work flow are good practices you follow on git or github. Some of which are:

  • Not working from your master/main branch,It is advisable that you create another branch, The idea is to checkout the master branch once you get in a new project, then create your own branch,so that if mistakenly something happens to your own,you could easily refer back to the master branch.

  • When you have reached a reasonable milestone on your project,push your code online,but its advisable to pull from your master or main branch before pushing online.

  • Pull request-Which could be described as requesting for your code to be merged to a different branch. This could be done by clicking on pull request in the repository,There you select both the branch you want to merge,and the branch you want it to be merged to. When that is done,a split screen of the code from the two branches loads,you are expected to check to be sure there are no mistakes,then when all that is done,click on create pull request button, add a name of the pull request. There is also a text area for you