is a distributed version control system for tracking changes in source
code during any development. Here each developer has their code on their
local system. Whenever they want they can push the code into Github or Git
SVN is a centralised repository. Everyone use to connect to central repo
and code there. If there is any network issue between server and client,
developers cannot do anything using SVN.
are the areas or stages in GIT?
1. Workspace Area
: What ever files we add/create are initially in the Workspace Area.
2. Staging/Index Area
files when created are in workspace area. To add them to staging we use “ git
add -a or git add . Or git
add <filename> “
commands. When the files are in Staging or index area means that they are
ready to commit. Once the files are in Staging area, they can be put into
local repository using “git
commit <file1> -m “comment about the committee” . So the
files are into local repository.
is a git repository?
repository is a folder or directory which is initialized using "git init"
command which creates a '.git' sub-directory which has all the elements to
track the code changes.
is git branching?
of commits is called a branch. A branch is a collection of directories and
files. Default branch is master. When we create a branch from master, all
code will be copied to the new branch and there we do required
modifications like feature1 and
if everything is fine, we can merge it back to master or continue as
branch : this
command displays all the branches in alphabetical order.
before a branch represents current active branch where we are into
git branch <newbranch
name” will create a new branch.
-b <newbranch name will
create the new branch and also switches to that branch.
If we want to merge, we need two branches at least. It will merge all
files and directories from source branch to target branch(master in this
case). The command is : git
merge feature1. For this go
to master branch and
do the “git
merge feature1” . This merges all files from feature1 branch to
the master branch.
So to merge two
branches, go to the target branch(like master) and give the command as git
merge <branch to be merged>
To delete a
branch, use “git branch -d <branch name>” from other branch. You
shouldn’t be in the same branch when deleting.
-D <branch name>; -D is force deletion of a branch
branch also can be deleted from
all branches in alphabetical order
branch <new branch>
checkout -b <new branch>
and change to the new branch
diff <branch1> <branch2>
into the current branch
branch -d <branchname>
branch -D <branchname>
this branch forcibly
* git init : to initialise a git repo
* git config --global user.name “your-user-name”
* git config --global user.mail “email@example.com”
* git config --list
* git status => displays status of our files; if they are red, they are
in workspace, If they are green, ready to commit and are in staging/index
* git commit -m “label”
* git commit -am <label” => to add files to index and commit in
single command. Its like git add -a and git commit. Only old files which
are already committed or in staging area can be added and committed using
this. New fresh files cannot be added and committed.
* git log => will display the commits
* git status <sha code/commit ID> will display details of that
* git status pop
* git add -f <filename> to add a file to index area even though its
ignored with “.gitignore” file
* git checkout <branchname> will change the branch into the given
* git branch will display in which branch we are into
* git log —oneline will display git commits in one liner.
* git log -5
* git log -1 to display about the last commit
* git show sha1code
Earlier we have several version control systems in place but none of them
is as famous as SVN or GIT.
All version control systems works from central repository.
1. Source Code Control system(SCCS)-1972
2. Revision Control System - 1982
3. Concurrent Version Control System(CVS) 1986-1990)
4. Apache Subversion(SVN) - 2000 Opensource
5. BitKeeper SCM -2000 - Distributed version control system - Proprietary
6. GIT - Linus Torvols - 2005
All repositories work as pull the code from central repository, make
changes and submit the changes back to the central repository.
But in GIT, different users(or teams of users) maintain their own repos,
instead working from a central repo.
Changes are stored as "Change Sets" or "Patches".
Means tracks changes, not versions
Different from CVS and SVN, which track versions
Download it from http://git-scm.com
$HOME/.gitconfig - in windows
git config --system => to configure system wide
git config --global => to configure user configuration
git config => to configure project configuration
Ex: git config --global user.name "vjrid4"
git config --global user.email "firstname.lastname@example.org"
To list the configurations:
git config --list
git config user.name
How to push local directory contents to github.com
STEP 1: git init => which will initialize the local directory as a Git
STEP 2: git add . =>Stage the files in the current directory to new
STEP 3: git commit -m "First Commit" => commits the tracked changes and
prepares them to be pushed to a remote repo. To remove this commit and
modify the file, use 'git reset --soft HEAD~1' and commit and add the file
Label can be 50 char long as best practice with ticket number or bug fix..
STEP 4: to set remote github repo, copy the repo URL and execute command
as 'git remote add origin https://github.com/vjrid4/devops.git'
STEP 5: To display remote URL, you can use 'git remote -v'
STEP 6: Now push the changes to remote git repo using command 'git push
git log: what changes are commited
git log -n <number of commits>
git log -n 1
git log --since=2012-06-15
git log --until=2012-06-15
git log --author='vjrid4'
git log --grep='Init'
Typically git workflow starts adding a file to the working copy. Then move
it to staging index using git add <file.txt> and then commit changes
using git commit -m <about this commit>
Add/Edit a File (Working copy)
git add <file.name> (Staging Index)
commit -m (Repository)
How git refers to each commit or changes made to a repository or sets of
changes made to a repository. Change sets are snapshots those are the
checksum algorithms convert data into a simple number
same data always equals same checksum
data integrity is fundamental
changing data would change checksum
Git uses SHA-1 hash algorithm to create checksums
its a 40 character hexa string which is a commit ID which we can see in
Each commit number is mapped to a snapshot as discussed above. also each
commit has parent, author and message details along.
point to a commit. Point to tip of the current branch in repository.
last state of repo, what was last checked out
points to parent of next commit
HEAD is like tape recorder head. Wherever head is recording starts from
there. As same in git, if we commit changes, HEAD is at current repo and
this HEAD is the parent for next commit. Means changes will start from
here till next commit. so head is always point to recent commit.
there is a file in each repo in .git\HEAD. if we display the file, it
displays refs/heads/master. If we go to that path, and display
master, it gives the commit ID which is head.
changes to files:
go to current working copy direcotry
git status gives us the difference between working directory, staging
Index and repository.
If add new files and execute git status, it displays new files which are
added and asks to add them to staging o commit changes to repository.
so git status is a very good command to know what are the changes we made
from last commit and what are at staging Index and what are at working
Gives the difference
git diff <file name> gives changes made to pirticualr file.
git diff --staged [ in earlier versions(>1.7), its git diff --cached ]
git diff by default gives changes made to your working directory. It
doesnt list the changes from staging. To get the differences made to
staging, use the command git diff --staged.
git rm <file
to be deleted> => git remove a file
If we delete a file from the working directory, we need to tell git to
delete it from repo also using git rm command. If we directly use git rm
to delete a file, it deletes permenantly without moving it to trash.
Instead of “git rm”, if we delete a file using linux rm command, we can
get that file back using “git checkout <filename>”
Git rm is equal to rm <file> and commit -am <commit message>.
Git rm will delete file from working directory, staging area and local
repo. So it can’t be restored or recovered.
Renaming: there are two ways to do this. First is do it from operating
system and tell git to remove the old file. Second way is directly from
git using git mv original.file newnamed.file
If we use mv command to rename a file, it will delete the existing file
and creates a file with new name. So when we check the status, it says the
file is deleted from working directory and a new file is added to working
directory. So we need to add these to staging and then we have to commit.
So to overcome the above complex issue, we can use git mv command. It will
directly renames the file and adds the file to the staging. So only commit
is enough in this case.
Untracked files are just files created in working directory. when you say
"git status", these files are shown in RED Color means they were not yet
added to Staging.
Tracked files are the files added to Staging using "git add
git commit --amend --reset-author is the command to fix the author issues.
git push --set-upstream origin master
a Real Project:
git init in new directory where repo is initialized
git add .
git commit -am <Add to staging and commit using single command>
git can undo changes made to our working directory or staging directory.
file has been modified
git diff = shows what has changed
git checkout index.html
git checkout <fileto be restored>
git checkout <branchname>
git checkout -- index.html => -- represents to stay on current branch
and restore file index.html.
how to undo changes to staging index.
git reset HEAD <filename.to unstage.
commits to repository:
git commit ammed -m "revert last commit"
How to undo
changes from a previous commit:
Find the HASH from git log in which you made changes to that pirticular
file or branch and execute git checkout <commit code/SHA> --
git checkout <
git revert <commit code/SHA>
If we are developing, there are some unwanted files generated. For
example, If we are developing a ‘C/C++” program, lot of obj files will be
generated. If we want omit them adding to index area or local repository,
we have two option. One is we can implement the same at system level using
“/etc/gitignore” file or at directory level, you can create a file under
directory like “.gitignore”. Include the files which you want to ignore
!index.php => as we gave *.php to ignore, but want to override the file
index.php with “!” Mark .
Now git add . And git commit -m “git ignore file added”
It will be in 3 ways. They are soft, mix and Hard type of removal
** git reset —soft <SHA1 Commit code>. => files will be
removed from local repository
** git reset —mix <SHA1 Commit code> => files
will be removed from staging area and local repository
** git reset —hard <SHA1 Commit code> => files will be
removed from working directory, staging area and local repository.
So we must take care while working with git reset command, as it will also
remove the files related to the commits. So when we do a reset, HEAD will
point to the <SHA1 commit code>. Prior to the SHA1 commit code will