You are on page 1of 38

From SVN to Git

with a pinch of GitHub

Marco De Stefano

!"

SVN features

!! Centralized version control !! Commit as atomic operation !! Versioning mantained for directories and file metadata !! Client/Server protocol sends diffs ! Renaming of files causes loss of the history ! .svn directories in the whole project ! Lack of management tools (e.g. deleting part of the history)

SVN commands
!! >svn checkout (co) !! >svn commit (ci) !! >svn status (st) !! >svn update !! >svn revert !! >svn add/rm !! >svnadmin create !!

Centralized vs. Distributed version control

!! Centralized VCS systems are designed with the intent that there is One

True Source that is Blessed, and therefore Good. All developers work (checkout) from that source, and then add (commit) their changes, which then become similarly Blessed.
!! Distributed VCS systems are designed with the intent that one repository

is as good as any other, and that merges from one repository to another are just another form of communication. Any semantic value as to which repository should be trusted is imposed from the outside by process, not by the software itself.
!! The real choice between using one type or the other is organizational - if

your project or organization wants centralized control, then a DVCS is a non-starter. If your developers are expected to work all over the country/ world, without secure broadband connections to a central repository, then DVCS is probably your salvation. If you need both, you're fu***d.

Centralized vs. Distributed version control

Centralized vs. Distributed version control

Git features

!! Distributed version control !! Strong support for non-linear development (branches and merges) !! Efficient handling of large projects (e.g. Linux Kernel) !! Pluggable merge strategies !! .git directory in project root ! More disk space required ! Not for Windows # (but there is a native porting, msysgit)

Git commands
!! >git init
!! Creates a .git subdirectory in your project !! Your working directory becomes a git repository

You use git init to make an existing directory of content into a new Git repository. You can do this in any directory at any time, completely locally

Git commands
!! >git clone <url> [path]
!! Copies a project in your working directory !! It copies all the project history locally !! You will get a directory with the main branch of the project

You use git clone to get a local copy of a Git repository

Git commands
!! >git add [filepattern]
!! Adds file contents to your staging area !! Used for new files and modifications to existing files !! Possible to skip this operation (with commit a option)

You use git add to start tracking files and to stage changes to already tracked files i.e. whenever you want to include file changes in the next commit

Git commands
!! >git status [-s]
!! Shows the status of the files (working directory and staging area) !! Gives context and hints displays which (but not how) files changed !! -s option gives short output (with no hints similar to svn st)

You use git status to see if anything has been modified and/or staged since the last commit

Git commands
!! >git diff [--cached / HEAD] [--stat] [filepath]
!! Shows a patch of content changed since last commit and not yet staged !! --cached option shows the staged changes, HEAD shows both staged and

unstaged changes
!! --stat option gives short output

You use git diff to see how (line by line) files have been modified and/or staged since the last commit

Git commands
!! >git commit [-a] [-m commit message]
!! Records a snapshot of the staging area in the current branch !! -a option adds to the snapshot all the unstaged changes !! -m option allows to insert the commit message directly

You use git commit to record a snapshot of the staged changes. This snapshot can then be compared, shared and reverted to, if you need to

Git commands
!! >git reset [--hard] <HEAD/commit> [-- files]
!! Unstages changes that have been staged !! With --hard options, the repository changes to the last commit state !! Possible to unstage single files

You use git reset HEAD to unstage files previously staged with git add, to avoid including them in the next commit

Git commands
!! >git rm [--cached] [files]
!! Removes files from the staging area and from the working directory !! --cached option allows to retain the file in the working directory

You use git rm to remove files from being tracked in Git

Git commands
!! >git mv <source> <destination>
!! Has the same behaviour of:
!! !! !!

>git rm --cached <source> >mv <source> <destination> >git add <destination>

You use git mv to rename files under version control

Git commands
!! >git branch [-d <branchname>] [branchname]
!! A branch is a context where you can switch !! Without arguments, it lists out the local branches and points out the current

one

!! With a branch name, it creates a new branch the default one is named master !! -d option will delete branchname

You use git branch to list current branches, create new branches and delete unnecessary branches

Git commands
!! >git checkout [[-b] <branchname>]/ [-- files]
!! Swithces to branchname !! -b option will create branchname and switch immediately to it !! -- followed by one or more files, reverts the content of these files to the

last commit state

You use git checkout to switch among branches, optionally creating them; you also use it to revert the data of any file to the last commit

Git commands
!!>git merge <branchname>
!! Merges the diffs between the current branch and branchname into the

current one
!! The branch branchname doesnt change

You use git merge to combine another branch context into the current one

Git commands
!! >git log [--oneline] [--graph] [--decorate] [branchname]
!! Shows commit history of branchname (the current one if not specified) !! The --oneline options shows only the first row of the commit message !! The --graph option shows a graph of branches and merges !! The --decorate option shows information on branches involved in merges

You use git log to list out the commit history

Git commands
!! >git remote [-v] [add <url> <alias>]/[rm <alias>]
!! Lists the remote repository aliases stored, -v options also shows the urls !! Possible to have different push and fetch urls !! add allows to add the repository at url under the local-remote alias !! rm allows to remove the alias local-remote

You use git remote to list out the remote repositories and the urls theyre using. You can also add and remove local-remote repositories

Git commands
!! >git fetch <alias/--all>
!! Downloads branches and data from a remote repository !! --all option allows to download data from all remote repositories !! Cannot checkout a remote repository

You use git fetch to synchronize your repository with a remote repository, fetching all the data not already existing in your local repository

Git commands
!!>git pull <alias>/<--all>
!! Fetches from a remote repository and tries to merge it into the current branch !! Has the same behaviour of:
!! !!

>git fetch <alias> >git merge <alias>

You use git pull to synchronize your repository with a remote repository, fetching all the data not already existing in your local repository and merging it into the current branch

Git commands
!!>git push <alias> [branch]
!! Pushes local branches and data to a remote repository !! If no branch is specified, the current branch is used !! The local branch to push has to be up-to-date

You use git push to update a remote repository with changes made locally

Git commands
!!>git revert [-n] <HEAD/commit>
!! Reverts the contents of the current branch to the commit state !! It makes a new commit on the current branch! !! With -n options the data is reverted and isnt committed

You use git revert to revert the data in the current branch to a specified state

Git commands
!!>git stash <save/pop>
!! Allows to record the current state of the working directory !! Doesnt make a commit !! Save allows to create a stash, pop puts the stashed changes back on the

working directory
You use git stash to temporarily stash (and afterwords recover) the data in the current branch, e.g to proceed with a pull request

Tips
!! Enable console colors !! >git config --global color.diff auto !! >git config --global color.status auto !! >git config --global color.branch auto !! Set name and email !! >git config --global user.name <Name and Surname> !! >git config --global user.email <email> !! Set aliases for commands, e.g.: !! >git config --global alias.history log --oneline --graph --decorate !! >git config --global alias.st status -s !! >git config --global alias.ci commit -m !! >git config --global alias.co checkout

GitHub

!! Web-based hosting service for software

development projects that use Git


!! Possible to have public repositories (free if they

require less than 300MB)

GitHub Create a repo

GitHub Create a repo


!! >mkdir help-example !! >cd help-example !! >git init !! >touch README !! >git commit -a m first commit !! >git remote add origin git@github.com:tekkub/help-

example.git

!! >git push origin master

GitHub Push data


!! I cant push, Permission denied (public key) !! To push data, you have to set up a pair of SSH keys,

and add your public key in the account options on GitHub

!! You can also configure your GitHub account locally: !! >git config --global github.user <username> !! >git config --global github.token <token> !! You can find your token under your GitHub account

settings

A simple scenario
!! Alice created her repo, test, on github (see previous

slides), containing these files:

!! README foo.txt bar.txt test.c!

!! Bob joins her and he has write access to the

repository:

!! >git clone git@github.com/alice/test.git test!

!! Bob works locally, and makes a branch:


!! >cd test! !! >git checkout b text-changes!

!! Now he has 2 branches: master and text-changes

A simple scenario
!! Bob modifies foo.txt and bar.txt; after that he runs:
!! >git add foo.txt! !! >git status s! !! M bar.txt! !! M foo.txt!

!! The file bar.txt has been modified but not already

staged; however Bob runs:


!! >git status s

!! >git commit a m modified txt files!

gives no output

!! These files have been modified only in text-changes

branch

A simple scenario
!! Bob wants to modify test.c in a new branch, without

having the *.txt files modified


!! >git checkout master! !! >git branch src-changes! !! >git checkout src-changes!

!! Bob modifies the file test.c and after that he runs:


!! >git add .! !! >git commit m modified test file!

!! Bob has 3 branches: master, text-changes, src-changes

A simple scenario
!! Meanwhile, Alice modifies README and test.c, and

runs:
!! >git commit a m modified readme and test.c! !! >git push origin!

!! Bob tries to merge text-changes with src-changes


!! >git branch!
!!
!! !!

master! * src-changes! text-changes!

!! >git merge text-changes!

A simple scenario
!! Bob tries to push the changes into the origin
!! >git checkout master! !! >git merge src-changes! !! >git push origin!

!! However, Bob cant push data to the origin, because he

is not up-to-date
!! >git pull!

!! What happens with the file test.c? Both Alice and Bob

modified it

A simple scenario
!! Git tries to merge contents and its very likely to be able

to do it
!! If Git cant merge correctly, Bob will see the classic merging

style on his copy of test.c:


!! !! !! !! !!

<<<<<<<<< yours: test.c! //Bobs content! =========! //Alices content! >>>>>>>>> theirs: test.c!

!! After merging, Bob can push his data to the origin


!! >git push origin!

What else?
!! Git Reference !! Git Tutorial !! Git Users Manual !! Pro Git Book !! Git Community Book !! GitHub Help !! Egit - Eclipse plugin !! Graphical frontend: !! Windows, Linux (gtk), Mac OSX, Multi-platform

You might also like