Git & Github: All you need to know

Git & Github: All you need to know

Hello amazing folks 👋

So, it seems like you just started learning coding and heard the word 'Github' from those geeks. Worried? what if they ask "hey so you code! lemme see your github profile" and you dont know what that is. no problem. finally you're here to learn, and i got you.

we will see some basic and most useful commands in Git.


What will be covering in this Blog

- What is Git?
- What is GitHub?
- Git basic terminology
- Git installation
- Git configuration
- Git commands:- Working With Local Repositories
- Git commands:- Working with Local Branch
- Git commands:- Working With Remote Repositories
- Conclusion


What is Git?

Git is a well-known DevOps tool used for source code management. It is a free and open-source version control system used to handle small and large projects efficiently. A version control system is a type of software that records changes to our files overtime and keep track of each and every modification. You can recall any specific version of those files at any given time on Git, which can save a lot of your time. Git also allows many people to collaborate on a project and they all can have their own version of project files on their computer. Assume if you're working on a project and after all the changes you made you now want to get back the first version of your project, 'But how can you revert back now?'. No worries, Git have your old version of project and all the changes you made in his pocket. You must have heard about GitHub or GitLab. Those are just cloud-based hosting services that let you manage Git. In this article, We'll be exploring Github further.


What is Github?

Github is a cloud based service that Git provides. You can store all your project on Github and it will be there hosted on cloud. Every project on github has it's own repository and it's own unique Url. You can also collaborate with all your team members, see their code, fork their code, modify their code and push into your own code branch. You might not be familiar with some terms you just read, So let me explain.


Basic terminology

Before you get started with Git commands, you need to understand some important terms:

1.) Repository(Repo):-
A repository is like a storage space or a folder for your project. It contains all files such as your code, images, documents and many more. It also tracks all the changes that you made or your collaboration. So that you can always revert previous version of your project.

2.) Commit:-
When you change version of your project, you have to commit. When you're done making a particular change and now you want to save, here you make a commit and save it in your github. You ever heard of 'checkpoints' in games? it is exactly like that. And always write good commit messages so that you know what changes you made in that particular commit.

3.) Branch:-
A series of commits of a particular project indicates a branch. Master is the default name of a repository’s active development branch. It's the primary branch of all your repositories. A repository can have multiple branches. The branch could be the one where there are commits for all new features; another branch for commits related to bug fixes and so on.

4.) Fork:-
A fork is a rough copy of a repository. A Forked repository saves in your own system and it allows you to freely debug and make your own changes without affecting the original project.

5.) Merge:-
Merging is used to combine multiple commits into one branch. If you like changes that your team member made that solves your problem, you can merge their branch into your own.

6.) Pull:-
The term Pull is used to fetch data from GitHub. And you can merge changes on the remote server to your working directory. The git pull command is used to make a Git pull.

7.) Push:-
The process of uploading the git repository in your local system to Github is called pushing. You can overwrite changes after pushing. In Git, "Origin" word is used to push to your main branch which is Master.


Git installation

typing.gif

Install Latest Version of Git

To verify if Git is installed in your system or not, use this command in The Terminal:

git --version

If this command returns the version of git in floating digits then Git has been successfully installed on your system.

Ready to get started? Let’s learn how this all works-


Git Configuration

To set a name that is identifiable for credit when reviewing version history:

git config --global user.name "[username]"

To set an email address that will be associated with each history marker:

git config --global user.email "[valid-email]"


Working With Local Repositories

1.) Create a new Folder you can make a new folder in your local system and change the directory('cd') or can use this command.

mkdir test

2.) Initialize Git Repository

git init

3.) Create Readme file README files are written in plain text. They usually include paragraphs describing the project or direction on how to use it.

cat > Readme.md

4.) Staging To stage a file is simply to prepare it finely for a commit. Git, with its index allows you to commit only certain parts of the changes you've done since the last commit.

git add <filename>

5.) Committing staged files This command saves your changes to your local repository.

git commit -m "Commit message"

6.) Remove file

git rm <file_name>


Working with Local Branch

1.) Create a branch

git branch <branch_name>

2.) Display all branches You can use this his command to list all remote or local branches.

git branch -a

3.) Delete a branch

git branch -d <branch_name>

4.) Delete remote branch

git push origin –delete [branchName]

5.) Checkout an particular branch

git checkout -b <new_branch_name>

6.) Merge a branch into an active branch

git merge <branch_name>


Working With Remote Repositories

First, go to your GitHub profile and create a new Repository. If you are not familiar with how to work on GitHub Watch this . Once the Repository is created, you will be able to see a page somewhat similar to the following screenshot :

image.png

Here you can see your Repository URL. Copy this URL and add in the below command to add the remote to the origin.

1.) add git remote

git remote add origin <repository url>

2.) add git clone This command is used to create a local working copy of an existing remote repository. Copy your repository URL and add in the below command to clone the repository to your local computer.

git clone <remote url>

3.) push git repository This command is used to push all the code from the local repository to the remote repository.

git push -u origin master

4.) pull git repository The git pull command is used to fetch and merge all the latest changes from the remote repository to the local repository.

git pull origin master


Conclusion:-

Hope this tutorial has helped you understand Git and Github. You have also learned various Commands that are used. And remember to always keep your Github profile updated.

Thanks for reading 😃

I would ❤ to connect with you at Twitter | LinkedIn | GitHub

Share your queries in the comments section.

Useful Resources