Introduction to Git and Github

In this article , we will be learning about git and github from scratch.

All the important topics you should know about git will be covered here in step-by-step manner.

🔥Let’s start

What are Git and Github?

What is version control

Version control is a system that help to keep track of every changes made in the codebase, hence making development across differnet team, environment and platform faster.

What is Repository

A Repository or repo refers to a central location where data is stored and managed
Local Repository Remote Repository
It is present on your computerIt is present on server like Github, Gitlab, etc
Created by you (cloned)Created and managed by organization
You can freely modify without any approvalYou can raise a PR, once approved than your change are seen
You have all the accessYou have very little or no access
Risk of data lossMore resilient to data loss, backups available

What is GIT?

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

What is GitHub?

Github is not git. Github is a web based git repository where the source code are hosted on the cloud.

Software required for git

  • Create an account on GitHub
  • Git (Link)

Once git is installed successfully, go to cmd and check your git version :

 git --version

Configuring Git

Once you install git for the first time, you need to configure git with your name and email.

Note – This is only a one-time setup

 > git config --global user.name "your name"
 > git config --global user.email "your@example.com"

The global keyword mean here is that git will be configured in all your future git setups as well, and you don’t need to configure every time you initiate a new git repo

Now, verify your configuration setting –

git config --global --list

Git Workflow

What is a Repository ?

repository a.k.a. repo is nothing but a collection of source code.

There are four fundamental elements in the Git Workflow.

  • Working Directory
  • Staging Area
  • Local Repository 
  • and Remote Repository.

Important github commands

git add is a command used to add a file that is in the working directory to the staging area.

git commit is a command used to add all files that are staged to the local repository.

git push is a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.

git fetch is a command used to get files from the remote repository to the local repository but not into the working directory.

git merge is a command used to get the files from the local repository into the working directory.

git pull command used to get files from the remote repository directly into the working directory. It is equivalent to a git fetch and a git merge .

git revert

git hard –reset command used to reset the current branch to a previous commit, discarding any local changes and modifications made to the files.

git status command displays the state of the working directory and the staging area

git log command displays committed snapshots.

git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on.

git rebase  allows you to integrate the changes from one branch into another.

git clone is primarily used to clone the repo present in remote repository

Create your first git repo

  • Create a GitHub account
  • Create your first GitHub repository
  • Add the following code
git init
git add .
git status
git commit -m "first commit"
git branch -M main
git remote 
git remote add origin https://github.com/<username>/<reponame>.git
git push -u origin main

Adding changes to the existing repo

git add index.html
git commit -m "content added"
git push

Creating your git branch [imp]

Git branches are effectively a pointer to a snapshot of your changes

Branch can be created by 2 ways –

  1. A brach is created in your local and then it pushed to your remote repo
  2. A branch is created at your remote repo and pulled it to your local
git checkout -b <your_branch_name>

Check git status

git status / git logs

Let’s understand the flow of branch

  • First, let’s say you made some changes to your local branch
  • Then you push those changes to your branch and present the remote repo
  • After that, you create a pull request to the main branch
  • Once the PR is approved, your changes are reflected in the main/dev.

Keeping branch up to date with main

Let’s say somebody else have pushed the code in main and you want your local branch to be updated with the main, you need to take a pull from main

1st way

git checkout main
git pull
git checkout <your_branch>
git merge origin/main
git push

2nd Way

directly pulling the changes done in remote repo to local branch
-----------------------------------------------------------------
git pull origin main 

push local branch changes to remote branch
--------------------------------------------
git push

Creating and resolving git conflicts [imp]

If a specific line has been modified in multiple branches, an error with conflict will be encountered when attempting to merge the branches.

Once you find the conflict, you can do 3 things –

1. Abort Git conflict

You can abort all the changes , meaning both the branches will go one step back, as it was before merge conflict.

git merge --abort

2. Reset your git base

Undo all your change so that it won’t create conflict when you merge

git reset --hard

3. Resolve conflict

Take the pain, and resolve the coflicted file manually

 - remove the conflited line
-  commit the code
-  and push 

Git Revert

Undo the recent change

git revert <commit_id>

Git stash

git stash
git stash pop

Conclusion

Help Others

Leave a Reply

Your email address will not be published. Required fields are marked *