DEV Community

Cover image for Getting started with Git and Github
Lovepreet Singh
Lovepreet Singh

Posted on • Updated on

Getting started with Git and Github

When I started with git and GitHub, it was a real pain. Most of the tutorials were terrible, I mean really terrible.
I want this article not to be a tutorial but my experience on how I figured it all and you can too.
So, first of all, you may be thinking what the heck is git?
In technical terms, you can call it "version control system".It means it creates a record of every change in our files. It was developed by Linus Torvalds(Yeah, you guessed right, he is the creator of Linux kernel too!). Its popularity can mostly be attributed to Github, the most popular open-source hosting and management system which provides git version control functionality.

Installing git

Windows

To download git for windows, visit https://git-scm.com/download/win. The downloading will start automatically.

With git installed, what to do now?
Go to cmd and type

git --version 
Enter fullscreen mode Exit fullscreen mode

to make sure git is installed properly.

Working with git

With git installed, we should now head to Github. Create an account.

Github stores codebase in what they call repository. We are going to create one.
Click on the new button to create the repo. We will be working with this repo.

Alt Text
Fill the details and ta-da you have created your first Github repo.
Open your repo and click on the code button and copy the URL.

Spoiler!

The coming steps are Command interface intensive.
If you are GUI type, then be sure to check out the GitHub desktop app.

For the command interface, I would recommend using git bash. It comes with git installation.

If you want to work with others repo, firstly Fork(github term for copy) the repo. This can be done easily from the fork button on the repo's page.
Follow the below given steps for further workflow.

Okay, let's continue with our repo.
Our first step would be to copy the repo to our local machine. It is done like this,

git clone [your GitHub repo url]
Enter fullscreen mode Exit fullscreen mode

This will copy the repo to your local machine. Url can be of forked repo or newly created repo.
Now make some changes to your code in your favorite editor.
In git bash type

git add .
Enter fullscreen mode Exit fullscreen mode

This will add your file to the staging area(sort of temporary storage)
then type

git commit - m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

This is to tell others what changes you have made.
finally, type

git push origin master
Enter fullscreen mode Exit fullscreen mode

This command will push the changes to your Github repo.
Last but not least let's list the steps for pushing your local changes to the remote repository.

git add .

git commit -m "your commit message"

git push origin master
Enter fullscreen mode Exit fullscreen mode

If everything goes right, then your changes will be reflected in the remote repository.

Creating and pushing local repo to Github.

Go to Github and create an empty repository.
In git bash, go to a suitable folder and type

git init
Enter fullscreen mode Exit fullscreen mode

This will initialize a git repo in your folder.
Type,

git remote add origin [repo url]
Enter fullscreen mode Exit fullscreen mode

Be sure to firstly, stage, and commit your changes. Then,

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Changes will be reflected in the Github repo.

There are some commands you should know,

git status
Enter fullscreen mode Exit fullscreen mode

This command lists all the modified files that are not staged and committed.

git log
Enter fullscreen mode Exit fullscreen mode

This command lists all the commits.

Before we wrap up, there another important concept,Branching.

Branch.

It is a very powerful and useful feature of git, for example, if you want to test a new feature without compromising the whole code, just create a branch. Changes you made in this branch will not affect the main 'master' branch and vice versa(When we create a Github repo, it automatically creates a 'master' branch, which is the default branch).

Creating a branch is easy

git branch [branch_name]
Enter fullscreen mode Exit fullscreen mode

To work on the branch, you need to go to the branch
just type

git checkout -b [branch_name]
Enter fullscreen mode Exit fullscreen mode

To merge the branch,

git merge [branch_name]
Enter fullscreen mode Exit fullscreen mode

Well, this is all you need for the starter pack.

Goodbye👋

Top comments (5)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Awesome, I think it's very good to understand how git works.
Although most IDE's will do most of the tech work for you.

Collapse
 
shannannon profile image
Shannon🦖

Awesome, thanks for sharing this - such a help for getting started. 🙏

Collapse
 
sinhasamarth profile image
Samarth Sinha

Amazing article dude 👍

Collapse
 
lovepreet_singh profile image
Lovepreet Singh

Thanks

Collapse
 
vikassethi3477 profile image
Vikas K. Sethi

Simple and subtle

Some comments have been hidden by the post's author - find out more