How To Use Git and Github From The Command Line

How To Use Git and Github From The Command Line

A lot of tech newbies confuse Git for GitHub others assume there are the same things but there are not in this article we are going to be diving deep into Git, GitHub, and how to use git from the command line.

What is Github?

GitHub is a software that allows developers to save files and projects in the cloud, GitHub also provides a free hosting platform to host projects and make them live. This may sound complex so let's use something simpler take your iCloud and Google Drive this software allows you to save your images, and files to the cloud so that if anything happens to your phone or computer your files are safe and can be retrieved.

This is what GitHub does so if your computer crashes you are sure that your project's files are safe in GitHub and Can easily be retrieved.

What is Git?

Git is a version control system(VSC). A version control system enables developers to track and manage changes in software code. Git allows you to create an updated version of your software without interfering with the main software or shutting it down while you make the changes.

Let's take for an instant you create an app called facetime and this is first version so we call it facetimeV.1.0 if you intend to make an update or change code in version one with git you can create a branch and make that update then merge it with the main software. This is branching we will talk more about it in detail in the article.

To download git software Click Here

Basic Git Commands

Git init

This command allows you to initiate git in your project. With this command, your project is moved to a working directory so that git can keep track of all changes in the file.

$git init

Git Add

This git add command is used to move files from the working directory to the staging area.

one can specify the particular file to add by using

$git add file_name

The dot(.) wildcard can also be used to add all files in the working directory to the staging area.

$git add .

Git Commit

The git commit command is used to move files from the staging area to your local repository. A local repository is a collection of all files and directories of a project in your local machine/computer. Committing to a local repository is like saving your code changes on your computer.

This repository is only available to your computer and not to everyone. This is where the push command becomes handy. The git commit must always have a commit message to show the changes that occurred in the files and what you fixed.

$git commit -m "commit_message"

Git Push

git push takes files from your local repository to a remote repository already created on Github. The remote repository is accessible to all users as it is available on the GitHub cloud except there are some restrictions or it is a private repository. Before the push command can work one must first create a repository on GitHub.

$git branch -M main
$git remote add origin https://github.com/{github-username}/{remote-url}
$git push -u origin main

The first line of code will change the name of the branch from master to main, GitHub initially referred to the master branch as master but subsequently, main is more used and widely accepted in the programming community.

The remote add is used to add the remote repository to your project.

The third line of code then pushes the whole files to your remote repository.

snnxx (1).png

Git Branch

The git branch command creates a new branch in your project. A branch is a duplicate of the version of your working directory, it contains all files and folders in your project.

The branch allows you to make changes to the code base and not affect the main project. The git branch command is followed by the name of the branch.

$git branch <branch-name>

To switch between branches we use the checkout command

$git checkout <branch-name>

Pushing a new branch

The method of pushing a new branch is different from the way we pushed the main branch earlier here we use the set-upstream option.

$git push --set-upstream origin <branch-name>

Git Merge

The git merge command is used to merge two branches. Before using this command navigate to the first branch using the checkout command. let's say we are merging the branch called version2 to the main branch.

$git checkout main
$git merge version2

Git Status

The git status command is used to display the current state of our project. it shows files that have been added to the staging area and files that have been manipulated in the working directory as not being added to the staging area, files that have been committed to the local repository or the remote repository.

$git status

Git Clone

Git clone is used to make a replica of a remote repository on your own computer. With this command, all files and directories in a remote repository are automatically moved to your local repository.

Git clone requires authentication before august 12,2021 passwords were used as a means of authentication but currently, personal access tokens are used as means of authentication. A personal access token can be created on your GitHub account.

$git clone https://{personal-access-token}@{github-username}.com/{remote-url}/

Conclusion

This is where I draw the curtain of today's article on how to use git and GitHub in the command line. Feel free to drop your questions, correction, and contribution in the comment box.