Git Basics !

Git Basics !

What is Git ?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Windows Installation

Visit Git Download then download and install git on your windows machine from the standalone installer. Installation is pretty straight forward same as any other software you install on windows.

Mac OS Installation

  1. Install brew from the Official Website
  2. Enter brew install git command on terminal to install git.

Now, let's learn some basic git commands :

  • git -v or git --version

    Let's you check the current git version installed on your system. This also indicates that the installation was done properly.

  • git -h or git --help

    It will give the list of most commonly used git commands. If further --all or -a is added then it will give list of all the commands.

  • git init

    Initializes a git repository inside that particular folder inside which you have run this command.

  • git config --global user.name "FIRST_NAME LAST_NAME"

    To configure the global user name to be used on your system. This name will also be displayed to you on your commits on git repository.

  • git config --global user.email "MY_NAME@example.com"

    To configure the global user email to be used on your system. The email id used should match with your email id that you have used for creating the GitHub account.

  • git add <file-name>

    This command can be performed multiple times before a commit. It only adds the content of the specified file(s) at the time the add command is run; if you want subsequent changes included in the next commit, then you must run git add again to add the new content to the index.

Example :

git add readme.md
  • git add .

    To add all the files which are to be included in the commit. The ( . ) dot indicates that you want all the files from the directory.

  • git commit -m "commit message"

    A shortcut command that immediately creates a commit with a passed commit message. By default, git commit will open up the locally configured text editor, and prompt for a commit message to be entered. Passing the -m option will forgo the text editor prompt in-favor of an inline message.

  • git remote

    List the remote connections you have to other repositories.

  • git remote -v

    Same as the above command, but include the URL of each connection.

  • git remote add <name> <url>

    Create a new connection to a remote repository. After adding a remote, you’ll be able to use <name> as a convenient shortcut for <url> in other Git commands.

  • git push -u origin <branch-name>

    This command will push the branch on the remote repository.

Example :

git push -u origin master
  • git clone <remote-repository-url-here>

    This command is used to make a copy of a repository from an existing URL. If I want a local copy of my repository from GitHub, this command allows creating a local copy of that repository on your local directory from the repository URL.

  • git branch

    This command lists all the branches available in the repository.

  • git branch <new-branch-name>

    This command is used to create new branch.

  • git merge <branch-to-be-merged>

    This command is used to merge the specified branch history into the current branch. You need to stay on the branch in which you want merge the other branch.

  • git checkout <branch-to-which-you-want-to-switch>

    This command is used to switch between branches.

  • git checkout -b <new-branch-name>

    This command will create a new branch and also switch immediately to that new branch.

  • git checkout -b [branch name] origin/[branch name]

    This command allows you to clone a remote repository on your local system.

  • git tag -a v1.4 -m "my version 1.4"

    Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the tag command. The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag, Git launches your editor so you can type it in.

  • git push origin v1.5

    Share the created tag with the team by pushing it to the remote repository.

You might require many more git commands to study as per the project requirement. But above mentioned are basic and used more often. Study more commands from the official documentation : Git Docs


That's it for this blog. I hope you have learnt something from it.

Happy coding. Thank you !