Explanation To Git And Github

Explanation To Git And Github

What are git and Git Hub?

Let's say you have one application and you made some changes to the code sample of that application but you want your application as same as it was at the beginning and you might be wondering if there would be some sort of way that I can go back to my project and click the picture of that initial code sample.

Git is the DevOps tool used for source code management and used for maintaining the history of the project.

Git Hub is the largest open-source platform that helps to host our git repository. Basically, the git repository is the sort of folder where all the changes are saved

Download the git

Click the below URL and download it based on your operating system

Download git

Once you are done, open the git then run the command git and if you get below output then you successfully set up git.

How to create the repository in the git hub?

open the below link and sign up to the git hub

Login to GitHub

Click on the " + " symbol in the top-right corner and click on New Repository.

After that put any name to the repository, you can make the repository private or public and you can also add a Readme file. Finally, click on the create repository button

Steps to push the file on the git hub?

Here, I am creating the hashnode directory and inside that directory, i have created file.txt and added some content to that file and I want to push that file in my git repository.

Step 1: Initialize the Repository

git init

Step 2: Add the file.txt

git add file.txt

Step 3: Commit all the changes that happen within a directory ( It means our file is ready for push )

git commit -m " file.txt added "

Step 4: Connect the Git repository to your local

git remote add origin git_repogistry_URL

Step 5: Right now, I am pushing to the master branch

git push origin master

Now you can check your git repository and you can see the file.txt file in your repository.

Few important commands in git

log

Using this command you can see the history of the project

git log

status

If you change the content of textfile and you wanna see the status of your project then use the below command

git status

Stashing changes, poping stash and clearing stash.

Let's take the example you are working on a project and made some changes to the code base but you don't want to commit that changes and put that changes in the stashing area and want your project with a clean code base then at that certain you can use stash command

git stash

Now, you want to retrieve that changes from the stash area then use the below command

git stash pop

if you want to delete changes from the stashing area then use the below command

git stash clear

Forking other's projects. Why do we create a fork? How to clone other's projects in your local?

There are so many open-source projects in the current world and if you want to contribute to that project or you wanna made some changes to the code base regarding any bug fixing, version update or security then you can't directly make changes to the original project what else you can do, make the fork to own repository, clone that projects to your local using below command.

git clone fork_repository_URl

After cloning, make the changes regarding any issue and push that changes in the git hub via a pull request and the project owner check your code if all thing is ok then your code will be merged and available in the main branch.

What are pull requests and branches? Why do we never commit to the main branch?

Pull requests let you tell about changes you've pushed to a branch in a repository on GitHub and Branches allow you to develop features, fix, bugs, or safety experiments with new ideas in a contained area of your repository.

We should never commit in the main branch cause it doesn't allow more than one pull request ( In simple terms, 1 branch = 1 pull request). let's say you are working on 100 different new features and you pushed those 100 changes in a single pull request (PR) then how it would difficult to review and merged your code? So that, for any new feature you are working on create a new branch.

How to create a new branch?

git checkout -b branch_name

This command creates a new branch and switches the head to that new branch.

Squashing many commit to a single using rebase command.

Here for example you have 4 commits for 4 files.

Then if you want to merge the 2nd,3rd and 4th commits to a single then use the below command

git rebase -i file_1_commit_hashid

After running this you will be directed to the below interface and you can either pick or squash. Here whatever above s(squash) merged commit to the above one. In the below example, the 3rd and 4th commits are merged to the 2nd commit.

After this, you can modify the commit message like "commit merged"

Then exit from the editor and run git log command

What is merged conflict and how solve that problem?

Let's say two people Anish and Bijay made changes at line number 5. While merging the pull request merged conflict occurs and at that time git will be confused that whose changes should i take.

So, we solve this problem manually that which changes we want.