Working with Git

Date Posted:16-04-2017

Git is a version control system designed to handle small projects to large projects in a effective way. This post explains on how to use git commands and how to handle git repositories,etc.,

Git Repositories:

Git repositories are the location where you can store the all your project/application code. It could be either on github account or your own private repositories such as gitlab or bitbucket account,etc.,

Clone repository:

Normally, there are two ways to clone the repositories. One is via http protocol or other way is to use SSH protocol.

The below command is to clone the git repository to your local machine using https protocol.

git clone https://gitrepourl/repo.git

This will create a directory called repo on your local machine.

Incase, if we receive an error git command not found, we need to install git on the machine.

For Ubuntu,

apt-get install git

For Centos,

yum install git

Now, we can setup git on the machine.

git config --global "Your Name"

git config --global "your-username@domain.tld"

Git Commands:

So far, we have cloned the repository and setup git.  Now, we are making changes the files and commit to the repository.

We are creating a file test.html on your local machine and push it to git repository.

vi test.html

Test Content

Now, add the file to git repository

git add test.html

Commit the file with your changes.

git commit -m "Adding test.html"

Add remote repository to origin.

git remote add origin https://gitrepourl/repo.git

Push the changes to the remote origin.

git push origin master


Origin is remote URL

master is a local repository.

Additional Information:

The above example is a simple one which explains on how to push a single file to git repo. In this section, we will explain few more git command which can help on how to use git effectively.

To show recent changes, current commits,etc.,

git status

To view the remote repository.

git remote -v

To view the git log and commit IDs.

git log

To stash all the local changes

git stash

To create a new branch from master

git branch name-of-branch

To checkout to another git branch.

git checkout -b name-of-branch

To merge a branch to master. First Checkout to master branch and merge the branch

git checkout master

git merge name-of-branch

To delete a file from git branch. First delete the file, commit and push the file.

git rm test.html

git commit -m "Delete from git"

git push origin master

To move to previously saved git commit.

git reset --hard HEAD~1


Leave a Reply