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