SVN 2 Git
SVN 2 Git
SVN 2 Git
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 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.
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
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
Git commands
!! >git mv <source> <destination>
!! Has the same behaviour of:
!! !! !!
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
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
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:
!! !!
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
example.git
!! 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
repository:
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!
gives no output
branch
A simple scenario
!! Bob wants to modify test.c in a new branch, without
A simple scenario
!! Meanwhile, Alice modifies README and test.c, and
runs:
!! >git commit a m modified readme and test.c! !! >git push origin!
A simple scenario
!! Bob tries to push the changes into the origin
!! >git checkout master! !! >git merge src-changes! !! >git push origin!
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
<<<<<<<<< yours: test.c! //Bobs content! =========! //Alices content! >>>>>>>>> theirs: test.c!
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