Getting Started with Git

Basic stuff you need to know about Git

Kemila De Silva
Platformer — A WSO2 Company

--

What is Git?

From Git’s official website..

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.

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Let’s get started to works with Git

First, we need to check whether we have git installed already. We can easily check this by running the following command.

git --version

If it is available you can see like this.

git version <some-version-number>

If it is not available you need to install it. So in this scenario the first step is installing Git in your operating system.

Step #1

Installing Git (for mac, windows, linux and ubuntu)

#i. For mac, linux and ubuntu os

For installing Git on your operating system just tryout these commands. First of all you need to create a GitHub account and you need to open up command prompt (for windows users); if you are working with mac, linux or ubuntu just open up your terminal. So, in this scenario you need to open up your terminal. After that enter this command.

$ sudo apt install git-all

#ii. For windows

Just follow below the link and download the software and install it.

https://git-scm.com/download/win

So now you have installed Git on your operating system.

Step #2

Configuring Git profile properly

Next, let’s add the global username and email for our git profile

git config --global user.name "yourusername"

Now you have perfectly configured out your GitHub account user name. Now what you want to do is; configure out your email account. Please type the following command.

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

So guys when you have done this part properly it should return your user name and email respectively.

So now you need to follow basic Git commands . It’s all about what you need to do when you start work with a project.

Basic Git commands

First and foremost, we need to create a local repository and initialize git for it.

Step #1

Create a remote repository

In this step let’s move to your GitHub account and click the ‘+’ button on the top of the menu. Then click the ‘create a new repository’ button. After that you will get a window for name that repository and other basic stuff. After that follow below steps.

In this step what we are going to do is work with Git in our local repository. For that just follow this commands.

git init

So now you have connected with the local repository. Let’s checkout a repository. For that follow up this command.

git clone /path/to/repository

Step #2

Add some files into the remote repository.

So guys when your working with projects you need to add some files in it. So let’s find out how to add files while you are connected with Git. For that you need to follow these simple commands. Let’s try this out.

git add <filename>

git add *

Awesome! Now you know how to add files onto a remote repository.

All right let’s move on to the next steps..

Step #3

Git commit

I will explain Git commit flow with an example; just assume if you working with vs code when you change instantly you get white circle on the top of your current tab. That’s means you need save it now. As well as when you are working with Git you need to save changes you are done. For that we need to follow this commands. Let’s try this out. This command for Commit changes to head (but not yet to the remote repository).

git commit -m "Commit message"

So, let’s try to do this one on your remote repository. Here’s the command for that.

git commit -a

So guys at this moment you have completed very basic stuff of Git. So, when your working with remote repositories you need to Git push. Here, we go..

Step #4

Git push

In this step we discuss about branches. Don’t worry about it if you are confused. I will let you know about that. So at that moment we are push this Git push into our master branch. Master branch is the default branch when your working remote repositories. You can also create other branches. I will let you know about it. Here’s the command for the Git push.

git push origin master

Now you can checkout your status also. For that try this command.

git status

So guys when you checkout your status it will show you like this.

$ git status
On branch master

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)

.DS_Store
LICENSE
README.md
attachments/
contact.html
css/
events.html
gulpfile.js
img/
index.html
js/
mail/
package-lock.json
package.json
scss/
upsurge.html
vendor/

nothing added to commit but untracked files present (use "git add" to track)

So guys now your know how to put a Git push. So let’s see how to what are the branches which I mentioned earlier.

Step #5

Git branches

So guys actually Git branch is a if get an example in practically it’s kind of a like folder in our hard drive. Just assume GitHub is our hard drive in our hard drive folder is Git branch. So, I think now you have a pretty idea about what is the Git branch. So, let’s see how to create a Git branch in your remote repository.

git checkout -b <branchname>

So, now your create a Git branch in your remote repository. Actually likewise you can create different branches in your projects. So, when you creates different branches you need switch to it. For that you need to follow this command. Let’s try this out.

git checkout <branchname>

Okay, guys so just assume if your working with a branch. But you can’t sure your working branch name. For that you need to configure that branch name. Let’s try this out.

git branch

So, if you want to delete your current branch; if there some issues. Just follow this command.

git branch -d <branchname>

So let’s push up your current branch into your remote repository. Follow this command for that.

git push origin <branchname>

So if you want to push all branches instantly. follow up this command.

git push --all origin

So, if there some issue on your remote repository branch. So you want delete that branch; for that this command.

git push origin :<branchname>

Step #6

Git pull

Actually Git pull needs to update your remote repository. When you update something in your remote repository it will Fetch and merge changes on the remote server to your working directory. For that you need to follow this command.

git pull

Step #7

Git merge

Git merging is depends on your working with couple of branches. After that you need merge these branches together. For that you need to follow this command.

git merge <branchname>

Step #7

Git tags

So, you can use tagging to mark a significant change set, such as a release. For that you need to follow this command.

git tag 1.0.0 <commitID>

So, if you want push all tags to remote repository. Here’s the command.

git push --tags origin

So guys here we go. Now you guys know all the basic Git commands. I think at the moment you don’t have issues yet. If you have any issue just comment it below.

Let’s find out what are supported developing softwares for Git.

  1. VS code
  2. Atom
  3. Android Studio
  4. Eclipse
  5. IntelliJ IDEA

Conclusion

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

Git is easy to learn and has a tiny footprint with lightning fast performance. It outclasses SCM tools like Subversion, CVS, Perforce, and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

If you have any issues in this tutorial you can comment it. Feedback and criticism is more than welcome.

--

--

Kemila De Silva
Platformer — A WSO2 Company

Senior DevOps Engineer @aeturnuminc • CNCF Ambassador • AWS Community Builder • AWS Certified • Community Organizer • || kemilad.bio.link