DEV Community

Cover image for Git Simplified: A Beginner guide to git and GitHub
shahabbukhari
shahabbukhari

Posted on

Git Simplified: A Beginner guide to git and GitHub

What git actually is?

Git is the most used tool for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.

Umm... But why do i need Git.?

confused

So here is why

When you are working in a team. you have different task every person is responsible for different task but everyone's work is dependent on each others work.
You have to send files to each-other when ever you changed something you have to send it back to back and maintaining those files becomes a tedious task.
folder list
Here comes the git. Git tracks changes in your files and save different version of files. if you make any crap in your current version you can easily go back to your previous stable version.
if you are working in a team and something goes wrong. It will help you to identity who made the change and where he/she made the change and when they made a change
git history

Prerequisites:

Before we start working on git there are some prerequisites:

  • [x] Install Git:
    • Mac Users click here
    • Windows Users click here
    • Linux Users click here
  • [x] Github Account:
    • If you don't already have one, you can make one

Git Basics:

Before we start working with there are some things you need to know.

Git repository:

Git repository aka repo is like a normal folder but with some Super powers. It saves every change you make in it.

Git Workflow:

Git workflow

If your file is in current working directory(folder). it can be in three states:

  • Working Directory: Working directory is the area where you are currently working. where your files resides. it is also called Working Tree or Untracked Area. every change you make here is not tracked by git.
  • Staging Area: Staging area is when git starts tracking and saving changes occur in file. after saving changes when you make another change in the file git will know where you made a change. with git add filename you can move file from working directory(Untracked Stage) to Staging Area(Tracked Stage).
  • Local Repo: local repository is the place where you save your changes before sending them on remote repository. you can save changes from staging area with git commit -m "message" filename)" .
  • Remote Repo: Remote repository is a place where you Push your code or Pull to update the local version of a repository from a remote repository. its usually Github or any other hosting service for version control.

Git Basic Commands:

start
Now, Its time to get our hands dirty on git commands. Lets Git.

Initilizing Local Repository:

Before you can use any Git super powers first you need a repository(Super Folder). Either you can connect your existing project folder to your git repository. or you can clone one from github.

Clone a repository from github:

First create a repository on github with new button from repository section.
git repo
Now add required details Repository name, Description and so on. then create a repository.
add details
After creating a repository copy HTTP or SSH Address.
Repo Adress
Now clone your repository into your local machine.

git clone address_of_your_repository
Enter fullscreen mode Exit fullscreen mode

Connect Existing project with remote repo:

If you want to connect your existing project to your git repository you can use this.
First initialize a git repository in that folder.

git init
Enter fullscreen mode Exit fullscreen mode

Now connect your existing repository with remote repository.

git remote add remote_name address_of_your_repository
Enter fullscreen mode Exit fullscreen mode

At remote_name you can specify any name you want to specify for your remote repository. usually it's origin.

Add Changes to stage:

To add changes from your working directory to staging area.

git add /path/to/file
Enter fullscreen mode Exit fullscreen mode

You can add all changes at one(if you are lazy like me).

git add .
Enter fullscreen mode Exit fullscreen mode

Commit changes

Now commit(save) the changes you added to your staging area:

git commit -m "Commit Message"
Enter fullscreen mode Exit fullscreen mode

The message in " " is given so that the other users can read the message and see what changes you made.

Check Status of Files:

You can check the status of the files in working directory and the staging area. which files are in staging area and which files or not tracked yet.

git status
Enter fullscreen mode Exit fullscreen mode

git status

Same as git status you can also check how many commits you have made in your git local repository(all the commits made to a repository).

git log
Enter fullscreen mode Exit fullscreen mode

git log

Push Code:

To upload your local repository content to a remote repository(Github). we use.

git push remote_name branch_name 
Enter fullscreen mode Exit fullscreen mode
  • remote_name: usually its origin but in start if you initialize your existing project to git repository you may have added a remote_ name in git remote add remote_name address_of_your_repository.
  • branch_name: its a branch where you want to submit your code. usually its master or main. you can check your branches with git branch it will list all the branches. the one with * before name is your your current branch.

Note: git maintain history of your project when your local repo history doesn't match with remote repo you cannot push your code. It may happen because of some files changes made by you/teammate in remote repository which are not present in local repository.
So when you are working in team/collaborative project you may want to pull changes in to your local working directory.

git pull remote_name branch_name
Enter fullscreen mode Exit fullscreen mode

git pull update the local version of a repository from a remote repository.
Now Push your changes to remote repository.

CongratulationsđŸ¥³

Congratulations, you made your first push to your remote repository.
celebrate

Conclusion:

In this article we have learned what git is, why we need git, some basic git terminologies and some basic git commands.
Stay tuned for the next article on how to work collaboratively on git in a team.

Thanks for reading.

Feel free to connect with me on LinkedIn. If you wanted to check what i am working follow me on Github and Follow my blog for more beginner's centric content(bcz i am not an expert).
Thanks

Top comments (0)