DEV Community

Cover image for A to Z of git and GitHub
Suraj Pandey
Suraj Pandey

Posted on

A to Z of git and GitHub

Before diving into what is git and GitHub lets understand what is “version control system”.

Version Control System

It is a fancy term used to represent management of the changes in project development lifecycle. Also known as source control or revision control.
This practice allows developers to track entire history of changes and who made them, if at any point of time incompatible changes are pushed into main codebase then it can be easily rolled back.

Collaboration is made possible because of version control systems, it can be centralized where tracked files are stored in a single server or distributed where tracked files can be cloned into multiple locations and also can be accessed from multiple locations.

Well known VCS tools are :
Git, Subversion and Mercurial

git

Among all git is the most popular option among developers. Also it is an open source distributed VCS.
Take a look at git source code here.
git provide GUI and command line tool.

I recommend using command line as it offers to use git more freely with all set of commands.

There are 4 stages we encounter when using git:

  • Untracked : files which are not being tracked.
  • Unmodified : files which are not modified but git is tracking them.
  • Modified : files contains changes and git is tracking them.
  • Staged : files are ready to commit.

In development process we create a new file to write code which are in untracked stage, after done with writing code we may want to commit this change so that later if something wrong happens we can revert back. File is moved to staged area and committed.
Now this file is in unmodified stage and if developer want to make any changes it will be under modified stage indicating there are some changes which developer may want to commit.
Each commit is recorded in history of the project(repository).

GitHub

Repositories are like folders which contains files and different versions of a project.

GitHub is a platform where we can host these repositories and others can collaborate.
Similar platform which provide same kind of services are: Gitbucket, Bitbucket, GitLab and many more

Others have read only access to public repositories on specific account in GitHub until or unless proper access is not provided to others.

Command ;

— What that command does

Once git is installed you will need to configure author information locally or globally, Author details are necessary before committing so,

git config — global user.name “[name]” ;

— Sets the name of author to your commit transactions

git config — global user.email “[email address]” ;

— Sets the email of author to your commit transactions

git config –global -l ;

— there are many other configurations, which can be listed

When we proceed with our project we have to initialize it as git repository first.

git init [projectName] ;

— initialize git repository

In the development process many files may be added or already tracked files may be edited, those files can be added to stage area. * means add all files to stage area

git add [fileName] or git add * ;

— add files to stage area

git restore — -staged [filename];

— remove specified file from stage area

Sometimes we may want to exclude files from tracking ,like files which are only required in development environment and others. Path of those files can be added in “.gitignore” file and it will be ignored by git.

git commit -m “commit message” ;

— commit staged changes to project version history

git reset [commitHash] ;

— remove commits done after specified commit preserving changes locally

git status ;

— at any point of time status of files can be checked

git log ;

— at any point of time logs of repository can be checked

git clone [url] ;

— clone a git repository to your local system

Branches are one of the superpower of git which helps to create parallel version of main repository and work in that without affecting main branch.

git branch [branchName] ;

— creates a new branch, -d can be used to delete a branch

git checkout [branchName] ;

— switch to specified branch, -b can be used to create and switch to it at the same time.

git merge [branchName] ;

— merge specified branch changes into current branch

Stash area is a place where changes can be stored temporarily without committing changes into repository. It can be used in a place where we want to try out many alternative solutions without committing any changes. Stash area is like a STACK, every stashed item is placed on top.

git stash -m “stash message” ;

— move changes to stash area and have clean codebase, -a can be used to stash untracked items[all] also

git stash pop ;

— bring back changes from stash area, — index n can be used to bring back specific stash item

Repositories in local and repositories hosted in GitHub can be synchronized , in this operation every repository is associated with unique URL, “remote ” command is used to work with URLs in git. After creating a repository in GitHub and project development is done locally, Now it is the time to push/sync local repository to remote repository in GitHub.

git remote add origin [RepositoryURL] ;

— map URL to origin[naming convention] and when dealing with local and remote repositories this origin name can be used in place of entire url.

git fetch ;

— gets history updates from remote repository

git pull ;

— updates working directory with commits in remote repository, it is combination of fetch and merge

git push ;

— updates remote repository with all the commits in local repository

In both, push and pull commands which branch to push and where to push can be specified
git push [whereToPush] [whichBranchToPush]

git push origin main

git pull origin main

Forking a repository means copying a repository from someone else’s account to your GitHub account, cause while making contribution we cannot directly push our commits to their repository that will raise security concerns.

Once repository in our GitHub account we can make changes as we like and when we want to merge it to original codebase of this forked repository we can raise a Pull Request and maintainers of that repository may review and merge it into their code base.

Do check out GitHub student developer pack

Top comments (0)