DEV Community

Cover image for Commands of Git Bash every Github developer should know
Rahul Sinha
Rahul Sinha

Posted on

Commands of Git Bash every Github developer should know

We all know what is Github? As it is an online software hosting website. Then what do we understand by Git? Git is a distributed version control system that keeps track of changes that you make to files and directories and it was created by Linus Torvalds in April 2005. It is open-source and free; Anyone can see and contribute or modify in the source code. The Version Control system is also known as SCM or Source Code Manager.
What do we mean by the Distributed version control system? In a very simple term that different users maintain their own repositories. There is no central repository. All repository is equal for git.

Changes are stored as changesets.

  • Track changes, not versions like V1, V2, etc.
  • Changesets can be exchanged b/w repositories.
  • No need to communicate with a central server.
  • No network access required
  • Encourages participation and forking(branch or divide) of projects.

Let's start with some Git command.

Open your GitBash. To know the version of the git installs in your system.

git --version

Now configure your user name and email address with git.

git config --global user.name "your username"
git config --global user.email youremail@email.com

You can also choose your default code editor by git. I am using VS code, you can add yours.

git config --global core.editor "vscode.exe"

If you are seeing only black color and you want to colorize your git. Also, you use help to know more commands about git.

git config --global color.ui true
git help
  • You can also see Git repository in Github

    GitHub logo git / git

    Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.

    Build status

    Git - fast, scalable, distributed revision control system

    Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

    Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

    Please read the file INSTALL for installation instructions.

    Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

    See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with …

Now Let's clone a repository on our computer. First sign-in in your GitHub account. Create a new repository there and copy the https link and just paste there using ctrl + v
Alt Text

cd D:/ #change the directory to D:/ directory
mkdir Git #created a folder named Git
cd Git #come Git folder and clone it here
git clone https://github.com/rahulsinha036/gitrepo.git

Alt Text

Come to gitrepo folder. Here we can see all the files and folder using ls -la command. In the gitrepo folder, you can .git folder where git is initialized and one README.md file which read me file.

cd gitrepo
git ls -la #it will show the all files and folder of Git directory

Let's create some text files using touch command in the gitrepo folder. To add some values in that text file we can use code command.

touch newgit.txt #it will create a new txt file
code newgit.txt #it'll open in your default editor and add some txt.

After some values in the text file, you can see the status of the file.

git status

Alt Text
In this image, you can see newgit.txt in red color. It means that it has not been tracked by git and not committed yet. Let’s add to git and the commit…

git add . # dot means to git all the files to git
git add newgit.txt #you can use in this way also

Before committing to the repository, we should now the workflow of git.
Alt Text
There is three tree architecture of git → working directory (your computer folder), staging area (where you first add files to git), and local repo or remote repo (means when you commit the files to GitHub). Here local repo means your files will not directly be added to Github repo but you can see all tracks using locally with the same commands.
Now, let's commit the files to GitHub. Here -m is for a commit message.

git commit -m "Initial Commit" newgit.txt
-->It will add your file to Github repo.

To see all the commit you have done…

git log #it will show all your commit
git log -n 5 #it will limit the no of commit & show up to 5 only

git log --since=2020-05-10
-->It will show all the commit onwards 10th May 2020

git log --until=2020-05-10
-->It will show all the commit done till 10th May 2020

git log --author="Rahul" #You can search commit by using your name

git log --grep="Init" #You can search using commit message
--> grep stands for Global Searching for Regular Expressions

git help log #it will show all commands to use in git

If you have made some changes to the file and forgotten, here git can help you to review it all.

  • But it shows changes only for the working directory, not for staged and local/remoter.
git diff

To also know the changes in the files in the staging area…

git diff --staged or git diff --cached

There is also one shortcut for committing the file to the repo. As you know that first, we have to add our file to git then commit the file to the repo. We can skip that adding process…

  • Note that this shortcut is only valid after first successful adding and committing the file. If you have made some changes and you want to skip the 2nd process and directly want to commit.
git commit -am "Commit all the file"
--> Here a is for all and m is for a commit message

After committing the file, you get a unique code which is known as SHA-1 or Hash Value.

  • Git generates a checksum(Hash Value) for each changeset.
  • Data integrity is fundamental
  • Changing data would change checksum. It means each hash value is unique whenever we change some data
  • The number generates 40 character hexadecimal string (0–9, A-F) Alt Text There is one more command git show to show all SHA-1 or hash value and it will also show all the changes you have done in the file.
git show
git show 30699c 
--> You can also use the first 6 char of hash value to show all the details of that commit.

Alt Text
Let's push the file to GitHub after committing and modifying the file.
Alt Text
You can see our newgit.txt has been uploaded to GitHub. There are more some commands and some of the most common ones are like:

--> to create one folder
mkdir newfolder
--> to move the file from one folder to another
git mv newgit.txt newfolder/newgit.txt
--> to rename the file
git mv newgit.txt newgitfile.txt
--> to remove the file from the working directory
git rm newgit.txt

Hurray!! You have learned some commands of git bash. Go and Try all these commands on your Git Bash.

Top comments (0)