Basic Information About Git And GitHub

Basic Information About Git And GitHub

In this article we will learn about Git and GitHub. This article will be helpful for those who are learning about Git and GitHub first time.

Introduction to Git And GitHub

What is Git

Git is a version control system . It is a software which is used to track changes in our files and folders in our system and used to collaborate with other developers. It is used to manage the history of your code and to merge changes from different branches. You can click on this image to download git software.

Git Logo png images | PNGEgg

What is GitHub

GitHub is a web-based hosting service for Git repositories. GitHub is an online platform that allows you to store and share your code with others.

GitHub - genuinetools/img: Standalone, daemon-less ...

What is VCS

VCS stands for Version Control System. It is a system which is used to manage the History of our codes. Version Control System is like a checkpoint in a game. Some common version control systems are Subversion , CVS, and Perforce.

All about Version Control – Git

How to Install Git

To Install git to our system (windows, macOS and Linux) we can visit official website of git git-scm.com/downloads and download the software . It is very simple to install git software but is you find any problem go search YouTube videos on how to install git.

Note - It is recommended to create an GitHub account.


Basic Terminology and commands Used in Git

How to Check git version

To check git version installed in our system we have to use the following command in git bash/vs code terminal

git --version

What is a repository

Repository is a collection of files and directories that are stored together. It is a way to store and manage our code.

How to check the current state of our repository

To check the current state of our repository we have to use this command.

git status
  • before using git status use pwd command to look your present working directory.

  • cd <folder name> command is used to open the folder.

Config Settings

It is important to config our user name and email id in git . Use following commands -

git config --global user.name "Username"
git config --global user.email "email id"

If you want to check your config settings use this command

git config --list

How to create a repository

Creating a new repository is a process of just like creating a new folder and initializing is as a new git repository . In simple words we are asking git to track it.

We can use the following command to create a new repository

 git status
git init
  • git status command will show us the current state of our repository.

  • git init command will create a new folder on our system and initialize this folder as a git repository. git init command adds a new hidden folder to our system known as .git folder.

What is Staging

It is a way to tell git to track a particular file or folder. Before staging a file it will show U letter which means file is untracked. After staging the file it will show A letter which means Index Added.

We have to use following commands in case we want to stage single files or multiple files.

git add <filename>
git add .
  • git add file name this command is used to stage single files.

  • git add . this command is used to stage multiple files.

What is Commit

It is a way to save the changes to our repository. It is a way to record the changes and make them permanent. To commit a file/files we have to use the following commands.

git commit -m "Message"
git status
  • -m flag is used to add a message to the commit. This message is a short description of the changes that were made.

Git Log

This command is used to show the history of our repository. This command will show us all the commits that were made to the repository.

git log

If your are not able to see the command line prompt use q letter.

git log --oneline
  • --oneline flag is used to show only the commit message.

What is gitignore

Gitignore is a file that tells the git which files and folders to ignore from tracking. To do this we have to create a .gitignore file and add list of files and folders to .gitignore .


Git Branches and Conflicts

Git Branches

Branches are a way to work on different projects or topics at a same time. Git allows the developers to work on different files independently from the main branch.

Git Branch | Atlassian Git Tutorial

HEAD in Git

Head is just like a pointer which shows us the current branch on which we are working at the time. It points to the latest commit in current branch.

How to create a new branch

To create a new branch we have to use the following command

git branch
git branch <branch name>
git log
  • git branch command will list all the branches in the current repository

  • git branch <branch name> This command will create a new branch.

  • git log command shows the commit history for the current branch

How to switch between branches

To switch between branches we have to use the following commands

git branch
git switch <branch name>
git checkout <branch name>

we can use both git switch or git checkout to switch between branches. But it is recommended to use git branch command to check out the present branch.

Merge Branches

Merging branches in git is the process of combining features , fixes or updates from different branches to the main branch. There are two ways to merge branches

  1. Fast-forward Merge - In this type of merge we can merge the branches to the main branch after completing the work on branches and commit them but there should be no work done on the main branch. In this case there will be no conflict while merging the branches to the main branch.

Fast-forward merges in Bitbucket Cloud - and by default, if you like - Work  Life by Atlassian

  1. Not Fast -Forward Merge

In this type of merge there is work done in both master and other branches and have commits. Master branch have commits which are not present in other branches. When we try to merge these type of branches there are conflicts. To solve them we have to manually resolve these conflicts by deciding what to keep and what to discard.

Vs code has a built in merge tool that can help us to resolve the conflicts. This is called not fast-forward merge.

From Micro to Macro. A custom git branching model and… | by Natan  Braslavski | Better Programming

Rename a Branch

We have to use the following command if we want to rename a branch.

git branch -m <old branch name> <new branch name>

Delete a Branch

We have to use the following command if we want to delete a branch.

git branch -d <branch name>

Pushing Code to GitHub

After doing our work we have to maintain our data in GitHub. It is a popular platform for developers to collaborate on projects and to share code. To do so we have to create a GitHub account which is a simple process . After Creating a GitHub account we have to follow the following steps.

step 1. configure our config file in git bash

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

Now you can check your config settings:

git config --list

This command will show us al the settings we have changed.

step 2. Setup ssh key and add to GitHub account

a. Generate a ssh key by using the git bash terminal and using this command

ssh-keygen -t ed25519 -C "your-email@chaicode.com"

After generating the key we have to save it in our computer.

b. After saving the ssh key we have to add it to the GitHub account ssh section to link it with our GitHub account.

This will complete the process of generating ssh key and linking it to the GitHub account.

Adding Code to remote repository

Now that we have setup our ssh key and added it to your github account, we can start pushing our code to the remote repository.

Step 1.

We have to create a new repo in our system add some content and then commit it by using these commands

git init
git add file name
git commit -m "commit message"

Step 2. We have to create a new git repository in our GitHub Account.

Step 3. After creating a git repository we have to copy the https link generated by github and use these steps in vs code terminal.

git branch -M main
git remote -v
git push -u origin main <remote url>

a. git branch -M main

b. git remote -v (This will show us the remote URL of our repository.)

c. git remote add origin <link generated to be added here>

d. use these commands to check

  • git remote -v (fetch and pull request link)

  • git branch -M main

  • git push -u origin main (This will set up an upstream remote and push code to the remote repository. This will allow us to run future commands like git pull and git push without specifying the remote name.)

These commands will push our code to git repository.

After completing these process we can start working on our project . After completing our work we have to only use this command to push our code to GitHub.

git push

Get the code from remote repository

There are two ways to get code from a remote repository:

  • fetch the code (Fetch the code means that we are going to download the code from the remote repository to your local repository.)

  • pull the code (Pull the code means that we are going to download the code from the remote repository and merge it with your local repository.)

To fetch the code from a remote repository we can use the following command.

git fetch <remote-name>
  • here remote name means the name of the remote git repository that we want to fetch the data from.

To pull the code from a remote repository we can use the following command.

git pull origin main

Conclusion

Introduction to Git and GitHub provides a comprehensive overview of Git, a version control system used to track code changes, and GitHub, a platform for hosting and sharing code repositories. The article covers key concepts such as repositories, staging, committing, and logging changes. It explains how to install Git, configure user settings, and create repositories. The article also dives into branch management, including creating, switching, merging, renaming, and deleting branches, along with handling merge conflicts. Additionally, it guides on pushing code to GitHub, setting up SSH keys, and fetching or pulling code from remote repositories.