DEV Community

Sankalp Swami
Sankalp Swami

Posted on • Originally published at

Git and GitHub Essentials You Should Know Right Now

Full article is available on Aviyel

If you are an open source contributor, you would know what Git means to you. It was the brainchild of Linus Torvalds when he was all engulfed in building Linux kernel. For beginners, who are just getting started with Git, it is a version control system that allows source code bases to exist in form of thousands of files and repositories. You can also check out some of its alternates Apache Subversion, ClearCase or Mercurial. But Git sits at the top owing to its already popular developer base, and industry standard practices.

Git & Github

The post will help you get started with Git if you are a beginner. For intermediaries and experts, I have underlined few Git essentials that developers can use for their everyday coding needs.

Getting Started with the Basics

Git, as a version control software tracks projects, files, and changes made to them by developers. Git’s repositories are storehouses where information about a code or overall project is stored. As soon as you get your hands on Git and start contributing, all your contributions and project-specific data will be stored in .git, another common but hidden subfolder. Git automatically merges the code with changes made by users. Contributors can then commit to a project for consenting to the changes made recently. Master is the main branch where all committed code goes after a nod from users.

Git Useful Tricks and Commands

  • git grep 'something' to search for a string’s part

  • git init [project-name] to create a repository

  • git config --global core.editor vim command to set the preferred editor when triggering the git commit -a command

  • git config --list to present all defined config lists.

  • git add [file] or git add * - to make changes to your Git files

  • Use the git remote command to present defined remote Git server URL lists.

  • Use git blame 'filename' - to show the author and saved history of your repo

  • git log -S 'something' - to get commits of a particular string

  • git log --author 'Author Name' - sort commits by author’s name

  • mkdir [directory-name] - to create a new directory

  • Use the git remote get-url remote_url_name command to present the current remote name to be the Git server URL.

Installing Git

For MacOS

$ brew install git
Enter fullscreen mode Exit fullscreen mode

For Linux

$ apt-get install git
Enter fullscreen mode Exit fullscreen mode


$ yum install git
Enter fullscreen mode Exit fullscreen mode

For other Linux distributions, you can click here

Git's Working Guide

Working of Git

  • Initialize your working directory as Git Directory with the command git init.

  • Once initialized, add all your files to the staging area. Staging is nothing but tracking files from the working directory.

  • To add untracked files from a particular directory, use the command git add or git add. We can’t commit untracked files from the working directory until and unless we don’t stage them.

  • The commit is used to save your changes to the local repository. Every time you save that file, all the changes made are tracked.

  • Git commit command takes the snapshot recording the staged changes. We can commit a file by the following command git commit -m "", where the message should be related to the changes made in that particular file at the time of staging.

  • Once all the files are staged and committed, push the local repository to the code hosting platform like Github for collaborations and protecting files from unwanted failure.

Setting Up Git

Open terminal and make a new directory with following command

$ mkdir gitEssentials
Enter fullscreen mode Exit fullscreen mode

now navigate to the directory

$ cd gitEssentials/
Enter fullscreen mode Exit fullscreen mode

Add some files to the directory.

$ touch index.js server.js index.html main.css
Enter fullscreen mode Exit fullscreen mode

now check the created files

$ ls
Enter fullscreen mode Exit fullscreen mode

Initialize the working directory with git init command.

$ git init
Enter fullscreen mode Exit fullscreen mode

Now add some lines to your index.js file so that we can add them to staging area.

$ echo "console.log('Git Essentials');" >> index.js
Enter fullscreen mode Exit fullscreen mode

git status command is used to check the current status of our working directory.
git status
As we can see there are no commits yet and we have 4 untracked files.
Now with the command git add, we will track/stage the files.

$ git add .
Enter fullscreen mode Exit fullscreen mode

Lets check what git status has to show now
git status
So we have staged all the files. Lets create another file and name it to_Be_Deleted.js.

$ touch to_Be_Deleted.js
Enter fullscreen mode Exit fullscreen mode

Now check the status with git status.
git status
This is what we get now. It shows that we have 1 untracked file. Let stage the untracked file using git add command.

$ git add .
Enter fullscreen mode Exit fullscreen mode

After adding, check the status using git status command.
Git status
We have successfully staged our newly created file to_Be_Deleted.js.
Now we will learn how to unstage an file.
For unstaging an file we will use git rm --cached <file> command.
Lets unstage the to_Be_Deleted.js file.

$ git rm --cached to_Be_Deleted.js
Enter fullscreen mode Exit fullscreen mode

Thats it, we have unstaged the file successfully.
Lets check it using git status command.
git status
Yepp! We have unstaged the file.
Delete the file to_Be_Deleted.js

$ rm to_Be_Deleted.js
Enter fullscreen mode Exit fullscreen mode

Now we will be working on git commit command.
Lets commit our staged files.

$ git commit -m "console.log added to index.js file"
Enter fullscreen mode Exit fullscreen mode

Lets check the status of our working tree after committing files.
git status
So as we have committed all the files we get the message nothing to commit, working tree clean. That's cool.
Lets check the logs using git log command. This command is super handy.

Now you can work practically on Git and Github if you follow the complete article on Aviyel. I bet that the practical knowledge that you will be gaining in this article will be really helpful for using it in your personal projects as well as if you are thinking on contributing to Open Source projects.


Discussion (0)