Git GitHub
Git GitHub
• Have you ever wanted to see how much work is being done, and where, when and by
whom?
• Have you ever wanted to share your code, or let other people work on your code
simultaneously?
• Have you ever wanted to experiment with a new feature without interfering with
working code?
Version Control System
• Version control systems are software tools that helps in recording changes
made to files by keeping a track of modifications done to the code.
• Tagging
• Collaboration
Why Version Control system is so
Important?
• As we know that a software product is developed in collaboration by a group of developers
they might be located at different locations and each one of them contributes in some specific
kind of functionality/features.
• So in order to contribute to the product, they made modifications in the source code(either by
adding or removing).
• A version control system is a kind of software that helps the developer team to efficiently
communicate and manage(track) all the changes that have been made to the source code
along with the information like who made and what change has been made.
• A separate branch is created for every contributor who made the changes and the changes
aren’t merged into the original source code unless all are analyzed as soon as the changes are
green signalled they merged to the main source code. It not only keeps source code organized
but also improves productivity by making the development process smooth.
Types of Version Control Systems:
• Local Version Control Systems
• Centralized Version Control Systems: Centralized version control systems contain just one
repository and each user gets their own working copy. You need to commit to reflecting your
changes in the repository. It is possible for others to see your changes by updating.
• Git relies on the basis of distributed development of a software where more than one developer
may have access to the source code of a specific application and can modify changes to it which
may be seen by other developers..
• Every git working directory is a full-fledged repository with complete history and full version-
tracking capabilities, independent of network access or a central server.
• Git allows a team of people to work together, all using the same files. And it helps the team cope
up with the confusion that tends to happen when multiple people are editing the same files.
What is Repository
• Before you can work with Git, you have to initialize a repository
for your project and set it up so that Git will manage it.
Git Repository Structure
• It consists of 4 parts:
• Working directory : This is your local directory where you make the project (write code) and
make changes to it.
• Staging Area (or index) : this is an area where you first need to put your project before
committing
• Local Repository : this is your local repository where you commit changes to the
project before pushing them to central repository on Github. This is what is provided by
distributed version control system. This corresponds to the .git folder in our directory.
• Central Repository : This is the main project on the central server, a copy of which
is with every team member as local repository.
• All the repository structure is internal to Git and is transparent to the developer.
Commands
• These are a list of few commands that you can use frequently on github(git bash)
• git help
• Take help from github help section for different commands and other errors
• git config
• To set the basic configurations on github like your name and email.
• git config –-global user.name “Sachin Rawat”
• Sets configuration values for your user name on git.
• git config –-global user.email “[email protected]”
• Sets configuration values for your user email on git.
Commands
• mkdir store
• Create a directory if not created initially.
• cd store
• To go inside the directory and work upon its contents.
• git init
• To create a local git repository for us in our store folder.This
will help to manage the git commands for that particular
repository.
Commands
• git commit -m “Created a Readme.txt”
• To commit our changes(taking a snapshot) and providing a message to
remember for future reference.
• git log
• To check the history of commits for our reference.
• git status
• To see whats changed since last commit.It shows all the files that have been
added and modified and ready to be commmitted and files which are untracked
Commands
• Different ways to use add command:
• git add
• To add a specific list of files to staging area.
• git add –all
• To add all files of current directory to staging area.
• git add *.txt
• To add all text files of the current directory to staging area.
Commands
• git remove rm
• To remove a remote from our local repository.
• git push -u origin master
• To push all the contents of our local repository that belong to master
branch to the server(Global repository).
• git clone https://github.com/sachin123/MyAlgorithms.git
• To clone or make a local copy of the global repository in your system
(git clone command downloads the repository and creates a remote
named as origin which can be checked by command – git remote -v).
Commands
• git branch Testing
• To create a new branch named as Testing.
• git branch
• To see all the branches present and current branch that we are
working on.
• git checkout Testing
• To switch to branch Testing from master branch.
Commands
• git merge Testing
• To merge Testing branch with master branch.
• git branch -d Testing
• To delete Testing branch.
• git checkout -b admin
• To create a new branch admin and set it as current branch.
• git add Readme.txt
• To add a file Readme.txt to the staging area to track its changes.
Branching
• Git branching allows developers to diverge from the production version of
code to fix a bug or add a feature.
• You create branches to isolate your code changes, which you test before
merging to the main branch .
• There is nothing special about the main branch. It is the first branch made
when you initialize a Git repository using the git init command.
Branching
• As you create commits in the new branch, Git creates new pointers to track the
changes.
• The latest commits are now ahead of the main branch commits. As you continue to
make commits, each branch keeps track of its version of files.
• Git knows which branch you have checked out by using a special pointer called
HEAD.
• When you create a new branch, Git doesn’t immediately change the HEAD pointer
to the new branch. You’ll see HEAD in the commit history when you create
branches and view the commit log.
Branching
• When you create a commit, Git identifies that snapshot of files with
a unique SHA-1 hash. When you initially create a branch, Git creates
a new pointer to the same commit the main branch is currently on.
• Let’s examine both of these based on the branches and commit history
Fast forward merge
• Fast forward merge can be performed when there is a direct linear path from the source
branch to the target branch. In fast-forward merge, git simply moves the source branch pointer
to the target branch pointer without creating an extra merge commit.
• Next, we create a branch called feature branch. In git a branch is nothing but a pointer to a
commit. At this point both feature and master are pointing to the same commit.
Fast forward merge
• Now let us switch to the feature branch and do a couple of commits. Now
we need to bring the changes to the master branch. There is a linear path
from feature to master.
• In order to merge the changes to the master branch, all git has to do is to
change the pointer of master forward. This is what we call fast-forward
merge.
3-way merge
• Let us look at an example of a 3-way merge. In this example, the
Feature branch is two commits ahead of the Master branch.
3-way merge
• Before we merge it with Master, let us say we have added an
additional commit to the Master as shown in the below diagram.
3-way merge
• Due to the commit performed on the Master branch, both our
branches Master and Feature are now diverged.
• If git simply moves the Master pointer to the Feature pointer, then the latest
commit C6 performed on the Master branch will be lost.