$> git init
Short History
Linus uses BitKeeper to manage Linux code
Ran into BitKeeper licensing issue
Liked functionality
Looked at CVS as how not to do things
April 5, 2005 - Linus sends out email showing first version
June 15, 2005 - Git used for Linux version control
Centralized VC vs. Distributed VC
Central Server
Remote
Server
SVN vs. Git
SVN Server
Git Server
SVN
Centralized
Checkouts
Branching
Conflict Resolution
No Offline support
Simple and easy to learn
GIT
DeCentralized
Fast, Reliable, Consistent
Branching / Checkouts
Conflict Resolutions
Offline support
Wide range of deployment hooks
SVN Commands
co - checkout
list
add
commit
update
Git Commands
init
clone
status
add
commit
push
pull / fetch
Git Work-flow
Remote
Server
Working
copy
Git Work-flow
Remote
Server
Working
copy
Stage
Area
> git add file-name
Git Work-flow
Remote
Server
Local
Repo
Working
copy
Stage
Area
> git commit -m ‘message’
Git Work-flow
Remote
Server
Local
Repo
Working
copy
Stage
Area
> git push origin master
Cloning a repository
> git clone url
> git clone git@github.com/username/myproject.git
> git clone https://github.com/username/myproject.git
> cd myproject
Git config
Global configuration
> git config --global user.name your.name
> git config --global user.email your.email
Project configuration
> cd myproject
> git config user.name your.name
> git config user.email your.email
Creating a new Repo
Initialized empty Git repository in ~/myproject/.git
[master (root-commit) 7106a52] my first commit
1 file changed, 1 insertion(+)
create mode 100644 README.txt
> mkdir myproject
> cd myproject
> git init
> touch README.txt
> git add .
> git commit -m 'my first commit'
Adding a remote server
> git remote add origin git@github.com/username/myproject.git
> git remote -v
origin git@github.com/username/myproject.git (fetch)
origin git@github.com/username/myproject.git (push)
Adding a remote server
> git remote add origin git@github.com/username/myproject.git
> git remote -v
origin git@github.com/username/myproject.git (fetch)
origin git@github.com/username/myproject.git (push)
> git remote add production git@github.com/live/myproject.git
> git remote -v
origin git@github.com/username/myproject.git (fetch)
origin git@github.com/username/myproject.git (push)
production git@github.com/live/project.git (fetch)
production git@github.com/live/project.git (push)
Adding files
Will stage your file to be committed
Conventionally `git add .` should be avoided
Will stage all your deleted files to be deleted
from repository
> git add -u
> git add [file-name | dot(.)]
Commit files
Will commit your file to your local repository
With inline message
Equivalent of [git add . + git commit -m]
> git commit
> git commit -m ‘message’
> git commit -am ‘message’
Pull
Will pull latest commits from the remote
repository and try to auto merge them
> git pull remote branch
> git pull origin master
Push
Will push your commits to the remote repository
Another dangerous command.
Overrides any previous commit/changes
> git push remote branch
> git push origin master
> git push -f origin master
Branching
Branches in Git basically is a collection of
commits
By default ‘master’ branch is available in all
repositories
> git branch
> git branch new-branch
> git branch -D your-branch
lists all branches
creates new branch
deletes a branch
master
A
> git commit –m ‘my first commit’
Branching
master
> git commit (x2)
A B C
Branching
bug123
master
> git checkout –b bug123
A B C
Branching
master
> git commit (x2)
A B C
D E
bug123
Branching
master
> git checkout master
A B C
D E
bug123
Branching
bug123
master
> git merge bug123
A B C D E
Branching
master
> git branch -d bug123
A B C D E
bug123
Branching
Advance branching and merging
Branches Illustrated
master
A B C D E
F G
bug456
master
A B C D E
F G
bug456
> git checkout master
Branches Illustrated
master
A B C D E
F G
> git merge bug456
H
bug456
Branches Illustrated
master
A B C D E
F G
bug456
Branches Illustrated
master
A B C D E
> git rebase master
F G
bug456
Branches Illustrated
master
A B C D E
> git checkout master
> git merge bug456
F G
bug456
Branches Illustrated
More git commands
Shows difference between working copy and
last revision
> git diff [file-name]
More git commands
Shows difference between working copy and
last revision
Shows a complete history of commits with
dates and author-name
> git diff [file-name]
> git log
More git commands
Shows who/when made changes to a file line-
by-line
> git blame [file-name]
More git commands
Shows who/when made changes to a file line-
by-line
Renames a branch
> git blame [file-name]
> git branch -m <old-name> <new-name>
More git commands
Remote branch tracking
Updates your local repository with remote
branches
> git fetch
More git commands
Stashing takes the dirty state of your working
directory - that is, your modified tracked files
and staged changes - and saves it on a stack
of unfinished changes that you can reapply at
any time.
> git stash
> git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be
committed)
#
# modified: lib/simplegit.rb
> git stash
Saved working directory and index state 
"WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")
> git status
# On branch master
nothing to commit, working directory clean
> git merge issue53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit
the result.
> git merge issue53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit
the result.
Merge Conflict
> git merge issue53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit
the result.
Merge Conflict
How to solve it?
<<<<<<< HEAD:index.html
<div id="footer">contact :
email.support@github.com</div>
=======
<div id="footer">
please contact us at support@github.com
</div>
>>>>>>> issue53:index.html
<div id="footer">
please contact us at email.support@github.com
</div>
<div id="footer">
please contact us at email.support@github.com
</div>
Installation
Windows
http://git-scm.com/download/win
Linux
$ sudo apt-get install git (Ubuntu)
$ yum install git
(CentOs)
Mac
http://git-scm.com/download/mac
Best Practices
Do not work on Master branch
Synchronize daily
Follow feature-wise branching
Avoid force push
Do not work on servers
e.g FTP
Tools
● Following are few very necessary tool for working
with git
> sudo apt-get install gitg
> sudo apt-get install git-gui
> sudo apt-get install meld
GitG (gitg)
Git-GUI (git gui)
Meld (git mergetool)
Thank You
- Waqar Baig
Special credits to
Rails Team

Lets Git Together

Editor's Notes

  • #23 Now lets see what this visually looks like. On my first commit I have A. The default branch that gets created with git is a branch names Master. This is just a default name. As I mentioned before, most everything in git is done by convention. Master does not mean anything special to git.
  • #24 We make a set of commits, moving master and our current pointer (*) along
  • #25 Suppose we want to work on a bug. We start by creating a local “story branch” for this work. Notice that the new branch is really just a pointer to the same commit (C) but our current pointer (*) is moved.
  • #26 Now we make commits and they move along, with the branch and current pointer following along.
  • #27 We can “checkout” to go back to the master branch. This is where I was freaked out the first time I did this. My IDE removed the changes I just made. It can be pretty startling, but don’t worry you didn’t lose anything.
  • #28 And then merge from the story branch, bringing those change histories together.
  • #29 And since we’re done with the story branch, we can delete it. This all happened locally, without affecting anyone upstream.
  • #31 Let’s consider another scenario. Here we created our bug story branch back off of (C). But some changes have happened in master (bug 123 which we just merged) since then. And we made a couple of commits in bug 456.
  • #32 Again, to merge, we checkout back to master which moves our (*) pointer.
  • #33 And now we merge, connecting the new (H) to both (E) and (G). Note that this merge, especially if there are conflicts, can be unpleasant to perform.
  • #34 Rebase flow - Let’s go back in time and look at another approach that git enables. So here we are ready to merge.
  • #35 Instead of merging, we “rebase”. What this means is something like this: 1. Take the changes we had made against (C) and undo them, but remember what they were 2. Re-apply them on (E) instead
  • #36 Now when we merge them, we get a nice linear flow. Also, the actual changeset ordering in the repository mirrors what actually happened. (F’) and (G’) come after E rather than in parallel to it. Also, there is one fewer snapshots in the repository.