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 RobinFeatures : Most popular algorithm of allPractically implementableImplementable with basic data structures like queueExtremely lesser starvationOptimum efficiency can be set by controlling time quantumThe 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 time using Round Robin algorithm.Assumpt…

Operating Systems | Scheduling Algorithms : SJF

Shortest Job First(SJF)SJF algorithm schedules the shortest job(low burst time) available on main memory at the time of context switching in ready state . It can be either preemptive or non-preemptive depending on the nature of scheduler.Since the SJF select the job which is shorted among available jobs on main memory, Non-preemptive SJF algorithm will have larger waiting time if a job with longer burst time get scheduled first. In short SJF is also vulnerable to Convoy EffectTo find shortest process among min-heap would be the best data structure to be usedExample : Consider the following table of processes and calculate completion time, turn around time and waiting time using SJF algorithm with the following assumptions. No preemption.No I/O request will be there from any process.Arrival time is relative to CPU on time.Process NumberATBT103222324442View Answer

The execution of processes can be visualized as follows : By analyzing above representation completion time, turn around ti…

Operating Systems | Concept of Process

Hard disk drive of the system in called primary memory and secondary memory is Random Access Memory(RAM). Either a program written in High Level Language(HLL) or a executable code generated by the sequence of works done by pre-processor, compiler and assembler, resides in secondary memory of the system. To start an execution, operating system allocates some space in the main memory, for the program to be executed and loads the program in the secondary memory to the allocated space. The piece of work which is loaded by the operating system to the main memory in order to execute program is called processEvery program loaded by operating system will create a focus boundary(or process body), a partitioned memory area where all memory requirements for the execution of program is satisfied.Variable which will not change its value through out life time of process is called static variable and variables which are globally accessible in a process known as global variable.Heap area is reserved f…