Get to know Git

3 minute read

Published:

Git is a distributed version control tool that runs on Linux, Unix, Mac and Windows. With the help of Git, you don’t have to manually record and save copies every time when the files are changed, or worry about file exchange and protection when working on big projects with others. This post introduces common usage of Git.

Installation

Git installation on Linux is simple, just type “git” in terminal and follow the hint.

CLICK ME

$ git
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git
$ sudo apt-get install git


Set your user name and email address. “global” means that all Git repositories on your machine will use this configuration, yet you can can use different names and email addresses for different repositories.

CLICK ME

$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"


Create a Repository

First let’s create a local git repository, so that all the files in the repository are managed by git, which means all the changes to the files can be tracked by git. For example, I enter “/home/teng/chatbot” and create a repository there.

CLICK ME

$ git init
Initialized empty Git repository in /home/teng/chatbot/.git/


Now we can work on the files in the directory. To actually add the file to the repository, it takes 2 steps: 1. add the file to “stage” using “git add”, and 2. commit the file to the “branch” we are in (default “master”) using “git commit”. The concepts of stage and branch will be explained later on.

CLICK ME

$ git add .
$ git commit -m "fix io bugs"


For convenience, here I use a dot “.” after “git add” to add all the files in the directory, you can put your file name there like “bitcoin.txt”. The parameter “m” after “git commit” means message, you are always recommended to add some description for the changes in the commit.

Remote Repository

The first git command I used is ‘git clone’, which is to clone a repository from github and save it to the local computer. We can also upload the local reporitory to remote github server to share the project with others. To communicate with the server, it is necessary to have a SSH key. Check if there are two files named “id_rsa” and “id_rsa.pub” in folder “.ssh”, which locates in the user directory, for example, “/home/.ssh”. If you cannot find the files, create the SSH key first:

$ ssh-keygen -t rsa -C "youremail@xxx.com"

Then we can go to github -> Settings -> SSH and GPG Keys and click “Add SSH Key”, paste the content of “id_rsa.pub”. If we want a repository same as the local one online, and synchronize the two repositories, first we should log in github webpage and create a new repo with the same name as the local repository, then push the local content to the remote repo following the instructions github given.

$ git remote add origin git@github.com:tengma137/chatbot.git
$ git push -u origin master

If we fork a repository from others and work on that repo, use “git clone” to download the repo, “git add” & “git commit” to edit files locally and “git push” to update the changes to the remote repo. Here’s how it looks like:

$ git clone git@github.com:user_name/repo_name.git
$ git add .
$ git commit -m "fix io bugs"
$ git push origin master


Branch Management

To be done.