Wednesday, 25th April 2012
Follow WikiJava on twitter now. @Wikijava

Revisioning your project using GIT

From WikiJava

Jump to: navigation, search


In this article I'll show you few techniques I use for revisioning projects using GIT, which will allow you to do several nice things, like merging your local git with an SVN project, or publish your Git repository so that it's accessible on the web, without having a dedicated (maybe expensive) dedicated repository.

Contents

versioning a project in GIT

This is very simple, and it is described in so many places around the web that I won't be going too much in detail on this. What is important is to use this procedure as early as possible in your project, so that you start saving the history of your files very early and have complete history. You can use the next techniques only after you are already versioning your project, so, again, it's important to do this asap.

This procedure will revision your project on site, so it won't require anything more than your project directory. No special servers or strange installations. (Ok Git works only on Unix like systems, so if you are a windows use you may need to install it via cygwin, but this is very easy as well].

Ok, let's start with the directions, from the very beginning.

You just started writing your project, have just written the first test class, or maybe just used the maven archetype to get your starting project structure (check out the article Starting a Java project with Maven 2, to see how).

You have your project on your disk, at some consistent state (ie. it builds, and the unit tests you created are successful), you need now to start versioning it.

First step is to initialize the local repository. You do this with:

  • Changing to the project directory (in the prompt, and using cygwin if you are a windows user)
  • Enter the command:
    git init

This will create a .git subdirectory containing the empty git repository, ready to store the versions of your project. Nothing is stored now. You will need to add them manually.

Your project directory probably also contains some files you want to keep out of the repository, so that you don't remember their history. These are generally the files generated by the builds (target or bin directories, log files) but also the operating system files such as (files .DS_Store, Desktop.ini, Thumbs.db ... ). There's a very easy way in Git to exclude files or directories from versioning. Just: add their name to the .gitignore file. it accepts wildcards too.

The commands are:

  • echo <filename not to revision> >> .gitignore
  • git status
  • git add <filename to revision>
  • git commit -m "commit message"

In the snippet below I show you how I did it.

dongiulio@host:~]$cd my-project/
[dongiulio@host:~/my-project]$ls
pom.xml src
[dongiulio@host:~/my-project]$git init
Initialized empty Git repository in /Users/dongiulio/my-project/.git/
[dongiulio@host:~/my-project]$git st
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	fileIdontWantToVersion.txt
#	pom.xml
#	src/
nothing added to commit but untracked files present (use "git add" to track)
[dongiulio@host:~/my-project]$echo fileIdontWantToVersion.txt >> .gitignore
[dongiulio@host:~/my-project]$git st
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	.gitignore
#	pom.xml
#	src/
nothing added to commit but untracked files present (use "git add" to track)
[dongiulio@host:~/my-project]$git add .gitignore pom.xml src/
[dongiulio@host:~/my-project]$git st
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   .gitignore
#	new file:   pom.xml
#	new file:   src/main/java/org/wikijava/App.java
#	new file:   src/test/java/org/wikijava/AppTest.java
#
[dongiulio@host:~/my-project]$git commit -m "first import" 
[master (root-commit) e21c962] first import
 4 files changed, 77 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 pom.xml
 create mode 100644 src/main/java/org/wikijava/App.java
 create mode 100644 src/test/java/org/wikijava/AppTest.java
[dongiulio@host:~/my-project]$git st
# On branch master
nothing to commit (working directory clean)

Sharing your git project to an SVN repository

In this paragraph I'll show you how to share your git repository to a svn server. This is something I use quite often, because many of the projects I contribute to are versioned on SVN, and I don't want to give up the benefits of GIT (especially the ability to use versioning even while I'm offline).

This is very simple, these are the steps:

  • create the project directory on the remote SVN (if it doesn't already exist)
svn mkdir --parents protocol:///path/to/repo/PROJECT/trunk \
-m "Importing git repo"
  • initialize the git svn repository pointing to the remote svn directory, created above (note trunk is implicit in git, so it's omitted)
git svn init protocol:///path/to/repo/PROJECT -s
  • download SVN history, to start synchronization
git svn fetch
  • Merge the remote SVN data with the local git data
git rebase trunk
  • check that everything is right
git status
  • Commit whatever is in GIT to the SVN remote repository.
git svn dcommit

Publishing your repository on the web

To be completed... stay tuned... sorry.


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.