Basics commands of git

The git is one of the popular VCS that is Version Control System. Which can be used for the managing of the source code.

The git can be used to share code with others and to collaborate with other developers. To learn why use git? check this blog.

git vs github :

As you know git is a VCS that was created by the developer of the Linux Kernel in 2005 by Linus Torvalds. And GitHub is a service built on top of the git and GitHub is owned by Microsoft. Github also has added more features of its own on top of git.

Installing git :

Before we start talking about the git check if you have git installed on your computer by running below command.

git --version

If git is installed then it will show the version when you run above command.

Git is a free and open source you can install it from its official site.

DOWNLOAD GIT

Git installation instructions for each platform

Basic command to work with file directory :

These are the basics commands well to know. These commands are helpful to interact with the file directory from git bash.

  • mkdir - This command is used to create a folder inside the present directory.
mkdir FolderName
  • ls - This is a command used to list all the files inside the directory.
    ls
    
  • cd - This command is used to change the directories.
    cd
    

    Note: The cd will move one folder backward from the current directory.

cd FolderName

Note: The cd Foldername will move forward inside the specified FolderName if the folder exists.

Basic git commands for beginners:

  • git config - This command of git is used to configure the git as per the user requirement.
git config --global user.name="yourname"
git config --global user.email="your email"

By setting the user.name and user.email Whenever you commit to the repo. It will show the name and email in the history log.

Note: Here --global will set the given value to all the repo, not to the particular repo.

  • git config -l this will list all config settings.
git config -l
  • git init - This command of git is used to initialize an empty repository in short it is called repo. The repo in git is like a folder. Repo is the place where the code and configuration files are stored. The repo is also called a working tree this is where the current version of the code is stored.
git init
  • git add - This command of git is used to add the files to the staging area from here only the git will track the changes before we commit.
git add filename.extension

In the above command, we can add file by file. example: git add Demo.java.

Adding files to the staging area file by file is not a practical way if we have more files in the repo. We can add all the files to the staging area by using the following command.

git add .

In this . is a wild card this will add all the files to the *staging area`. This is better compared to writing names of files.

  • git status - This command is used to check the status. In git there are traced and untracedfiles. Traced files are the files that are committed in the git repo. And the untraced files are the files that are new or modified or removed from the working tree.

To make the files untraced to traced add the files to the staging area to check this we can use the git status command to do so.

git status
  • git commit - This command is used to save the changes which are in the `staging area to the repo. After we commit the changes repo* creates the history of changes made.
git commit

The above command can be used to commit to the repo. When you execute the above command it will open a default editor which you have set up in git config. The editor will open because it needs a commit message before committing. If you didn't give the commit message then the commit will be aborted.

This is not a perfect way to write a commit message. We can use the other method.

git commit -m "message"

Unlike the above git commit, This will not open the editor to take the commit message. But, in this one in the same line only we can write the commit message. In this -m git will understand that we are writing the commit message. Example: `git commit -m "Created Demo.java".

After committing the changes to the repo the files will be in the git directory and git will create history.

References if you want to learn more about git :

  1. PRO GIT - This book (available online and in print) covers all the fundamentals of how Git works and how to use it. Refer to it if you want to learn more about the git.

  2. GIT TUTORIALS - This tutorial includes a very brief reference of all Git commands available. You can use it to quickly review the commands that you need to use.

  3. GIT OFFICIAL DOCS - This is official documentation of git. Best source to learn about git and it's updates.