Git - In a nutshell (Part -1)

                 If you are coding, then you must know about the guy called 'GIT'. Here in this post I m trying to post the most essential git coding for you. Here I will code the basic git commands.

I assume you have installed git in your local machine. To verify it, open your terminal and type  git 
If its installed, you should get something like this.
Fig-1 : git installed machine

If you are not getting this kind of window, you have to install git, please follow this official git guide.

1) Initializing a git repository in your local machine

    Go to your project folder using cd command in terminal. Type

         git init           # creates an empty git repository  

         git add .       # add all the files in the current directory to git repo

         git commit - m "type your message"        # commit all the files to git repo

    These will create your first git repository in your local machine. Follow step by step. In the commit one, you have to put a meaningful message. It will help you in later time.

2) Revert a commit

    Boom, you made a mistake, your project is not working after some changes you made. and you don't know where are those changes. This is the place where git helps to code our life.
Don't panic, just follow these steps.

       git add .        # add all your things to repo

      git commit - m "I made a mistake"      # your message and commit

       git revert HEAD       # check your files. ;)

In order to revert back to your previous git repo, you need to commit your changes (even though they are wrong). Finally revert HEAD will bring all your old files.

3) Working with Remote git repository

    First you need to get that repository from github. Using your terminal use following codes.

       git clone https://github.com/gitrepo/folder

   Here 'folder is an optional, you can define it as you want, repo will save into this folder'

   then go to that folder using cd command. cd folder

   Enjoy coding your newly cloned repository. Oh, now its time to add your changes to github repository.

       git add --all

      git commit -m "commit from my local repo to github"

      git push -u origin master
 
Here git add --all will add all the changes to your local repository. You know what does commit do. Please make sure you put proper message there.
Then git push -u origin master

   here -u for the user

   origin is our name, its default assigned.

   master is the branch where you need to push your changes.

Then git will ask about your username and password. Insert them correctly. You are done, you added your changes to the original github repository.

4) Working behind the original git 

    Suppose you are working in a team, and others have committed changes to original repository, in this case, if you try to commit your local repo. You will get an error.



   Don't worry, this is what you have to do before starting coding

          git pull origin

and then try to commit your changes. Here all the works and testing were done in Debian environment.
If you are getting permission denied when you try to execute any of these codes, you need to add sudo for the all git commands I posted here.

In the next part, I will try to discuss about git branches and merge conflicts. Remember git is a life saver, but you need to keep coding and coding with git to get used. 

Comments

Popular posts from this blog

Youtube-dl cheatsheet

Git mistakes - 1

Docker Development - Mistakes 1