A Simple Guide to Basic GIT Commands

Ada Lovelace, a young visionary mathematician wrote the first computer program for Charles Babbage’s Analytical Engine to calculate a sequence of Bernoulli numbers. She predicted that by representing data into numbers, machines (modern computers) could produce graphics,  compose music and be useful to science. What she predicted more than 150 years ago is actually happening. It all started with her program.

Today programmers are writing way more complicated computer programs and software applications. Teams of developers collaborate to develop mobile apps, desktop apps and web apps that are running businesses. 

However, multiple programmers writing code for the same application can lead to inconsistency and errors without proper collaboration and version control. This is the reason why code collaboration and version control platforms play a significant role in the development of software applications. SVN, GIT and there are several other version control systems. However, GIT is the most used tool for source code management and tracking changes in files. GITHUB is another tool that is very popular in the software/web development industry. 

GIT and GITHUB are not the same. However, they are closely connected. GIT is a version control system. You have to install and maintain GIT on your local system. It provides you with the branching model. You can create an independent branch and try out new ideas. It takes a click of a button to delete, merge, and recall branches.  

On the other hand, GITHUB is a cloud-based hosting service that is designed as a GIT repository hosting service. It allows you to share code with others. You can revise or edit your code.

 

To use GIT and GITHUB, install git and create a GitHub account. To create a new project on your computer, create a new repository. First of all, open the terminal and move to the folder to place the project. 

$ cd ~/Desktop

$ mkdir myproject 

$ cd myproject/

Now, run git init command to initialize a git repository.

$ git init

Once you see the following in the terminal, you can add a new file to repo:

Initialized empty Git repository in /………………./Desktop/myproject/.git/

Adding a new file to the repo:

You can use touch command to add a new file to the repository.

$ touch filename.txt

Use the git status command to see the files in the repo:

$ git status

If you want to upload a file from your computer, open the terminal and use the following commands:

$ git config –global user.name “ADD_YOUR_USERNAME”
$ git config –global user.email “ADD_YOUR_EMAIL”

Clone GIT repository

$ git clone GIT_REPO_LINK
$ cd FOLDER_NAME
$ touch README.md
$ git add README.md
$ git commit -m “add README”
$ git push -u origin master

You can also push an existing folder using the following commands:
Move to the existing folder.

$ cd existing_folder

 

 

 

 

 

 Now run the following commands in the terminal:

$ git init
$ git remote add originGIT_REPO_LINK
$ git add .
$ git commit -m “Initial commit” //ADD YOUR COMMENT HERE
$ git push -u origin master

You can push an existing repository using the following commands:
Move to the existing_repo

$ cd existing_repo

Use the following commands:

$ git remote rename origin old-origin
$ git remote add origin GIT_REPO_LINK
$ git push -u origin –all
$ git push -u origin –tags 

Switching Branches in GIT

Having more than one branch in the repository is pretty common. You can easily switch branches in GIT using checkout command. Suppose you have the following two branches in your repository:

  • master
  • hello-git-branch

Suppose that the master branch is the current branch. Run the following command to switch to hello-git-branch.

$git checkout hello-git-branch

In this demo, we transfer a remote repo to a local machine from Github. It contains the following two branches:

  • master branch
  • hello-git-branch

The hello-git-branch is created using:

$ git branch hello-git-branch

As you can see in the git bash screenshot, the current active branch is master branch:

You can see $ ls command returning the names of the files in the master branch.

 

Switching From master to hello-git-branch Branch   

Run the following command and check the change in the below screenshot:

 

$ git checkout hello-git-branch

 

You can see that no message is displayed. However, you can see the name of the branch ‘hello-git-branch’ in brackets.

 

 

gitignore File 

It is a file in which you can list certain files and folders that you want GIT to ignore. You can create this file both locally and globally. When created locally, it is often added to the project’s root directory. When created globally, all entries in the .gitignore file are ignored in all the Git repositories.

 

Creating a .gitignore File Locally

Create a text file with name ‘.gitignore’. Now add folder or file you want to be ignored. Keep in mind that you have to add one entry at each line.

The entries in the .gitignore can follow certain matching patterns. Check the table:  

 

 

* Wildcard Match 
/ Ignore pathnames
# Add comments

 

 .gitignore File Example 

# Ignore Mac system files
.DS_store

# Ignore node_modules folder
node_modules

# Ignore all text files
*.txt

# Ignore files related to API keys
.env

# Ignore SASS config files
.sass-cache

Run the following command to add or change the global .gitignore file:

$ git config –global core.excludesfile ~/.gitignore_global

This creates the file ~/.gitignore_global. Edit the global .gitignore file just like a local .gitignore file.
 

Untracking Files Committed from Gitignore 

 

You can untrack a single or every file in .gitignore.

The following command untracks a single file

$ git rm –cached filename

Untracking every file:

Commit all the outstanding changes in the code and run the following command:

$ git rm -r –cached

Now run:

$ git add .

Commit it:

$ git commit -m “.gitignore is now working”

You can use git add filename to undo git rm –cached filename.

Avatar photo
Rahul Vij

Rahul’s mathematically competent, process-oriented mindset finds creative ways to fill gaps that arise in business operations. At WebSpero, he’s directed 650+ global developmental projects using advanced frameworks like Angular JS, Node JS, React JS, among others, that prime websites & applications for conversions. His team uses cutting-edge technologies and builds rewarding strategies that helped scale the ROI of over a thousand international brands 40X+. In addition, his love for classical music keeps him enthralled when he’s not working.