DEV Community

Benjamin Ajewole
Benjamin Ajewole

Posted on

New to Git and GitHub

What is GitHub?

GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. I’d like to say GitHub is the Hub of Git. After version controlling your source file, you need a safe place to store it, GitHub is that safe place. I’d call GitHub a social platform of developers, a place where developers collaborate, contribute to open source projects, fork repositories, raise issues etc.

What is Version Control?

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. While working on a source file, you can use Ctrl + z to undo changes but when you close the source file and reopen it, you can’t use Ctrl + z to undo earlier changes before you close the source file. With version control, when you reopen your file, you can go back to an earlier version of your source file.

What is Git?

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source-code management in software development, but it can be used to keep track of changes in any set of files.

To follow this tutorial, you need a GitHub account and you need Git to be installed on your local machine(Computer).

In case you don’t have a GitHub account yet, go to https://github.com to create one.

And in case you don’t have Git installed on your local machine, go to https://git-scm.com/downloads to download one and install it on your PC.

Having created an account on GitHub and installed Git, follow the following steps religiously:

Step #1

Image

Start a project

Image

Create a new Repository

Create a new Repository on GitHub, call it any name.

Check the box Initialize ReadMe.md

Add Licence, MIT License

Click on Create Repository

So, we have successfully created a new repository one.

Step #2

Image

Clone Repository to Local machine

Clone Repository to your Local Machine

Open Git Bash on your Local machine

You need to set the username and email for your git

git config — global user.name “Rexben”

Replace Rexben with your Github username

git config — global user.email “rexben.rb@gmail.com

Replace with your GitHub email

Navigate to the folder you wanna clone your repository into, as for me, I wanna clone it into my document folder.

cd “C:\Users\postgres\Documents”

cd — current directory, change the current directory

Now, I’m now in my document folder.

Clone my GitHub repository I created

Copy the URL of your repo and paste it into Git Bash

git clone https://github.com/Rexben001/Tutorial.git

git clone — Cloning your online or remote repo into your local machine

When it is done cloning, you can go to check your document folder, you’ll find a folder with the name of your GitHub Repository name.

Step #3

Image

Create a new branch

Navigate into our cloned repo

cd Tutorial

Create another branch

By default, a branch master is created by git

It is best practice to create another branch and not to work directly in your master branch.

So, let’s create a branch called “develop”

git checkout -b develop

git branch — Used to list all the branches in a repo

git checkout <branchName> — To move out of the current branch into another branch. 

E.g. git checkout hello — I moved out of my current branch into a branch named “hello”

git checkout -b <branchName> — To create a new branch and move into it

E.g. git checkout -b develop — I created a new branch called develop and I moved into it

Step #4

*Add some files into it *Let’s add an index file and CSS file

touch index.html touch style.css

touch <fileName> — To create a new file 

mkdir <folderName> — To create a new folder

Step #5

Add some lines of code to index.html and style.css

You can use any text editor of your chosen, I’d be making use of VsCode

Open the folder from VsCode and add some codes to your index.html and style.css

Then, save the files

Step #6

Image

Commit changes to Git

Commit changes to git

Navigate back to your Git Bash

For git to start tracking your files/folders, you need to add them to add.

git add .

git add . — Add all the files/folders in your project to git.

*git commit -m “Initial commit” *

Replace initial commit with your commit message

git commit -m “initial commit” — Commits your files to git with a commit message. After adding your files to git you need to commit any changes made to your projects. I’d say it is the equivalent of Ctrl + s in any application. Also, you need to make your commit messages unique in case you wanna get a particular version of your source code, a very detailed commit message will make it stress-less to locate it.

Step #7

Image

Push to GitHub

**Push to GitHub **After working on a particular part of your Application, it is best practice to push it to GitHub in order to back up your project.

git push origin develop

git push origin <branchName> — Push the current branch to GitHub, in case the branch does not exist in your GitHub repo, it will be automatically created.

You’ll be prompted to enter your GitHub username and password. If you enter the correct details, your branch will be pushed online. You can navigate to your GitHub account and you’ll see the branch.

Image

Develop brain now on GitHub

Other Git commands

git log — To check your logs, to see a list of all commits and their ids

git status — This command is called finding out where you are. It will tell you which branch you’re currently on and what changes you have made that haven’t been recorded.

git merge — To merge two branch together

git diff— To see the different between two commits

For more commands:

https://git-scm.com/docs

https://github.com/joshnh/Git-Commands

Credits: Wikpedia

Top comments (0)