svn Git Command Guide
The git svn command provides bidirectional operation between Subversion (SVN) and Git repositories, allowing teams to migrate from SVN to Git or maintain compatibility with existing SVN infrastructure. It enables Git users to interact with SVN repositories while preserving Git’s advanced features locally.
git svn Syntax:
Section titled “git svn Syntax:”git svn <command> [<options>] [<arguments>]Git SVN Commands:
Section titled “Git SVN Commands:”| Command | Description |
|---|---|
clone | Clone SVN repository to Git |
init | Initialize Git repository for SVN tracking |
fetch | Fetch changes from SVN |
pull | Fetch and rebase from SVN |
rebase | Rebase local changes onto SVN HEAD |
dcommit | Commit local Git changes to SVN |
log | Show SVN log messages |
blame | Show SVN blame information |
find-rev | Find Git commit for SVN revision |
set-tree | Set tree for specific SVN revision |
create-ignore | Create .gitignore from svn:ignore |
show-ignore | Show SVN ignore patterns |
mkdirs | Recreate empty SVN directories |
commit-diff | Commit diff between revisions |
Common Options:
Section titled “Common Options:”| Option | Description |
|---|---|
-r <revision> | Specify SVN revision |
--revision=<rev> | SVN revision (same as -r) |
-s, --stdlayout | Assume standard trunk/branches/tags layout |
-T <trunk> | Trunk directory |
-t <tags> | Tags directory |
-b <branches> | Branches directory |
--prefix=<prefix> | Prefix for SVN tracking branches |
-A <authors-file> | Authors file for mapping |
--authors-prog=<cmd> | Program to generate author mapping |
--no-auth-cache | Don’t cache SVN credentials |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<URL> | SVN repository URL |
<path> | Local path or file |
<revision> | SVN revision number |
Understanding Git-SVN Integration:
Section titled “Understanding Git-SVN Integration:”Repository Structure Mapping:
Section titled “Repository Structure Mapping:”SVN Repository Structure:├── trunk/ -> Git: main/master branch├── branches/ -> Git: remote tracking branches│ ├── feature-x/ -> Git: remotes/git-svn/feature-x│ └── bugfix-y/ -> Git: remotes/git-svn/bugfix-y└── tags/ -> Git: tags (annotated)
Git-SVN Tracking:├── git-svn-id: <URL>@<rev> <UUID>├── Remote branches: git-svn, git-svn-<branch>└── Linear history: SVN revisions as Git commitsBidirectional Workflow:
Section titled “Bidirectional Workflow:”Git-SVN Bidirectional Flow:├── SVN -> Git: git svn fetch/rebase├── Git -> SVN: git svn dcommit├── Local Git: Full Git features (branches, merges)└── SVN Sync: Linear commit history to SVNAuthor Mapping:
Section titled “Author Mapping:”Author Mapping Strategies:├── Authors file: username = Full Name <email@domain.com>├── Authors program: Dynamic author resolution├── Default: username <username@UUID>└── Manual override: --authors-prog optionBasic Git-SVN Operations:
Section titled “Basic Git-SVN Operations:”Cloning SVN Repository:
Section titled “Cloning SVN Repository:”# Clone entire SVN repositorygit svn clone <svn-url> [local-dir]
# Clone with standard layout (trunk/branches/tags)git svn clone -s <svn-url> [local-dir]
# Clone specific branchgit svn clone -b <branch> <svn-url> [local-dir]
# Clone with author mappinggit svn clone --authors-file=authors.txt <svn-url>Initializing Existing Repository:
Section titled “Initializing Existing Repository:”# Initialize Git repo for SVN trackinggit svn init <svn-url>
# Initialize with custom layoutgit svn init -T trunk -b branches -t tags <svn-url>
# Add author mappinggit svn init --authors-file=authors.txt <svn-url>Fetching Changes:
Section titled “Fetching Changes:”# Fetch latest changes from SVNgit svn fetch
# Fetch specific revision rangegit svn fetch -r 1000:HEAD
# Fetch and rebase (equivalent to pull)git svn rebase
# Fetch with verbose outputgit svn fetch --verboseCommitting to SVN:
Section titled “Committing to SVN:”# Commit local changes to SVNgit svn dcommit
# Commit with specific messagegit svn dcommit -m "Commit message"
# Dry run commitgit svn dcommit --dry-run
# Commit specific commitsgit svn dcommit HEAD~3..HEADAdvanced Git-SVN Scenarios:
Section titled “Advanced Git-SVN Scenarios:”Branch Management:
Section titled “Branch Management:”# Create new SVN branch from Gitgit svn branch <branch-name>
# Switch to SVN branchgit checkout remotes/git-svn/<branch-name>
# Merge Git branches before SVN commitgit merge feature-branchgit svn dcommitTag Management:
Section titled “Tag Management:”# Create SVN tag from Gitgit svn tag <tag-name>
# Create tag from specific commitgit svn tag <tag-name> -r <revision>
# List SVN tagsgit branch -r | grep tags/Author Mapping Setup:
Section titled “Author Mapping Setup:”# Create authors filecat > authors.txt << 'EOF'jdoe = John Doe <john.doe@company.com>asmith = Alice Smith <alice.smith@company.com>bwilson = Bob Wilson <bob.wilson@company.com>EOF
# Use authors filegit svn clone --authors-file=authors.txt <svn-url>
# Use authors programgit svn clone --authors-prog=./author-lookup.sh <svn-url>Handling SVN Ignore Patterns:
Section titled “Handling SVN Ignore Patterns:”# Create .gitignore from svn:ignoregit svn create-ignore
# Show SVN ignore patternsgit svn show-ignore
# Update ignore patternsgit svn create-ignore --revision=HEADConfiguration and Best Practices:
Section titled “Configuration and Best Practices:”Git Configuration for SVN:
Section titled “Git Configuration for SVN:”# Configure global SVN settingsgit config --global svn.authorsfile ~/svn-authors.txtgit config --global svn.noMetadata falsegit config --global svn.useSvkProps falsegit config --global svn.useSvndumpProps false
# Configure remote trackinggit config svn-remote.svn.url <svn-url>git config svn-remote.svn.fetch :refs/remotes/git-svngit config svn-remote.svn.branches branches/*:refs/remotes/git-svn/*SVN Best Practices:
Section titled “SVN Best Practices:”# Always fetch before committinggit svn fetch && git svn rebase
# Use rebase instead of mergegit svn rebase # Instead of git pull
# Keep linear history for SVN# Avoid Git merges before dcommit
# Regular repository maintenancegit gc --aggressivegit svn gcMigration Strategies:
Section titled “Migration Strategies:”# Safe migration approach# 1. Clone SVN repositorygit svn clone -s <svn-url> project-git
# 2. Verify all historygit log --oneline | tail -20
# 3. Test build and functionality# ... run tests ...
# 4. Switch team to Git# ... team training and migration ...
# 5. Stop SVN commits (read-only)# ... update SVN permissions ...Integration with Development Workflows:
Section titled “Integration with Development Workflows:”SVN to Git Migration:
Section titled “SVN to Git Migration:”#!/bin/bash# Complete SVN to Git migration
migrate_svn_to_git() { local svn_url="$1" local git_repo="$2" local authors_file="${3:-authors.txt}"
echo "Migrating SVN repository to Git..." echo "SVN URL: $svn_url" echo "Git Repo: $git_repo"
# Create authors mapping if [ ! -f "$authors_file" ]; then echo "Creating authors file..." # Extract authors from SVN svn log "$svn_url" --xml | grep -E '<author>' | \ sed 's/<author>//g' | sed 's/<\/author>//g' | \ sort | uniq > svn-authors.tmp
# Create mapping file while read -r author; do echo "$author = $author <$author@company.com>" done < svn-authors.tmp > "$authors_file"
rm svn-authors.tmp echo "Edit $authors_file to add proper names and emails" return 1 fi
# Clone SVN repository echo "Cloning SVN repository..." git svn clone --authors-file="$authors_file" \ --stdlayout \ --no-metadata \ "$svn_url" \ "$git_repo"
cd "$git_repo" || return 1
# Convert SVN tags to Git tags echo "Converting SVN tags to Git tags..." git for-each-ref refs/remotes/git-svn/tags | while read -r ref; do tag_name=$(basename "$ref") git tag "$tag_name" "$ref" done
# Convert branches echo "Converting SVN branches..." git for-each-ref refs/remotes/git-svn | \ grep -v tags | while read -r ref; do branch_name=$(basename "$ref") if [ "$branch_name" != "trunk" ]; then git branch "$branch_name" "$ref" fi done
# Clean up remote refs git branch -r | grep git-svn | xargs -n1 git branch -r -d 2>/dev/null || true
# Set up Git remote (optional) echo "Setting up Git remote repository..." # git remote add origin <git-url> # git push -u origin main
echo "Migration complete!" echo "Repository: $git_repo" echo "Authors file: $authors_file"}
migrate_svn_to_git "https://svn.company.com/repo" "project-git"Bidirectional Workflow:
Section titled “Bidirectional Workflow:”# Maintain bidirectional SVN-Git workflowbidirectional_workflow() { echo "=== Bidirectional SVN-Git Workflow ==="
# Fetch latest SVN changes git svn fetch
# Rebase local changes on SVN HEAD git svn rebase
# Work with Git features locally git checkout -b feature-branch # ... make changes ... git commit -m "Implement feature"
# Merge back to SVN tracking branch git checkout git-svn git merge feature-branch
# Commit to SVN git svn dcommit
# Clean up git branch -d feature-branch}
bidirectional_workflowCI/CD Integration:
Section titled “CI/CD Integration:”# Git-SVN in CI/CD pipelineci_svn_integration() { echo "=== CI/CD with Git-SVN ==="
# Fetch latest changes git svn rebase
# Run tests if ! run_tests; then echo "Tests failed" exit 1 fi
# Build application if ! run_build; then echo "Build failed" exit 1 fi
# Commit build artifacts to SVN git add build-artifacts/ git commit -m "Build $(date +%Y%m%d-%H%M%S) [ci skip]"
# Push to SVN git svn dcommit
echo "CI/CD pipeline completed"}
ci_svn_integrationTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Authentication Issues:
Section titled “Authentication Issues:”# Clear SVN auth cacherm -rf ~/.subversion/auth
# Force re-authenticationgit svn dcommit --username=newuser
# Use different credentialsgit svn clone --username=user --password=pass <url>
# Configure credential storagegit config svn.authorsfile ~/svn-authors.txtRevision Mapping Issues:
Section titled “Revision Mapping Issues:”# Find Git commit for SVN revisiongit svn find-rev r1234
# Find SVN revision for Git commitgit log --grep="git-svn-id.*@1234"
# Rebuild revision mappinggit svn reset -r <revision>git svn fetchBranch/Tag Conflicts:
Section titled “Branch/Tag Conflicts:”# Handle conflicting branch namesgit branch -m git-svn svn-trunkgit svn reset --parent
# Fix tag conflictsgit tag -d conflicting-taggit svn tag conflicting-tagEncoding Issues:
Section titled “Encoding Issues:”# Handle encoding problemsexport LANG=en_US.UTF-8export LC_ALL=en_US.UTF-8
# Convert repository encodinggit svn clone --encoding=utf-8 <svn-url>
# Fix existing repositorygit filter-branch --tree-filter 'recode latin1..utf8 *' HEADPerformance Issues:
Section titled “Performance Issues:”# Optimize large repository handlinggit config svn.brokenSymlinkWorkaround truegit config core.deltaBaseCacheLimit 2g
# Use shallow clone for testinggit svn clone -r HEAD:HEAD-100 <svn-url>
# Clean up repositorygit gc --aggressivegit svn gcMerge Conflicts:
Section titled “Merge Conflicts:”# Handle SVN rebase conflictsgit status
# Resolve conflicts manually# Edit conflicted filesgit add resolved-file.txt
# Continue rebasegit rebase --continue
# Abort if neededgit svn rebase --abortReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Enterprise Migration:
Section titled “Enterprise Migration:”#!/bin/bash# Enterprise SVN to Git migration
enterprise_migration() { local svn_root="$1" local git_org="$2" local projects=("project-a" "project-b" "project-c")
echo "=== Enterprise SVN Migration ===" echo "SVN Root: $svn_root" echo "Git Org: $git_org"
# Create authors file create_enterprise_authors() { echo "Creating enterprise authors file..."
# Query LDAP/Active Directory for users # This is a simplified example cat > enterprise-authors.txt << 'EOF'jdoe = John Doe <john.doe@company.com>asmith = Alice Smith <alice.smith@company.com>bwilson = Bob Wilson <bob.wilson@company.com>EOF
echo "Enterprise authors file created" }
# Migrate individual projects migrate_project() { local project="$1" local svn_url="$svn_root/$project" local git_repo="$project-git"
echo "Migrating project: $project"
# Clone with enterprise settings git svn clone \ --authors-file=enterprise-authors.txt \ --stdlayout \ --no-metadata \ --preserve-empty-dirs \ "$svn_url" \ "$git_repo"
cd "$git_repo" || return 1
# Convert branches and tags git for-each-ref refs/remotes/git-svn/tags | \ sed 's|refs/remotes/git-svn/tags/||' | \ xargs -I {} git tag {} refs/remotes/git-svn/tags/{}
# Setup Git remotes git remote add origin "https://github.com/$git_org/$project.git"
# Initial push git push -u origin main git push origin --tags
cd .. echo "Project $project migrated" }
# Main migration process create_enterprise_authors
for project in "${projects[@]}"; do migrate_project "$project" done
# Generate migration report cat > migration-report.txt << EOFEnterprise Migration Report
Migration Date: $(date)SVN Root: $svn_rootGit Organization: $git_org
Migrated Projects:$(printf -- '- %s\n' "${projects[@]}")
Authors File: enterprise-authors.txtMigration Script: $0
Next Steps:1. Update CI/CD pipelines2. Update documentation3. Train development teams4. Archive SVN repositories (read-only)5. Monitor for issuesEOF
echo "Enterprise migration completed" echo "See migration-report.txt for details"}
enterprise_migration "https://svn.company.com" "company-org"Gradual Migration Strategy:
Section titled “Gradual Migration Strategy:”# Gradual SVN to Git migrationgradual_migration() { echo "=== Gradual SVN-Git Migration ==="
# Phase 1: Read-only Git mirror setup_git_mirror() { local svn_url="$1" local mirror_dir="${2:-git-mirror}"
echo "Setting up Git mirror..."
git svn clone --mirror "$svn_url" "$mirror_dir" cd "$mirror_dir"
# Setup automatic sync cat > sync-svn.sh << 'EOF'#!/bin/bashcd "$(dirname "$0")"git svn fetchgit update-server-infoEOF
chmod +x sync-svn.sh
# Setup cron for regular sync echo "*/15 * * * * $(pwd)/sync-svn.sh" | crontab -
echo "Git mirror setup complete" }
# Phase 2: Team migration migrate_team() { local team_members="$1"
echo "Migrating team to Git..."
for member in $team_members; do echo "Setting up Git for $member..."
# Clone team repository git clone git-mirror "$member-repo"
# Setup Git configuration cd "$member-repo" git config user.name "$member" git config user.email "$member@company.com"
# Create feature branch git checkout -b "$member-feature"
cd .. done
echo "Team migration setup complete" }
# Phase 3: Final cutover final_cutover() { echo "Performing final cutover..."
# Last SVN sync cd git-mirror git svn fetch
# Make SVN read-only echo "Make SVN repository read-only now"
# Final Git push git push origin --mirror
# Update all team repos for repo in *-repo; do cd "$repo" git pull origin main cd .. done
echo "Final cutover complete" }
# Interactive migration echo "Gradual Migration Options:" echo "1. Setup Git mirror" echo "2. Migrate team" echo "3. Final cutover"
read -p "Select phase (1-3): " phase
case "$phase" in 1) read -p "SVN URL: " svn_url setup_git_mirror "$svn_url" ;; 2) read -p "Team members (space-separated): " team migrate_team "$team" ;; 3) final_cutover ;; *) echo "Invalid option" ;; esac}
gradual_migrationSVN Bridge Maintenance:
Section titled “SVN Bridge Maintenance:”# Maintain SVN-Git bridge for legacy systemssvn_bridge_maintenance() { echo "=== SVN-Git Bridge Maintenance ==="
# Sync Git to SVN sync_git_to_svn() { echo "Syncing Git changes to SVN..."
# Fetch latest Git changes git fetch origin
# Rebase on latest git rebase origin/main
# Commit to SVN git svn dcommit
echo "Git->SVN sync complete" }
# Sync SVN to Git sync_svn_to_git() { echo "Syncing SVN changes to Git..."
# Fetch from SVN git svn fetch
# Rebase local changes git svn rebase
# Push to Git git push origin main
echo "SVN->Git sync complete" }
# Bidirectional sync bidirectional_sync() { echo "Performing bidirectional sync..."
# Pull from both git fetch origin git svn fetch
# Check for conflicts if git merge-base --is-ancestor origin/main git-svn; then # Git is ahead sync_git_to_svn elif git merge-base --is-ancestor git-svn origin/main; then # SVN is ahead sync_svn_to_git else # Diverged - manual intervention needed echo "Repositories have diverged - manual merge required" exit 1 fi }
# Maintenance tasks maintenance_tasks() { echo "Running maintenance tasks..."
# Clean up old refs git for-each-ref --format="%(refname)" refs/remotes/git-svn | \ xargs -n1 git update-ref -d 2>/dev/null || true
# Repack repository git repack -a -d
# Clean SVN metadata git svn gc
echo "Maintenance complete" }
# Interactive bridge maintenance echo "SVN-Git Bridge Maintenance:" echo "1. Sync Git to SVN" echo "2. Sync SVN to Git" echo "3. Bidirectional sync" echo "4. Maintenance tasks"
read -p "Select operation (1-4): " operation
case "$operation" in 1) sync_git_to_svn ;; 2) sync_svn_to_git ;; 3) bidirectional_sync ;; 4) maintenance_tasks ;; *) echo "Invalid option" ;; esac}
svn_bridge_maintenanceLegacy System Integration:
Section titled “Legacy System Integration:”# Integrate with legacy SVN-based systemslegacy_integration() { echo "=== Legacy System Integration ==="
# Setup SVN webhook equivalent setup_svn_hooks() { echo "Setting up SVN-style hooks..."
# Post-commit hook equivalent cat > .git/hooks/post-commit << 'EOF'#!/bin/bash# Equivalent to SVN post-commit hook
# Notify legacy systemscurl -X POST "http://legacy-system/api/git-commit" \ -H "Content-Type: application/json" \ -d "{\"commit\":\"$(git rev-parse HEAD)\", \"message\":\"$(git log -1 --pretty=%B)\"}"
# Update documentation./update-docs.shEOF
chmod +x .git/hooks/post-commit
# Pre-commit checks cat > .git/hooks/pre-commit << 'EOF'#!/bin/bash# Equivalent to SVN pre-commit checks
# Run legacy validationif ! ./legacy-validation.sh; then echo "Legacy validation failed" exit 1fi
# Check code standardsif ! ./check-standards.sh; then echo "Code standards check failed" exit 1fiEOF
chmod +x .git/hooks/pre-commit }
# SVN-style access control setup_access_control() { echo "Setting up access control..."
# Create access control script cat > git-access-control.sh << 'EOF'#!/bin/bash# SVN-style access control
USER="$1"PATH="$2"ACTION="$3"
# Read access - generally allowedif [ "$ACTION" = "read" ]; then exit 0fi
# Write access - check permissionscase "$USER" in admin|manager) # Full access exit 0 ;; developer) # Development branches only if [[ "$PATH" == refs/heads/dev* ]]; then exit 0 fi ;; *) # No write access echo "Access denied for user $USER to $PATH" exit 1 ;;esacEOF
chmod +x git-access-control.sh }
# Automated SVN reporting setup_reporting() { echo "Setting up automated reporting..."
cat > generate-svn-report.sh << 'EOF'#!/bin/bash# Generate SVN-style reports
echo "=== SVN-Style Repository Report ==="echo "Generated: $(date)"echo
echo "Recent Commits:"git log --oneline -10 --no-mergesecho
echo "Active Branches:"git branch -r | grep -v HEADecho
echo "Repository Statistics:"echo "Commits: $(git rev-list --count HEAD)"echo "Contributors: $(git shortlog -sn | wc -l)"echo "Branches: $(git branch -r | wc -l)"echo "Tags: $(git tag | wc -l)"EOF
chmod +x generate-svn-report.sh
# Schedule daily reports (crontab -l ; echo "0 9 * * * $(pwd)/generate-svn-report.sh | mail -s 'Daily Git Report' admin@company.com") | crontab - }
# Interactive setup echo "Legacy Integration Options:" echo "1. Setup SVN-style hooks" echo "2. Setup access control" echo "3. Setup automated reporting"
read -p "Select option (1-3): " option
case "$option" in 1) setup_svn_hooks ;; 2) setup_access_control ;; 3) setup_reporting ;; *) echo "Invalid option" ;; esac}
legacy_integrationWhat’s the difference between git svn and regular Git?
Section titled “What’s the difference between git svn and regular Git?”git svn bridges Git and SVN repositories, allowing bidirectional operation. Regular Git is native Git without SVN compatibility layer.
How do I migrate from SVN to Git?
Section titled “How do I migrate from SVN to Git?”Use git svn clone with —stdlayout and —authors-file options. Convert branches/tags, then push to Git remote.
Can I use Git features with git svn?
Section titled “Can I use Git features with git svn?”Yes, use full Git features locally (branches, merges, etc.), but dcommit requires linear history for SVN compatibility.
How do I handle SVN author names?
Section titled “How do I handle SVN author names?”Create authors file mapping SVN usernames to full names/emails: username = Full Name email@domain.com
What’s the difference between git svn rebase and git pull?
Section titled “What’s the difference between git svn rebase and git pull?”git svn rebase fetches from SVN and rebases local changes. git pull is for Git remotes; use rebase for SVN.
Can git svn work with SVN branches?
Section titled “Can git svn work with SVN branches?”Yes, git svn clone —stdlayout tracks trunk/branches/tags. Branches appear as Git remote branches.
How do I commit Git changes back to SVN?
Section titled “How do I commit Git changes back to SVN?”Use git svn dcommit to commit local Git commits to SVN. Requires linear history.
What’s git-svn-id in commit messages?
Section titled “What’s git-svn-id in commit messages?”Metadata added by git svn showing SVN URL, revision, and repository UUID for tracking.
Can I use git svn with existing Git repository?
Section titled “Can I use git svn with existing Git repository?”Yes, use git svn init to add SVN tracking to existing Git repo, then git svn fetch.
How do I find the Git commit for an SVN revision?
Section titled “How do I find the Git commit for an SVN revision?”Use git svn find-rev
What’s the performance impact of git svn?
Section titled “What’s the performance impact of git svn?”git svn can be slower than native Git due to SVN protocol overhead. Use for migration/compatibility, not primary workflow.
Can git svn handle large SVN repositories?
Section titled “Can git svn handle large SVN repositories?”Yes, but may require incremental fetching and patience. Use —revision to limit initial clone size.
How do I clean up after git svn migration?
Section titled “How do I clean up after git svn migration?”Remove .git/svn directory, clean reflog, consider filter-branch to remove git-svn-id metadata.
Can multiple people use git svn on same repository?
Section titled “Can multiple people use git svn on same repository?”Yes, but coordinate dcommits to avoid conflicts. Each user should rebase before committing.
Applications of the git svn command
Section titled “Applications of the git svn command”- SVN Migration: Migrate repositories from Subversion to Git with full history preservation
- Bidirectional Workflow: Maintain compatibility with existing SVN infrastructure while using Git locally
- Gradual Transition: Enable teams to adopt Git incrementally while maintaining SVN access
- Legacy Integration: Connect modern Git workflows with legacy SVN-based systems
- Bridge Operations: Support hybrid development environments with mixed version control systems
- Historical Preservation: Maintain complete project history during version control transitions
- Enterprise Compatibility: Meet organizational requirements for SVN usage while enabling Git benefits
- Training and Adoption: Allow teams to learn Git while maintaining SVN safety nets