Skip to main content

Git in short - A Practical Guide

Git is one of the popular software version control systems.

It helps to track modifications of files in a project and provides mechanisms to interact with the changes.

This article explains some set of practical GIT work flows in brief.

Section 1: Creation and management of a local GIT repository.

STEP 0: Set path to project directory where version controller to be initialized.

git init

Syntax :   git init

git init command initializes the current directory as a git repository.

A hidden .git will be created in the current folder, to store all tracking related information.

See the command line response on listing the files in the repository.

Added a file to the repository.

git status

Syntax :      git status

git status command show the current status of the repository.

The cmd line response indicates, the file is not tracked by git.

git add

Syntax :      git add

git add command moves the files under git's tracking.

Theoretically git add moves files to staging area,which is an intermediate tracking state between 'change-in progress'(modified files) and 'changes-completed'(committed files)

Git modification states

  • Working directory.
  • The directory where all your files live in. Only an explicit addition, will move the file under git tracking. Till then the files will be listed as untracked files.(see git status response above)

  • Staging area.
  • The git command git add tells git to track files. Staging area is an intermediate state where git is tracking a file but not saved it into local git storage space (git repository).

  • Git repository.
  • On every developmental check points, the files under tracking of git (files in staging area), can be wrap up together and push into git repository by executing the command git commit.

git commit

Syntax :      git commit -m "<CustomMessage>"

git commit saves changes in staging area into local git repository with a message given with -m flag.

So '' moved to git repository with commit id 'ea31455'.

Do a git log to see the context of previous commits.

See how to copy the what ever the content created in local git repository to remote a remote cloud repository(Here it is GitHub).

Login to GitHub and create a repository, get the repository URL.

git remote add origin

Syntax :      git remote add origin "<URL>"

This command sets the remote git repository to the location pointed by the appended URL.

git push

Syntax :      git push origin "<BranchName>"

It is a common trait in version control managed projects, to keep a finalized clean code in one space (called main branch), and take a copy of that (branch out to a working branch) to do experimental changes.

Once the code in working branch is fully furnished, it will merge with main branch.

The default branch for every git repository is called master

The changes discussed till yet were on local master branch. By appending origin as a master to git push command, the local changes will be pushed to master branch of remote repository.

See the changes reflected in remote GitHub repository.

Section 2: Working with remote GIT repository.

This section describes, how to copy, make changes and update back to a remote code repository.

git clone

Syntax :      git clone <AssociatedURLofTheRemoteRepository>

git clone down loads all resources/files from remote repository(remote storage space), to our local working directory.

See the cloned repository gitHub.

Make the required changes on theses files and see the repose of git status command.

The cloned repository, is the one pushed to remote GitHub, earlier in this document.

So we will modify the file, create on more AnUntrackedFile.txt and create and move a A_File_Already_Moved_To_Staging_Area.txt file to staging area(using git add).

And see the status.

git status

Syntax :      git status

git status will show current status of all non committed files in the repository.

Files listed under Changes to be committed are files in staging area.

The files under Changes not staged for commit, are modified files among already tracked files, and the changes on them not lifted to staging area.

The untracked files comes under third heading, those yet to be included under Git tracking.

Try: git diff command will show all tracked but not staged changes.

Move all untracked and modified files to staging area and commit them to local git repository.

(see git add and git commit operations in Section : 1)

To reflected the changes committed in local repository, it has to be pushed to remote repository.

There might multiple developers working on single project.

If any two of them changes same files, updating remote repository may lead into conflicts.

So to ensure conflicts free code, before delivering local changes, it is a good practice to update local repository with latest changes form remote repository.

Git may not allow to pull directly if there is a conflict between local changes and changes in remote repositories.

Git stash is used to isolate non committed changes to a separate space.

git stash

Syntax :      git stash

git stash moves all changes (except untracked files) to a isolated area.

After pulling the latest code, apply the stashed changed and remove conflicts if any(see git stash apply).

Still the untracked file is still not moved. (it won't create any conflict since it is a new file.)


  1. git stash list : to see list of stashed changes.
  2. git stash show -p stash{0} : to see changes in last stash.

git pull

Syntax :      git pull origin

Updates the code that you had cloned earlier with latest version of code available at remote repository.

git stash apply

Syntax :      git stash apply

The command apply the changes isolated earlier and show conflicts if any.

Remove conflicts if any, then add and commit the changes.

Add and commit the changes.

Avoid committing changes, if it does not required to be updated in remote repository.

git push

Syntax :      git push origin

Pushes the code to specific branch (here it is master branch)of remote repository.

Now the changes would have reflected in the remote repository.

References:      Git documentation.

Uncovered topics:     

  • User configurations(git config).
  • Undoing changes( git revert/ reset/ clean).
  • Branching techniques(git branch).
  • History rewriting (git rebase/ reflog ...)


Popular posts from this blog

Operating Systems | Scheduling Algorithms : Round Robin

Round Robin Features : Most popular algorithm of all Practically implementable Implementable with basic data structures like queue Extremely lesser starvation Optimum efficiency can be set by controlling time quantum The round robin algorithm will continuously switch between processes if a process in CPU (under execution) exceeds a time limit set by OS called time quantum . Flow Chart : Scheduler allocated process to for execution. CPU starts monitoring the execution time right after from allocation. If the process completes its execution before exceeding time quantum, OS forwards the process to termination state. else, the processes gets preempted once the time quantum limit exceeded and if the process finished at this moment, OS moves the process to termination state, else it moves to ready queue and iterates over the whole process listed above. Example : Consider the following table of processes and calculate completion time, turn around time and waiting tim

Operating Systems | Concept of shared memory and critical section

While printing your document opened in Adobe Acrobat Reader, if you gave a same command for document opened in Microsoft Word! How will it be? These are the real time situations where two processes competing together for same resources due to improper process synchronization. An unstructured access approval to these resources may lead to a abnormal output or a breach in information security or even it may cause for data loss. In the above case which might be a bunch of over-written papers coming out of the printer connected to that system. Shared Memory and Critical Section Resources or memory spaces where different processes have access to read, write or update at same time are known as shared memory . And the piece of program which tries to access the shared memory is known as Critical Section . There are situations where an operating system need to handle multiple request from critical sections of various processes in a machine , in order to maintain da

Operating Systems | Lock Variable Synchronization Mechanism

To learn lock variable synchronization mechanism, you have to understand different modes of execution of instructions in an operating system. Basically there are two modes of execution in a system, User Mode and Kernel Mode. The unrestricted access to any resources in the system, for executing instructions in Kernel Mode makes resources available only for most trusted basic level functions of operating system . Direct access of hardware or memory references are not allowed in User Mode , due security concerns over higher level applications. System APIs provide a path way to reference these resources. Features of Lock Variable A software logic implemented in User Mode. A Busy waiting method of synchronization. Multiple processes can be handled by single mechanism. There are two basic section for every synchronization method, which decides the nature of that algorithm, Entry and Exit sections. Arranged as shown. Here in lo