Introduction to Github and it’s usage
What is Github ?
GitHub is one of the world’s largest community of developers. It uses Git as it’s underlying technology. To know the difference between Git and Github you can start from here. Github allows collaboration and communication between developers. It has a number of useful features that enable development teams to work together on the same project and easily create new versions of software without disrupting the current versions, but it doesn’t stop there.
Once new additions to a program are complete, for example, they can easily be incorporated into existing programs. GitHub also makes it extremely simple to work together on strings of code to really dial in and perfect even the smallest parts of a program. With GitHub, you can collaborate and work on projects with others anywhere in the world.
Of course, that’s only scratching the surface, because when it comes to the question “What is GitHub?” the answer is all about what it can do for you.
It’s Benefits:
- It makes it easy to contribute to your open source projects.
- Proper Documentation.
- Track changes in your code across versions.
- Various Integration options.
- Both Graphical User Interface(GUI) and Command Line approach available.
- Platform to showcase your work.
How do I use Github ?
Basically, github is used for many different purposes related to coding but since it’s your first time using it. You will just need to understand some basic concepts of it’s usage and you are good to go. Let’s quickly dive into it’s installation now.
1. Git Installation
Firstly, you have to install Git on your system which a mandatory part for the working of Github. To install git:
- Visit git download page shown above.
- Choose your operating system from the options provided and press download.
- It will automatically install git on your system.
- To check if git is installed on your system type
git
in your command prompt. If it’s successfully installed you will see an output like the image below.
2. Sign Up on Github
You can create a free account on Github here. It’s a pretty straight forward process, you just have to enter your details like Email address, Name, Birth Date etc.
You are all set to use github now. Let’s discuss managing your code using basic github commands.
Github operates by creating two repositories that is Local and Remote. Local is the one present on user’s machine and Remote is stored on Github servers which is accessible from anywhere from the world. Now let’s move ahead on creating them…🏃
3. Creating a remote repository on Github
You can just tap on Create Repository on your Github profile which you have created at the start of the article.
You have to fill some details such as repository name and mark if you want the repository to be public or private. After that you are good to go just tap the create button and Voila 😲 your brand new repository is created. 🚀
4. Cloning the remote repository on your local machine
Type the command git clone <repository-url>
to fetch your remote repository on your local machine. In this case it will be like
A new folder with a name same as your repository name will we created after the cloning process is completed. This will be the area where you will store your project files and git will keep a track of those files which we will be seeing further.
5. Pushing the changes to remote repository
Firstly we need to track the changes we made on the local repository which is possible with the command git status
. Make sure you cd <repo-folder>
before performing these commands.
As we can see we have created some new files(consider these are your coded project files 😜) git has tracked these files and it says that these changes are not present on the remote repository.
Pushing the changes to the remote repository is a 3 step process :
1. git add .
2. git commit -m "My First Commit"
3. git push
git add
This function is going to stage a commit by adding the changed files to a commit buffer. If we want to add a specific file to a commit we can just writegit add <filename>
instead ofgit add .
which basically adds all the changed files to the staged commit.
git commit
This function will allow us to give a message value to our commit, mostly users prefer to write about the code changes they have made in the respective commit as it’s message value.
git push
Git push is finally going to submit all your changed files to the remote repository and hence will make the remote repository up to date with all the recent changes from the local side.
As we can see below after a successful git push
the remote repository will get updated and the files which we had created are now on remote repository Yay💃!!
6. Pulling the changes to local repository
Suppose your remote repository has been changed by some other person and you want to make sure you have all the recent changes updated on your local repository before you start editing the code then git pull
command comes in very handy rather than cloning the whole repository again which is a lengthy process.
I have created a git-pull-demo.txt
on remote repository which is not available on the local repository.
I will be performing a git pull
command and let the magic begin 🎉
As we can see all the remote changes have been reflected back on our local repository and you are good to go… 👍
What is .gitignore File ?
Git ignore allows the users to restrict some files present on local repo(short for repository) from being pushed onto the remote repo. Mainly these files contain some critical data such as API keys, Software keys etc.
Some people also avoid the folders which contain images/videos for the project that are very heavy on their size and usually slow down the process of pushing your code on the repo. Now let’s it’s actual working.
- Create a filed named
.gitignore
in your project folder. - Add all the files which you want to be ignored getting pushed on the remote repo.
- And that’s pretty much it. You are good to go!! 👋
As you can see in this image as soon as we add the file file-to-ignore.txt
to the .gitignore
file, git changes its focus and doesn’t track the file listed under .gitignore . That’s the basic function of gitignore file.
What is a Conflict?
As we know there are many developers working simultaneously on a same project. If the work on the same file and suppose “Person B” performs a git push
operation before “Person A” then “Person A” is going to get a conflict as shown in the image below.
Now “Person A” has to resolve the conflict by discussing it with the other person weather which code change to keep. After coming to the conclusion “Person A” will perform a git push
by resolving the conflict and will just keep the change which is recent and necessary.
We should be always careful while working on the same file and have to resolve the conflicts, if not they can sometimes cause some old changes to remain in our code.
What are Branches?
As the name suggests branches are used to isolate development work without affecting other branches in the repository. Each repository has one default branch, and can have multiple other branches. You can merge a branch into another branch using a pull request
.
As we can see once a feature is added to the main branch using a pull request
. That branch will then be merged with the main branch. After that we can simply delete the branch because all the changes will get reflected into main branch and it will just be another cloned branch.
It allows user’s not to disturb the ongoing development on the main branch and if there are some major issues in a branch we can always create a new one by deleting it.
Working with branches:
- Creating a branch:
git branch <branch-name>
will create a new branch.
git branch <branch-name>
will create a new branch.
git branch -a
will list all the branches present in a repository.
git checkout <branch-name>
will be used to switch branches
2. Merging a branch:
git merge <branch-name>
will merge the branch with main branch
3. Delete a branch:
git branch -d <branch-name>
will delete the branch