Git for the SVN (subversion) users
Git and SVN (subversion) both are excellent versioning control system But there are times when a project runs on Git where else the other one is on SVN. If we talk about the difference between Git and SVN, it certainly needs a separate post but majorly Git is Distributed Revision Control System where as SVN is centralized.
In case of Git we actually work in 3 main states,
The Working Directory,
The Staging Area and
The .git Repository But in case of SVN its only 2
The working Directory and
In this post, we will quickly learn the commands that perform the similar jobs in both the VCS. We will discuss the Git and SVN in-depth in separate posts.
How to create a Repository?
$ git init $ git add . $ git commit -m "Initial commit, New Repo"
$ svnadmin create /path/to/repo $ svn import /path/to/Project http://svn_repository_url trunk -m "Initial Commit, Importing project"
How to clone a Repository?
SVN, we can clone either using the direct
http/s url of repository or using the
Git clone will setup the full-fledge repository to your local not just the working copy. This means, now your repository can act as
origin and someone can clone from your repository as well, just like a regular git repo.
$ git clone ssh://firstname.lastname@example.org/path/to/the/git-repo.git OR $ git clone https://example.com/path/to/git-repo.git
$ svn svn+ssh://email@example.com/svn/trunk OR $ svn checkout http://svn.example.com/svn/trunk OR $ svn co http://svn.example.com/svn/trunk
How to see logs?
As you posses local copy of Repository, Git
log shows the latest available log at the repository. You need to sync your repository with the Origin to get the latest logs.
Git pull will do that.
$ git log
$ svn log
How to commit changes?
SVN, we use
add to create new files or directory in the repository. It will stage your changes. We can add file using
add command and remove files using
To see local uncommitted changes in both GIT and SVN repository
$ svn status $ git status
To see the
$ svn diff $ git diff
To add or stage the file:
$ git add
.. $ git rm
To remove the untracked files (those not are committed) from the Repository
$ git clean $ git clean --dry-run # Don't actually remove anything, just show what would be done.
$ svn add
$ svn rm
How to commit?
Now you have staged your changes, its time to commit them to repository
$ git commit -m "message"
Tip: If you want to stage and commit all your files, Instead of adding them manually use
-a flag while committing. like:
$ git commit -a -m "message"
In order to update the remote repository (default origin) with your committed changes, so that other can fetch that, you need to use
$ git push # You can also specify the on which remote branch changes should go, like $ git push
$ svn commit -m "message"
How to create branch or tags?
In Git, branches are merely pointers and more powerful than in branches in SVN which is the directories and copy of the repository itself. In Git, you always work on a branch. By default, when you create a new repo or clone, you remains in
master branch. If you do
git branch, it will show you the current branch you are in.
So let’s have a look, How to create a new Branch.
$ git branch
Tip: If you want to switch immediately after creating a branch, Do
git checkout -b
$ git checkout -b
$ svn copy http://example.com/svn/trunk/ http://example.com/svn/branches/
-m "message" OR $ svn cp http://example.com/svn/trunk/ http://example.com/svn/branches/ -m "message"
How to switch in branches?
$ git checkout
OR While creating the branch itself $ git checkout -b /
$ svn switch http://example.com/svn/branches/
How to list branches?
# Show all local branches $ git branch # Show all local + remote branches $ git branch -a
$ svn list http://example.com/svn/branches/
How to get information about the Repository/branch/origin?
$ git remote show origin OR $ git remote show
$ svn info
How to create tags?
$ git tag -a
$ svn copy http://example.com/svn/trunk http://example.com/svn/tags/
How to Delete Branches?
$ git branch -d
# make sure you are not deleting the branch you are in. Although you will not able to :) # If branch has any pending changes to commit. # It Deletes a branch irrespective of its merged status $ git branch -D
$ svn delete http://example.com/svn/trunk http://example.com/svn/branches/
How to Merge Changes?
# This will merge the given branch to your local current branch $ git merge
$ svn merge http://example.com/svn/branches/
While merging, If you got
conflicts, then remove the conflicts from the file and then mark the files resolved like
$ git add
$ svn resolved
How to update your local copy with latest changes
If you want to download and merge new changes from the remote repo then do
git pull. Git pull internally does two things, first it fetches the latest changes from the remote repo and then it merges them in to your current local branch.
By default, Git operate in the context of your remote repo
origin and branch
$ git pull # OR If you want to pull a particular remote branch, then $ git pull
/ # If you simply want to download the changes and want to verify before merge $ git fetch OR $ git fetch / # After Git fetch you can do git diff to inspect changes before merging them.
$ svn update
How to rename files?
$ git mv
$ svn rename
How to push your local branch to the remote repository in Git
$ git push -u
So, that was the quick overview of GIT and SVN. We will see advance usage of GIT like setting upstream, cherry-pick, setting diff editor in further posts.
Till then have fun and Happy Coding !!