GitHub
In the previous blog, I walked you through the core concepts like what is git, terms related to git, and the basic git commands. If you have not read the previous blog, you can read it here.
In this blog, we will discuss the following topics of Git:
- Introduction to GitHub.
- Creating a remote Repository.
- Concepts related to Git & GitHub
Introduction To GitHub.
GitHub is a web hosting service for software development that allows us to create repositories on the web. It is commonly used for open-source projects. It helps developers to collaborate and work together on a common project.
We first need to create a GitHub account to use it. To get a GitHub account, simply sign up with your Gmail or you can choose to create a free account. Once you are signed in, you should see your dashboard similar to the below image.
Creating A Remote Repository.
To create a new remote repository on GitHub, follow the below steps:
Step 1: Click on the +
symbol in the top-right corner and select New repository.
Step 2: Enter the name of your repository, Give a proper description, there are more fields as shown in the below image but this much is enough for us. Now, click on create repository button.
Step 3: You will see an interface as shown below. At this point, you have successfully created a remote repository.
since we have created a remote repository, we can either clone this in our local machine and start using it or connect the local repo with the remote.
Cloning:
Cloning refers to downloading an entire remote repository to your local machine with all the files and versions present in the remote repository at that moment in time. It is done using the git clone
command. Follow the below steps to clone a repository:
Go to the repository which you want to clone and click on the green-colored dropdown named code and copy the HTTPS URL.
Once you have copied the URL, run the below command followed by the URL.
git clone {URL}
Now your local setup is done! The repo is ready to use.
If you have a local repository, then we can set it to a remote repository using the below commands.
git remote add: This command connects a remote repository with your local repository:
git remote -v: This command returns a list of all the connected remote repositories to your local repository.
git remote remove: This command is used to disconnect a remote repository from your local Repository.
git push: This command is used to move your local code to the remote repository. It copies all the files to the remote repository.
git push origin <branchName>
Once you run the push command, you should see the files on your remote repo.
git fetch: This command is used to update the local repository so that it is on the same level as the remote repository.
For Example, I have created a new file on my remote repo named Lesson4.txt and I added some text to it. Now I want that file in my local repo so that I can work on it locally. Hence I will use the git fetch command to update my local repo.
git fetch <remote>
Ex: git fetch origin
Other concepts Related to Git & GitHub.
.gitignore
Usually, there are many files in a directory that we will want to add to the staging area, so the best option is to run the following command.
git add --all
, but there might also be some files in the directory which we do not want to track or push to the remote repository like the .env file. In such cases, we create a file named .gitignore
. In this file, we list the names of those files that need not be added to the staging area even if we run the command to add all files.
An example of a .gitingnore file is shown below:
The text in green color is the comment and comments are written using # symbol in the .gitignore file. You can find a collection of useful templates of .gitignore files for several languages at this link.
Forking:
Forking is the concept where a user copies a remote repository from another user’s GitHub account to his GitHub account. Forking is different from cloning, in forking the repository copy goes from remote to remote repository while in cloning it is from remote to local. To fork a remote repository, go to the repository and click on the fork tab.
Branching:
Branching is the most important concept in Git, For any task to be done, we should create a new branch. Any changes that are done in this branch will stay in this only. We later push these changes to the root branch.
A branch is basically a pointer that tracks the changes. There is always the main branch in a repo which is the default branch and major changes are committed to this branch only, this branch automatically gets created during the first commit. The changes made to one branch will not be reflected in other branches.
For Example, I created a remote repo for my project to-do list. I have committed the first version of my project to the main branch, Let’s call the main branch as, branch A. Now, let’s assume I want to add a new feature to my project. So I will implement that feature in another branch named branch B which will not affect my main branch.
This is what a branching timeline looks like, you can find your repository branching timeline by navigating to insight>Networks on GitHub. In this, the circular dots indicates commits and the black line is the main branch while the blue and green line are side branches.
Commands related to branching:
git branch: This command is used to check the current branch you are in.
git checkout -b <branchName>: This command is used to create a new branch from the current branch.
git checkout <branchName>: This command is used to switch to another branch
git branch -d <branchName>: This command is used to delete a branch.
Merging.
When one branch combines with another branch then it is called a Merging.
In the above example, I have created two branches A and B in my repository to-do list, where A is the main branch, where I have committed my first version of the project and B is the side branch where I was adding a new feature to my project. Let’s assume, I am done with my feature and now I want to include it in the main branch, Hence I will perform merging and I will merge branch B with Branch A, which is the main branch.
Commands related to merging:
git merge: This command is used to merge one branch to another branch.
In the above example, I first made a new commit to a branch called newBranch
and then switched to the master
branch using the checkout command, and then I merged the changes of newBranch
to the master
branch, using git merge.