Git: All you need to know about it.

·

8 min read

In this article, I’m going to discuss all the things you need to know about Git as a beginner.

Ok, so let's start from the beginning….

What is Git?

“Git is free and open source software for distributed version control: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development.”

Okay, but it is a little hard to understand, right? Don't worry, I'm here to make it easy. Suppose you are working on a huge project, after working for 1 month, you have observed that you have messed with your project, but it was totally fine one week ago, so you want to go back there. Here comes the role of Git. You can track all the previous changes/versions by its version control system and get back to any version you want. Not only that, but you can also work with another person remotely using internet hosting platforms like GitHub and do many other things using Git. Git is founded by legendary software engineer Linus Torvalds who developed the Linux kernel.

What is Github?

GitHub is an online hosting platform that is used for software development. Using GitHub we can keep our code files remotely, work on a single project from multiple places and multiple devices, keep track of progress by using a version control system, share our project with people and contribute to other open source projects. GitHub is not the only platform to provide this service though it is the most popular in this area. GitLab, Bitbucket are some popular alternatives to GitHub.

How can I install Git?

You will get the link to download Git from their official website for Mac, Windows, and Linux also. You can also Click here to download Git.

pexels-realtoughcandycom-11035539.jpg

How can I configure Git?

After installing it on your device, first, you have to introduce yourself with git. Using the git config command you have to give details about yourself to git. Open the bash terminal of your device (for mac and Linux users) or git bash (for windows users) and type these commands.

git config --global user.name <your name>
git config --global user.email <your email>

How can I upload my project on GitHub using Git?

Okay, I have installed git and configured it, but how would it know about my project? Don't worry, there are some simple commands to do that for you.

  • Open your terminal and first navigate to the directory of your project using cd command. Then in that directory enter the command:

    git init
    

    It will initiate git in that directory by creating a hidden file named .git.

  • Now add your files by using the command:

    git add <file name>
    

    You can add all your files in that directory by replacing the file name with .

    git add .
    
  • By adding the files we are sending them to the staging area. Now you can commit the project by using the command:
    git commit -m "Your massage"
    
    A quick tip here: You can add all your files and commit them in a single step without the git add command by using this command:
    git commit -am "your massage"
    
  • Now during this process, if any time you want to check whether all your files are in the proper position or not, you can use the command:
    git status
    
  • At this point, your project is committed but still in the local directory, If you want to put them in your GitHub account, you have to follow these steps:
    • Go to your GitHub account and click on Create new repository. This option will be available on the top right section of the home page just beside the profile icon. This option will also be available on the left of your screen with a green button just below the search option.
    • After going to create a new repository page, you have to give the name of your repository, add a description if you want to, select private or public as you want and just click on create repository button.
    • Now copy the link given below the quick setup section and return back to your terminal and type the command given below:
      git remote add origin <pest the link you copied before>
      
      and then:
      git push -u origin master
      
    • Now go back to your browser and refresh the repository. Your project will be updated there.

How can I clone a remote repository on my local device?

Till now you have learned how to push your local repository to GitHub, but do you know you can clone any remote repository from GitHub to your local directory also? Here is the command:

git clone <URL of that repository>

Just give this command and that repository will be cloned to the directory you are currently in.

What is Branch in Git?

image.png Suppose you want to create a new feature in your project but you are not sure if it will work properly in the project or not and maybe it will create a mess in your project. Here comes the concept of the branch in git. You can just create a new branch, then move to that branch and add that feature, and after finishing that if you think it is working properly, you can just merge that branch to the main branch. Now let's see how this works.

  • Create New Branch:
    git branch <branch name>
    
  • Switch to Another Branch:
    git checkout <branch name>
    
  • List of Branches:
    git branch
    
  • Create and Checkout to New Branch:
    git checkout -b <branch name>
    
  • Delete Branch:

    git branch -d <branch name>
    

    NOTE: It is a safe option as it will show a warning if there is any unmerged change in that branch. But there is another delete option is also available which will delete the branch in any situation without any warning. Remember delete option will not work if you are in the same branch which you want to delete, you have to switch to another branch before deleting.

    git branch -D <branch name>
    
  • Rename Branch:

    git branch -m <new name>
    
  • List all Remote Branch:
    git branch -a
    
  • Merge: To merge a branch with another branch, first, you have to move to the branch where you want to merge the other branch. Suppose you want to merge new_branch with the master branch. the commands should be:
    git checkout master
    git merge new_branch
    

Extras:

So far I have mentioned almost every topic you need to know about Git as a beginner but there are some more features available that can help you also.

  • Stash: You can temporarily save the changes of your project using git stash and then view or use that later. To stash your project you have to use this command:

    git stash save "your massage"
    

    You can add multiple stashes of a single project, even in multiple branches also. You can see the list of your stashes using-

    git stash list
    

    Your stash will be shown in the format stash@{index number, eg. 0}: <branch name> : <your massage>. To bring the stash to your branch you can use these commands:

    git stash pop stash@{0} //instead of 0 use the index of stash that you want to bring back
    

    or

    git stash apply stash@{0}
    

    NOTE: pop & apply both will bring the stash to your branch but there is a small difference between them. pop will bring the stash to the branch and remove it from the stash list while apply will copy it to the branch keeping the stash list intact.

    You can delete a stash using

    git stash drop stash@{0}
    

    or you can also delete all the stashes using

    git stash clear
    
  • Log: git log command will show all the commits made in the repository as a list with a unique SHA id for each. You can customize this list with some commands:

    • git log --oneline will show each log in a single line, you can use - and a number to see the logs till that particular number.
    • git log --until="<date>" or git log --since="<date>" will show you the log from a particular date or from that particular date.
    • git log --<author name> will show logs of all commits made by a particular author.
    • git log --graph will create a graph of logs to make it more understandable.

    You can use these options in combination to get more optimized results. There are many more options available too, you can read official documentation for more details.

  • .gitignore: Git tracks all the files available in that repository, but if you want Git to remove any file or folders from its tracking system but keep that in that directory, you have to create a file named .gitignore and add the file names or folder names in that file. It will ignore all those files automatically.

Conclusion:

These are the most used features of Git, but there are so many features to explore in Git. You can read about them from official documentation. Thank you for reading the article, please leave your comment below.

Â