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 README.md 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 README.md 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 'README.md' 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 README.md 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.)

Try:

  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 ...)

Comments

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 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 data consistency in resources