Getting started with Git and link with GitHub Repository

Git is very popular version control system. It's free and open source system designed to handle small or larger projects efficiently. There are several option to install git based on your device, operating system. Along with the Git you need to know about GitHub which helps to backup your project online. Git and GitHub together help in better manage of the projects. 

Getting started with Git and link with GitHub Repository
Git and GitHub logos

Although there are other cloud based hosting service but I would recommend to have account in GitHub before starting with Git. You can still use Git in absence of GitHub but to get track of your work from any device and to keep backup your project in online either privately or publicly.

How to create GitHub Account?

  • It's easy. Simply you can search GitHub in your favorite browser and get into its website or directly go to github.com.
  • If you don't have GitHub Account you can create one clicking on 'Sign Up' button. If already have an account you can click on 'Sign in'.
  • Creating GitHub account
    Creating GitHub Account
  • While signing up you have to give your email, password, username and click on continue.
  • You need to solve puzzle to check you are not Robot and then click on 'Create account'.
  • You can verify your email by clicking on link sent to you and you can sign in with that email and password to begin with the GitHub.
  • You will soon get used to it if you haven't used it to this date. 
  • You can create your first repositories simply clicking on 'New repository' from dropdown menu in + sign nearby your profile picture. Depending on page there may be the button for New repository in multiple spot.
  • Creating GitHub repository
    Creating GitHub Repository
  • Once you click on 'New repository'. You have to fill up the form giving project name, description, selecting private/public and default files to create while starting up. Recommend to make private repository if you are in phase of leaning as you don't want to share the code publicly. If you are working with the larger group then you can make public otherwise in small group of 2-3 you can invite friend for collaboration in private repository.
  • Once you click on create repository you have made it. It would provide a link to your repository. The link is useful for downloading the project on local device and work offline. The link help to share your projects to others if you like to for that repository should be public. You can change private repository into public repository at any time you wish.

Now, lets get into Git.

How to install Git in your Device?

Search Git in your browser and click on its site. Or to the link: https://git-scm.com/

In this site you can download Git for your device depending on your device you can select one.

When downloaded you can proceed to install. While installing just be patience you have to go through multiple windows. Just you have to confirm in each steps, most of are default setting. Somewhere you can wisely select options. In this article I am not focusing on the steps to install but on view basic Git command after installation.

How to check whether Git Installed in your device?

Search on your device typing 'Git'. You may find Git Bash, Git CMD, Git GUI if the installation is OK.

You can open Git Bash (recommended) or Window PowerShell to begin using git.

Checkout your git version with the code:


    $ git --version


You have to give your email and username before beginning project with the Git. It is required to keep track of commit made by the user. Username and email distinguish users making commit. It would better use same email used in GitHub and the username for your convient. The command for the task is as shown;

    $ git config --global user.name "Amrit Devkota"
    $ git config --global user.email "devkota@bloggernepal.com"
    $ git config --list                (Check)


How to link local project with the Git?

Create your working folder for the project with the project name. Let's say git_test folder created. Inside the folder you create an index.html file using your favorite editor. -VS Code recommended.
Once html file prepared. You can open the folder and right click which provides shortcut to open Git Bash in that directory. 

You can begin git their with code: (checking status --optional; here we are doing to check the stages of file change)


    $ git init
    $ git status


Init begins Git functions on the folder. Begin recording each changes. status checks are the files saved, changed, or modified, etc. When the file are added and are not added then status would gives the list of files added, modified with red fonts. You can add them into record. When you add file moves to staging area. You can add using code as shown below. '.' after add with space means adding all changes.


    $ git add .
    $ git status

When you check status after adding up. All the files shown up into green means file get into staging stage.
You can now commit and save the changes.

    $ git commit -m "First commit"
    $ git status

For now you can simply say 'first commit'. Later on, if the changes you made don't work later on you can come back to previous changes. Commit should reflect what have been changed in the files. 
Checking status after committing the change would say 'Nothing to commit'.

You can repeat the above process changing the files in your project. Make chages in files -- add to the git -- commit with message and check status to verify if it works.

When you do multiple commits let's say more than 1 for now. You can view details about the commit you made with the code.

    $ git log
    $ git log --oneline

git log gives details with author name, email, time of commit, commit, etc. whereas git log --oneline would give only list of commit with commit id and first line of commit you made previously. 

Let's assume you have made few commits. And you realize you have made mistake in last commit and you like to move back to the changes commit stage. Then you can revert back to that commit with the code checkout commitid  (lets say 943efef). CommitId followed by filename would only revert back the changes on that particular file. Then you have to save the revert back changes as if its' a fresh change with commit.

    $ git checkout 943efef
    $ git checkout 943efef index.html


Sometimes you revert back to previous commit but found that many things to add up so, you may don't want that revert back. You want to go to the latest commit whole or for particular file. when checking status you may find some changes after last commit and if you don't want any changes after last commit then you can -- checkout.

    $ git reset HEAD (or)
    $ git reset HEAD index.html
    $ git status (modified)
    $ git checkout -- index.html (remove changes in the file)
    $ git status (nothing to commit)


When you have already added the file. The file in staged state and you want to upstaged  a staged file without changing/disturbing changes in working directory. The code are for particular file and all the project.
    
    $ git reset <file>
    $ git reset



How to link Git with GitHub?

We have created a repository in GitHub and we have local project connected with Git. Git is working fine locally. Connecting with GitHub make more easier and accessible to multiple device and for collaboration among many people and sharing the project online for the public or to save for personnel use for the future.

If we like to connect to the created GitHub repository then, we should have copy the link of that repositories. Then, in the Git Bash we would write line of code. Second line of code command to push the local git repository to the origin to the master/main branch in GitHub. In master/main (depends on installation setting) 
    
    $ git remote add origin https://github.com/AmritDevkota/git_github_test.git
    $ git push -u origin master (or)
    $ git push -u origin main

For the first time pushing into GitHub from the device asked for your GitHub Username and password.

How to clone GitHub repository into local directory?

Here, we are copying the remote GitHub repository into local directory. It's not like uploading local Git repository into GitHub repository but follows different method. Here, you still need the link to the GitHub repository that we are going to clone in local directory. We open GitBash in the folder where we want to clone the repo. GitBash is open by; right click -- Open Git Bash 
In the code; Use GitHub Repository link of your own. 
    
    $ git clone https://github.com/AmritDevkota/git_github_test.git


Now, you would see new folder created. You can get into the directory and work in each file and make changes. That changes can be saved with proper commit following the above steps. That all steps repeated from 'git init' that initiate Git to newly created project.

Hope this article would be helpful to you. If you have any trouble please free to share and give chance to improve the lacking part.

0 Comments