How to merge on subversion

In this post we will show how to do a merge between a branch and the trunk.

In a previous post we saw how to use subversion from the command line to manage our project repositories, now we will see how to do merge from a branch to a trunk.

We use branches for new features to be developed, while the trunk is normally used as the main development thread. To do a merge we should use the following commands:

1. Check out a copy of the trunk:

user@unix:~$ svn co

2. Now, we do a check out of the branch we want to merge:

user@unix:~$ svn co

3. We should change the work copy to the last branch and find in what revision we are:

user@unix:~$ cd /path/to/branches/1.0.1
user@unix:~$ svn log --stop-on-copy

This last command should show us the changes those did made since the creation branch. Take note of that number (should be in the way of rXXX, where XXX is the revision number).

4. Change our work copy to the trunk and do an update:

user@unix:~$ cd /path/to/trunk
user@unix:~$ svn update

That update will update our work copy to the most recient revision, and it will tell us what revision is. We should take note of this revision number (“at revision YYY”).

5. Now, we could simulate the merge:

user@unix:~$ svn merge --dry-run -r XXX:YYY

The –dry-run option simulates the merge, but it does not make it, in that way we can see what files will be merged and if we should resolve some conflicts.

6. After review the simulation output, we should do the merge:

user@unix:~$ svn merge -r XXX:YYY

7. And at the end, do a commit to the repository:

user@unix:~$ svn ci -m "MERGE myProject from branch 1.0.1"

That is all, the merge is done.

NOTE: Take in account that the url used are just samples.

How to use Subversion from command line

Here we will see how to use Subversion from CLI (Command Line Interface).

As you may know, there are SVN clients like Tortoise for Windows or RabbitVCS for Linux, but in this post we will only cover CLI (my favourite way ;-)).

Initialize a project work copy in my local machine: svn checkout

To get a work copy of source files in their last version we need to execute a process to copy all the files in our file system, this is accomplished with the checkout command:

user@unix:$ svn checkout hasheado-repo --username=hasheado

The above command will copy all files in our file system and all files are ready to be edited. Now, you can edit existing files, create new files and/or directories or remove files locally. Keep on mind that everything change we make, it only affects our local copy until we commit the changes to the svn repository.

Add files/directories: svn add

We can add a new file to the repository after create it and editing it in our local copy, or well, add a directory with or without content using the svn add command. This command will add the files/directories from your local copy and they will be added to the repo in the next commit. Also, we can change our minds and revert whatever we do not want using the svn revert command.

user@unix:$ svn add css/style.css
user@unix:$ svn revert

View file information: svn blame and svn cat

We can see the information about the author and revision number for specific files, using the svn blame FILENAME command; each line in the file is displayed at the beginning with the author (svn username) and revision number of the last change for the line.

If we want to see the differences in an specific file before to commit our changes, we can run the svn cat command.

Unlock a work copy

Sometimes we can see a “working copy locked” error. So, to unlock it, we can run:

user@unix:$ svn cleanup

Copy a file/directory: svn copy

Sometimes we have the need to just copy a file, to do this we can use the svn copy command as we can see below:

user@unix:$ svn copy SRC DST

Delete a file/directory: svn delete

If we want to delete a file or directory we can use:

user@unix:$ svn delete FILENAME

And to persist the deletion we need to commit.

Export a directory without .svn files: svn export

With this command we can extract a work copy with no version (without .svn files), to export a directory without .svn files we can run:

user@unix:$ svn export [-r REV] [PATH]

This will export a non-version work copy from the specified repository, we can specify the version as well, if no version is specified the head revision (HEAD) is exported. If the PATH is omitted, the last url is used to export.

user@unix:$ svn export PATH1 PATH2

SVN help

We can use the SVN help to see how to use every command:

user@unix:$ svn help [SUBCOMMAND...]

Send changes to repository

After we have made the changes locally in our files and/or directories, we should commit those changes to the repository.

Commit the changes: svn commit

To commit the changes to the repo:

user@unix:$ svn commit -m "Type your comment here" [files]

If we do not include any comment in the commit, we should add it in the default text editor which is triggered before SVN can complete the commit. All commits are logged into the SVN log.

Show commit logs: svn log

If we want to see the history of files or directories in our local copy or in the repo to track the revisions we can run:

user@unix:$ svn log [PATH]

The result is information about the files/directories, starting with the most current revision and showing information like the commit’s messages and authors.

Merge changes: svn merge

We can run the svn merge command to tell to Subversion that it should merge the last versions of the repo files into our local work copy.

Working with the repository

Create a new directory: svn mkdir

To create a new directory in your local work copy:

user@unix:$ svn mkdir PATH

Or, to create a new directory in the repo:

user@unix:$ svn mkdir URL

The end part of PATH or URL determines the new directory’s name. The new directory in the repo is created with an inmediate commit which requires a commit’s message.

Move a file/directory: svn move

We can move a file/directory using the svn move SRC DST command, with SRC being the source and DST the destiny. This command is equal to make a svn copy followed by a svn delete.

Resolve conflicts: svn resolved

In some situations we can get a conflict while we update our work copy. If this is the case, we need to resolve the conflict and then mark it as resolved. To do that we need to run:

user@unix:$ svn resolved PATH

SVN status

A good practice is to review our changes before to commit them, for that we can run svn status to print the file/directory status in our local copy.

Update our local copy: svn update

Another good practice is to update the local work copy every time we start working in the project, running:

user@unix:$ svn update [PATH...]

The updated items with their status are displayed as:
* A = A file was added to the local copy.
* U = A file was updated in our local copy.
* D = A file was deleted in our local copy.
* R = A file was replaced in our local copy.
* G = A file was successfully merged.
* C = A file has conflicts that we need to resolve.

Branching and Tagging

The project trunk is normally the main development thread, while the branches are used to different variants of that main development thread. For instance, when we want to build a new feature we can create a new branch to work on it. A tag is a way to group stable versions of the project. Although, the branches and tags are created using the svn copy command, their concepts are totally different. To create a branch/tag we have to run:

user@unix:$ svn copy SRC DST -m "Type your message here"