Git for the SVN (subversion) users

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 The Repository.

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

$ git init
$ git add .
$ git commit -m "Initial commit, New Repo"

SVN

$ 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?

In both Git and SVN, we can clone either using the direct http/s url of repository or using the ssh.

GIT

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://git@example.com/path/to/the/git-repo.git 
OR
$ git clone https://example.com/path/to/git-repo.git

SVN

$ svn svn+ssh://svn@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?

GIT
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

$ svn log

How to commit changes?

In both Git and 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 rm command.

To see local uncommitted changes in both GIT and SVN repository

$ svn status
$ git status

To see the diff:

$ svn diff
$ git diff

To add or stage the file:

GIT

$ 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

$ svn add 
$ svn rm  

How to commit?

Now you have staged your changes, its time to commit them to repository

GIT

$ 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

$ git push 
# You can also specify the on which remote branch changes should go, like
$ git push  

SVN

$ svn commit -m "message"

How to create branch or tags?

GIT
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

$ 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

$ git checkout 
OR While creating the branch itself
$ git checkout -b  /

SVN

$ svn switch http://example.com/svn/branches/

How to list branches?

GIT

# Show all local branches
$ git branch
# Show all local + remote branches
$ git branch -a

SVN

$ svn list http://example.com/svn/branches/

How to get information about the Repository/branch/origin?

GIT

$ git remote show origin
OR
$ git remote show 

SVN

$ svn info

How to create tags?

GIT

$ git tag -a 

SVN

$ svn copy http://example.com/svn/trunk http://example.com/svn/tags/

How to Delete Branches?

GIT

$ 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

$ svn delete http://example.com/svn/trunk http://example.com/svn/branches/ -m "messages"

How to Merge Changes?

GIT

# This will merge the given branch to your local current branch
$ git merge 

SVN

$ 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

$ git add 

SVN

$ svn resolved 

How to update your local copy with latest changes

GIT
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 master>

$ 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

$ svn update

How to rename files?

GIT

$ git mv  
OR
$ git mv  
# Once you have moved a file then you need to commit that as well. 
# use --dry-run option to see what would happen with original command

SVN

$ 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 !!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.