Introduction

Most of us dislike Git on the first try, even after running the most basic Git commands. GIT is a distributed version control system. It helps you in effectively manage product versions. The purpose of this blog is to provide most basic commands to get you started.

Common Uses:

  1. Keep track of all the files in a project
  2. Record any changes
  3. Restore versions
  4. Compare versions
  5. Manage code from teams

What is Repository?
A repository is just a directory (a folder) in your project's root directory.

GIT Code Cycle

Below diagram shows the common cycle for code in git, read from top left in an anti-clockwise direction.

  1. The user pulls changes from Github Repository into its local.
  2. The user checks out and starts working on a new branch
  3. User push changes to his branch
  4. User raises a pull request to the remote for merging

github

Git Cycle

Basic GIT Commands

There are around ten most basic commands that assist you in getting to know the status and start working on the repository.

  1. Check the current status

Input:
git status
Output:

On branch master
Your branch is ahead of 'origin/master' by 48 commits.
(use "git push" to publish your ocal commits)
Nothing to commit, working directory clean
  1. Fork the repository in the github project and get the HTTPS URL

Input:
git clone https://github.com/YOUR-USERNAME/Spoon-Knife
Output:

Cloning into `Spoon-Knife`...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (8/8), done.
remove: Total 10 (delta 1), reused 10 (delta 1)
Unpacking objects: 100% (10/10), done.
  1. Go to the directory of the forked project and check the fork information.

Input:

cd dirname
git remote -v

Output:

origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  1. Go to the directory of the forked project and upstream which we can get from github project page HTTPS URL

Input:

cd dirname
git remote add upstream https://github.com/octocat/Spoon-Knife.git
git remote -v

Output:

origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
  1. Branch out from the master

Input:
git checkout -b branchName
Output:
Switched to a new branch "branchName"

  1. Basic Merge your changes needed to manage multiple branches for segregation of work
git checkout master
git merge branchName
  1. Add New File or all new files
git add filename
git add .
  1. Commit your work to your branch

git commit -m "Comment Title for Committing"

  1. Push changes to our repository master (enter username and password for your git account)

git push origin BranchName

  1. Switch and Update Master Branch
git checkout master
git pull upstream master