Working with Git

4 minute read



As we have learned what git and version control is, from my previous blog. Now, we will learn git commands, a developer will use at his early stages. To know the version of the git installed inside the computer, goto terminal and type:-

$ git --version

git --version

Now, for using git, you need a service that uses git as version control. The service will act as your central / remote / online repository for your git repositories. You can use any service like github, gitlab, bitbucket and many more. I am using github for my projects. Goto any service and create an account in them. Goto the service and create a repository for learning git.

Now, we will configure the git installed in the computer

$ mkdir test-repo # to create a test repo folder
$ cd test-repo # got inside the folder
$ git init # to initialise the folder as a git repository


After creating a local repo, we will to link the repository to a service that git like github, gitlab etc. We will create a repository in Gitlab.


Now, link the local repository created to the remote repository created o gitlab or any other server. You can do that by running the following command.

$ git remote add name url


If you see the above image the command executed is git remote add origin, origin attribute defines the original path of repository from where it is taken. The URL after origin is taken as the repository’s source URL. After this step, we need to set the upstream. That is, you have to set the branch to which you are going to upload the code.

$ git push --set-upstream origin branch-name


Now, if you dont want to do all these steps. That is, creating local repository using git init, add remote link using git remote add <name> <url> and setup upstream using git push --set-upstream origin master. You can just create the repository on github or gitlab and just clone the repository.

You can clone a git repository using:-

  1. URL
  2. SSH


If you want to clone a git repository using URL, you can do that using the following.

$ git clone repo-url


If you clone using URL, everytime you try to push code into the github / gitlab (or any remote repository), the console will ask for username and password. If you dont want that, you can use ssh.

For using SSH, you need to do setup you system. You need to generate keys for communication. You can do that using the following command.

$ ssh-keygen -t rsa -b 4096 -C ""

After executing the above command, you will be prompted just press enter. You might be asked for password, if you are using a public computer, then you should keep it or else no need. You need to copy the key generated to github or gitlab. Enter the following command to view the key.

$ cat

The generated ID will be displayed on the console. To validate if it is correct or not, it will start from ssh-rsa. Copy the key and goto github settings (if using github) and goto ‘SSH and GPG Keys’ and click ‘new SSH Key’ and give a title (Your PC hostname) and paste the copied key to the next field and save. Now, we can clone using SSH.

$ ssh clone ssh-url


We have successfully created a git repository. We will now configure the git engine installed in our system. The configuration involves telling who we are. That is, username and user email. We can do that as follows.

$ git config --global "yourname" # to set your name
$ git config --global "youremail" # to set your email


If you want to enable colour highlighting for your git console, just run the following commands.

$ git config --global color.ui true
$ git config --global color.status auto
$ git config --global color.branch auto


When, we have to write huge commit message, we need a text editor linked to it. For doing that, we use the following command.

$ git config --global core.editor editor-name


Here, if you see, I have given all config commands an tag --global, using this tag applies the configuration to your system and will be automatically implemented to all the git repositories you create inside your computer. If you are using git on your college computer, then from your git repository, run these configuration commands without using --global tag.

Now, we have added new configurations to the git repository. To view the configuration, use the following command.

$ git config --list

Now, we have setup the git repository. Now, we will learn to make commits and push code to remote (Github or Gitlab or any other) repository. Now we have added code to the repository and now we want to make a commit. First, we need to prepare the files that needs to be committed. We call it staging process. You can do that using the following command.

$ git add file-name

The other attributes for git add are * and .. I use * because, it will select all the changes made in current directory and it’s sub folders. If you use ., it will select only the files present on the current directory. It will not select the sub folders.

Now, we will commit the staged files. Use the following command to commit changes.

$ git commit -m "message"

Replace message with your message. If you have to write a huge message, just type `git commit’, your prefered editor will pop up and type the message.

As we have commited the code, we need to push it to the remote repository. You can do that using the following code.

$ git push


Categories: ,


Leave a comment