Git & Github

Starting with git and github

ยท

8 min read

Git & Github

What is the difference between GIT and GITHUB ???

GIT:

Its a command line tool to keep track of changes we make to our code locally in our system.

GITHUB:

GitHub in simple words, it is a online version of a repository or folder.

For Example, Think if some other person need to do changes to your code. Then you have share the code folder to him/her and he/she do some changes which you are not aware of it. Its ok when its a single person you can call him/her and ask about changes. but , this way doesn't work when you work with more than thousands of people . so, we are in need to host our code somewhere online so everyone can able to access it and do changes which is visible to everyone. This is where GitHub comes into action.

The Table given below gives you, more introduction about Git and GitHub.

GITGITHUB
Git is a software.GitHub is a service.
Git is a version control system to manage source code history (Local Machine)GitHub is a hosting service for Git repositories.
Git is maintained by Linux.GitHub is maintained by Microsoft.
Git is installed locally on the system.GitHub is hosted on the web.
Git has no user management feature.GitHub has a built-in user management feature.
Git is open-source licensed.GitHub includes a free-tier and pay-for-use tier.

Install Git

Install Git based on your operating system.

Configure GitHub

  1. Go to the below link and create your account with GitHub
    • After creating your account go to your GitHub profile icon-> Settings-> developer settings -> Personal access token -> Generate token.
    • It will ask for password, give the GitHub password which you used to create account on GitHub.
    • It will take you to New Personal access token page -> Add Note -> Give Expiration days(default-30days) -> select all check boxes in select scope -> Generate Token.(NOTE: you should use this token as password when you are pushing code from git to github.)
  2. After generating token you can go and create your repository on GitHub by following steps:
    • Click on New button on the GitHub page.
    • add Repository name (accepts no special characters except _ & -. if you give any special characters in your Repository name it converts into "-")) and give description about repository.
    • choose your repository public(visible to all) or private(visible to you and the users whom you decide to see your repository)
    • Click on create repository. Your repository will be created and set of instructions will be given. you can execute the commands line by line in your git terminal. you will know more about what those command does after going through git commands section.

Configure Git

  • After installing Git on your machine do the following :
    • Open git terminal on your vs code or open git bash on your workspace location and enter the following commands
      • git config --global user.name "username"
        

        username- can be any name not necessary to give username of your git hub account.

      • git config --global user.email "youremail@gmail.com"
        

        give your email id used for account creation on github

GIT Commands

  1. git init;
    
    This is a initial command which says git to keep track of current working folder(use this command in current working folder which you are going to move it to the github repository)
  2. git status;
    
    It will list files modified recently which is not commited yet(in red color)
  3. git remote add origin <repo url>
    

    By this command simply you are adding the location of your remote repository where you wish to push/pull your files to/from.

    • To verify remote is set properly or not . you can use any one of the command git remote -v

    OR

    git remote get-url origin

  4. git remote set-url origin git@github.com:User/UserRepo.git
    

    If at any stage you wish to change the location of your repository you can use this command

  5. git clone <repo-url>
    
    This command is used to download existing central repository from github to your system(local repository).
  6. git add <filename>
    
    This command is used to add the un-committed file changes to commit stage.
  7. git add .
    
    This command is used to add all the un-committed changes to commit stage.
  8. git commit -m "message"
    
    This command is used to save a snapshot of current state of repository. as soon as you execute this command a commit hash id(unique) will be created by git. This commit hash id helps us to move from current state of working repository to previous state or succeeding state of commits. We can move to specific committed state or original state using git reset commands.
  9. git commit -am "message"
    
    This command is used to combine both commands git add . and git commit -m "message" into single command.
  10. git log
    
    This command is used to log all the commits made so far. It shows below details.
    • commit: **(commit hash)
    • Author: username (with mail id)
    • date: date of commit made
    • commit message given by time of commit
  11. git push
    

    OR

    git push -u origin master/main
    

    This command used to push your changes from local repository to remote repository(github). Note: while pushing you will be prompted to enter username and password(

    • use username as given in git config --global user . name "username"
    • use the password generated from GitHub Personal access token )
  12. git pull
    
    This command is used to pull the most recent changes from central repository and merge it your local repository with changes made from your side. so that when you push you and code on github is on same level. but, you will be one step ahead of central repository with your changes (By default git has ability to merge files automatically until a merge conflict happens)

Merge Conflicts

when does Merge conflict occurs? Ans: If you and some developer making change to same line of code with different values then, Merge conflict occurs. That time git gets confused and throws you a merge conflict message like below

<<<<<<Head
a=10;    //Your code
====================
a=12;    // some one code
>>>>>> commit hash id (Incoming changes)

In order to resolve this merge conflict, remove conflict markers(<,>,=) and decide what you want to go through(10 or 12 as per example ) NOTE: (Merge conflicts can be occurred when merging branches too. Resolve it based on what you want ).

  1. git reset
    

    This command is used to go backward or forward state of commits made.

    git reset

    Its a hard reset that is used to undo local changes and takes you to the state of a Git repo(central)

    git reset --hard <commit hash>

    It takes you to the commit state with respect commit hash mentioned.

    git reset --hard origin/master(or)main

    It takes back to the state of central repository.

Branching commands:

Branch is nothing but a copy of a repository. For example, you are working to add feature to your website. you don't want to test it on main branch. because, you may forget what are changes made so if it breaks entire website you don't know what you need to undo. In this situation here comes branching (A branch is nothing a exact copy of your repository where you can test your changes if it works you can merge it otherwise you can ignore or remove that branch)

  1. git branch
    

    This command is used to list all branches available. By default HEAD->* points to master/main

  2. git checkout -b <branch name>
    

    This command is used to create a new branch.

  3. git checkout <branch name>
    

    This command is used to switch within existing branches.

  4. git merge <branch name>
    

    This command is used to merge two branches. consider there are two branches main and style branch. currently, you are in main branch performing merge command git merge style -what happens here is style branch is merged to main branch. vice versa.

GITHUB Forking:

If you need to contribute to opensource repository that adds value you to then them. then you can fork repository from GITHUB and do your contribution and open a pull request. After that the community will review and will figure out whether your code need be merge or not.

github. io Pages

You can host your websites in github by simply adding github.io to your repository name(but this is not recommended) NOTE: The repository name should be your username as you created for github account

Example: Set of Commands used to move code from existing folder to github repo

  - git init
  - git remote add origin <repo-url>
  - git add <filename> or .
  - git commit -m "message"
  - git branch -M main
  - git push -u origin main 
  // after push command you will be prompted to enter
   username(use global config username) and 
   password(use personal access token)

These are the few useful git commands used in daily development environment. Hope this article will be useful.

THANK YOU! Happy Coding ๐Ÿ˜Š

ย