My favorites
|
Sign in
svnbook
Version Control With Subversion
Project Home
Issues
Source
Checkout
|
Browse
|
Changes
|
‹r3305
r3652
Source path:
svn
/
tags
/
en-1.5-final
/
src
/
en
/
book
/
ch04-branching-and-merging.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
<chapter id="svn.branchmerge">
<title>Branching and Merging</title>
<blockquote>
<attribution>Confucius</attribution>
<para><quote>君子务本
(It is upon the Trunk that a gentleman works.)</quote></para>
</blockquote>
<para>Branching, tagging, and merging are concepts common to
almost all version control systems. If you're not familiar with
these ideas, we provide a good introduction in this chapter. If
you are familiar, hopefully you'll find it interesting to
see how Subversion implements them.</para>
<para>Branching is a fundamental part of version control. If
you're going to allow Subversion to manage your data, this
is a feature you'll eventually come to depend on. This chapter
assumes that you're already familiar with Subversion's basic
concepts (<xref linkend="svn.basic"/>).</para>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.whatis">
<title>What's a Branch?</title>
<para>Suppose it's your job to maintain a document for a division
in your company—a handbook of some sort. One day a different
division asks you for the same handbook, but with a few parts
<quote>tweaked</quote> for them, since they do things slightly
differently.</para>
<para>What do you do in this situation? You do the obvious: make
a second copy of your document and begin maintaining the two
copies separately. As each department asks you to make small
changes, you incorporate them into one copy or the other.</para>
<para>You often want to make the same change to both copies. For
example, if you discover a typo in the first copy, it's very
likely that the same typo exists in the second copy. The two
documents are almost the same, after all; they differ only in
small, specific ways.</para>
<para>This is the basic concept of a
<firstterm>branch</firstterm>—namely, a line of
development that exists independently of another line, yet still
shares a common history if you look far enough back in time. A
branch always begins life as a copy of something, and moves on
from there, generating its own history (see <xref
linkend="svn.branchmerge.whatis.dia-1"/>).</para>
<figure id="svn.branchmerge.whatis.dia-1">
<title>Branches of development</title>
<graphic fileref="images/ch04dia1.png"/>
</figure>
<para>Subversion has commands to help you maintain parallel
branches of your files and directories. It allows you to create
branches by copying your data, and remembers that the copies are
related to one another. It also helps you duplicate changes
from one branch to another. Finally, it can make portions of
your working copy reflect different branches so that you can
<quote>mix and match</quote> different lines of development in
your daily work.</para>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.using">
<title>Using Branches</title>
<para>At this point, you should understand how each commit creates
an entirely new filesystem tree (called a <quote>revision</quote>)
in the repository. If you don't, go back and read about revisions in
<xref linkend="svn.basic.in-action.revs"/>.</para>
<para>For this chapter, we'll go back to the same example from
<xref linkend="svn.basic"/>. Remember that you and your
collaborator, Sally, are sharing a repository that contains two
projects, <filename>paint</filename> and
<filename>calc</filename>. Notice that in <xref
linkend="svn.branchmerge.using.dia-1"/>, however, each project
directory now contains subdirectories named
<filename>trunk</filename> and <filename>branches</filename>.
The reason for this will soon become clear.</para>
<figure id="svn.branchmerge.using.dia-1">
<title>Starting repository layout</title>
<graphic fileref="images/ch04dia2.png"/>
</figure>
<para>As before, assume that Sally and you both have working
copies of the <quote>calc</quote> project. Specifically, you
each have a working copy of <filename>/calc/trunk</filename>.
All the files for the project are in this subdirectory rather
than in <filename>/calc</filename> itself, because your team has
decided that <filename>/calc/trunk</filename> is where the
<quote>main line</quote> of development is going to take
place.</para>
<para>Let's say that you've been given the task of implementing a
large software feature. It will take a long time to write, and
will affect all the files in the project. The immediate problem
is that you don't want to interfere with Sally, who is in the
process of fixing small bugs here and there. She's depending on
the fact that the latest version of the project (in
<filename>/calc/trunk</filename>) is always usable. If you
start committing your changes bit by bit, you'll surely break
things for Sally (and other team members as well).</para>
<para>One strategy is to crawl into a hole: you and Sally can stop
sharing information for a week or two. That is, start gutting
and reorganizing all the files in your working copy, but don't
commit or update until you're completely finished with the task.
There are a number of problems with this, though. First, it's
not very safe. Most people like to save their work to the
repository frequently, should something bad accidentally happen
to their working copy. Second, it's not very flexible. If you
do your work on different computers (perhaps you have a working
copy of <filename>/calc/trunk</filename> on two different
machines), you'll need to manually copy your changes back and
forth or just do all the work on a single computer. By that
same token, it's difficult to share your changes in progress
with anyone else. A common software development <quote>best
practice</quote> is to allow your peers to review your work as
you go. If nobody sees your intermediate commits, you lose
potential feedback and may end up going down the wrong path for
weeks before another person on your team notices. Finally, when
you're finished with all your changes, you might find it very
difficult to remerge your final work with the rest of the
company's main body of code. Sally (or others) may have made
many other changes in the repository that are difficult to
incorporate into your working copy—especially if you
run <command>svn update</command> after weeks of
isolation.</para>
<para>The better solution is to create your own branch, or line of
development, in the repository. This allows you to save your
half-broken work frequently without interfering with others, yet
you can still selectively share information with your
collaborators. You'll see exactly how this works as we go.
</para>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.using.create">
<title>Creating a Branch</title>
<para>Creating a branch is very simple—you make a copy of
the project in the repository using the <command>svn
copy</command> command. Subversion is able to copy not only
single files, but whole directories as well. In this case,
you want to make a copy of the
<filename>/calc/trunk</filename> directory. Where should the
new copy live? Wherever you wish—it's a matter of
project policy. Let's say that your team has a policy of
creating branches in the <filename>/calc/branches</filename>
area of the repository, and you want to name your branch
<literal>my-calc-branch</literal>. You'll want to create a
new directory,
<filename>/calc/branches/my-calc-branch</filename>, which
begins its life as a copy of
<filename>/calc/trunk</filename>.</para>
<para>You may already have seen <command>svn copy</command> used
to copy one file to another within a working copy. But it can
also be used to do a <quote>remote</quote> copy entirely
within the repository. Just copy one URL to another:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Creating a private branch of /calc/trunk."
Committed revision 341.
</screen>
<para>This command causes a near-instantaneous commit in the
repository, creating a new directory in revision 341. The new
directory is a copy of <filename>/calc/trunk</filename>. This
is shown in
<xref linkend="svn.branchmerge.using.create.dia-1"/>.
<footnote>
<para>Subversion does not support copying between different
repositories. When using URLs with <command>svn
copy</command> or <command>svn move</command>, you can only
copy items within the same repository.</para>
</footnote>
While it's also possible to create a branch by
using <command>svn copy</command> to duplicate a directory
within the working copy, this technique isn't recommended. It
can be quite slow, in fact! Copying a directory on the
client side is a linear-time operation, in that it actually
has to duplicate every file and subdirectory on the local disk.
Copying a directory on the server, however, is a constant-time
operation, and it's the way most people create
branches.</para>
<figure id="svn.branchmerge.using.create.dia-1">
<title>Repository with new copy</title>
<graphic fileref="images/ch04dia3.png"/>
</figure>
<sidebar>
<title>Cheap Copies</title>
<para>Subversion's repository has a special design. When you
copy a directory, you don't need to worry about the
repository growing huge—Subversion doesn't actually
duplicate any data. Instead, it creates a new directory
entry that points to an <emphasis>existing</emphasis> tree.
If you're an experienced Unix user, you'll recognize this as
the same concept behind a hard link. As further changes are
made to files and directories beneath the copied directory,
Subversion continues to employ this hard link concept where
it can. It duplicates data only when it is necessary to
disambiguate different versions of objects.</para>
<para>This is why you'll often hear Subversion users talk
about <quote>cheap copies.</quote> It doesn't matter how
large the directory is—it takes a very tiny, constant
amount of time and space to make a copy of it. In fact,
this feature is the basis of how commits work in Subversion:
each revision is a <quote>cheap copy</quote> of the previous
revision, with a few items lazily changed within. (To read
more about this, visit Subversion's web site and read about
the <quote>bubble up</quote> method in Subversion's design
documents.)</para>
<para>Of course, these internal mechanics of copying and
sharing data are hidden from the user, who simply sees
copies of trees. The main point here is that copies are
cheap, both in time and in space. If you create a branch
entirely within the repository (by running <userinput>svn copy
<replaceable>URL1</replaceable> <replaceable>URL2</replaceable></userinput>), it's a quick, constant-time operation.
Make branches as often as you want.</para>
</sidebar>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.using.work">
<title>Working with Your Branch</title>
<para>Now that you've created a branch of the project, you can
check out a new working copy to start using it:</para>
<screen>
$ svn checkout http://svn.example.com/repos/calc/branches/my-calc-branch
A my-calc-branch/Makefile
A my-calc-branch/integer.c
A my-calc-branch/button.c
Checked out revision 341.
</screen>
<para>There's nothing special about this working copy; it simply
mirrors a different directory in the repository. When you
commit changes, however, Sally won't see them when she
updates, because her working copy is of
<filename>/calc/trunk</filename>. (Be sure to read <xref
linkend="svn.branchmerge.switchwc"/> later in this chapter: the
<command>svn switch</command> command is an alternative way of
creating a working copy of a branch.)</para>
<para>Let's pretend that a week goes by, and the following
commits happen:</para>
<itemizedlist>
<listitem><para>
You make a change to
<filename>/calc/branches/my-calc-branch/button.c</filename>,
which creates revision 342.</para>
</listitem>
<listitem><para>
You make a change to
<filename>/calc/branches/my-calc-branch/integer.c</filename>,
which creates revision 343.</para>
</listitem>
<listitem><para>
Sally makes a change to
<filename>/calc/trunk/integer.c</filename>, which creates
revision 344.</para>
</listitem>
</itemizedlist>
<para>Now two independent lines of development (shown
in <xref linkend="svn.branchmerge.using.work.dia-1"/>) are happening on
<filename>integer.c</filename>.</para>
<figure id="svn.branchmerge.using.work.dia-1">
<title>The branching of one file's history</title>
<graphic fileref="images/ch04dia4.png"/>
</figure>
<para>Things get interesting when you look at the history of
changes made to your copy of
<filename>integer.c</filename>:</para>
<screen>
$ pwd
/home/user/my-calc-branch
$ svn log -v integer.c
------------------------------------------------------------------------
r343 | user | 2002-11-07 15:27:56 -0600 (Thu, 07 Nov 2002) | 2 lines
Changed paths:
M /calc/branches/my-calc-branch/integer.c
* integer.c: frozzled the wazjub.
------------------------------------------------------------------------
r341 | user | 2002-11-03 15:27:56 -0600 (Thu, 07 Nov 2002) | 2 lines
Changed paths:
A /calc/branches/my-calc-branch (from /calc/trunk:340)
Creating a private branch of /calc/trunk.
------------------------------------------------------------------------
r303 | sally | 2002-10-29 21:14:35 -0600 (Tue, 29 Oct 2002) | 2 lines
Changed paths:
M /calc/trunk/integer.c
* integer.c: changed a docstring.
------------------------------------------------------------------------
r98 | sally | 2002-02-22 15:35:29 -0600 (Fri, 22 Feb 2002) | 2 lines
Changed paths:
A /calc/trunk/integer.c
* integer.c: adding this file to the project.
------------------------------------------------------------------------
</screen>
<para>Notice that Subversion is tracing the history of your
branch's <filename>integer.c</filename> all the way back
through time, even traversing the point where it was copied.
It shows the creation of the branch as an event in the
history, because <filename>integer.c</filename> was implicitly
copied when all of <filename>/calc/trunk/</filename> was
copied. Now look at what happens when Sally runs the same
command on her copy of the file:</para>
<screen>
$ pwd
/home/sally/calc
$ svn log -v integer.c
------------------------------------------------------------------------
r344 | sally | 2002-11-07 15:27:56 -0600 (Thu, 07 Nov 2002) | 2 lines
Changed paths:
M /calc/trunk/integer.c
* integer.c: fix a bunch of spelling errors.
------------------------------------------------------------------------
r303 | sally | 2002-10-29 21:14:35 -0600 (Tue, 29 Oct 2002) | 2 lines
Changed paths:
M /calc/trunk/integer.c
* integer.c: changed a docstring.
------------------------------------------------------------------------
r98 | sally | 2002-02-22 15:35:29 -0600 (Fri, 22 Feb 2002) | 2 lines
Changed paths:
A /calc/trunk/integer.c
* integer.c: adding this file to the project.
------------------------------------------------------------------------
</screen>
<para>Sally sees her own revision 344 change, but not the change
you made in revision 343. As far as Subversion is concerned,
these two commits affected different files in different
repository locations. However, Subversion
<emphasis>does</emphasis> show that the two files share a
common history. Before the branch copy was made in revision
341, the files used to be the same file. That's why you and
Sally both see the changes made in revisions 303 and
98.</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.using.concepts">
<title>The Key Concepts Behind Branching</title>
<para>You should remember two important lessons
from this section. First, Subversion has no internal concept
of a branch—it knows only how to make copies. When you
copy a directory, the resultant directory is only
a <quote>branch</quote> because <emphasis>you</emphasis>
attach that meaning to it. You may think of the directory
differently, or treat it differently, but to Subversion it's
just an ordinary directory that happens to carry some extra
historical information.</para>
<para>Second, because of this copy mechanism, Subversion's
branches exist as <emphasis>normal filesystem
directories</emphasis> in the repository. This is different
from other version control systems, where branches are
typically defined by adding
extra-dimensional <quote>labels</quote> to collections of
files. The location of your branch directory doesn't matter
to Subversion. Most teams follow a convention of putting all
branches into a <filename>/branches</filename> directory, but
you're free to invent any policy you wish.</para>
</sect2>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.basicmerging">
<title>Basic Merging</title>
<para>Now you and Sally are working on parallel branches of the
project: you're working on a private branch, and Sally is
working on the <firstterm>trunk</firstterm>, or main line of
development.</para>
<para>For projects that have a large number of contributors, it's
common for most people to have working copies of the trunk.
Whenever someone needs to make a long-running change that is
likely to disrupt the trunk, a standard procedure is to create a
private branch and commit changes there until all the work is
complete.</para>
<para>So, the good news is that you and Sally aren't interfering
with each other. The bad news is that it's very easy to drift
<emphasis>too</emphasis> far apart. Remember that one of the
problems with the <quote>crawl in a hole</quote> strategy is
that by the time you're finished with your branch, it may be
near-impossible to merge your changes back into the trunk
without a huge number of conflicts.</para>
<para>Instead, you and Sally might continue to share changes as
you work. It's up to you to decide which changes are worth
sharing; Subversion gives you the ability to selectively
<quote>copy</quote> changes between branches. And when you're
completely finished with your branch, your entire set of branch
changes can be copied back into the trunk. In Subversion
terminology, the general act of replicating changes from one
branch to another is called <firstterm>merging</firstterm>, and
it is performed using various invocations of the <command>svn
merge</command> command.</para>
<para>In the examples that follow, we're assuming that both your
Subversion client and server are running Subversion 1.5 (or
later). If either client or server is older than version 1.5,
things are more complicated: the system won't track changes
automatically, and you'll have to use painful manual methods to
achieve similar results. That is, you'll always need to use the
detailed merge syntax to specify specific ranges of revisions to
replicate (see
<xref linkend="svn.branchmerge.advanced.advancedsyntax"/> later
in this chapter), and take special care to keep track of what's
already been merged and what hasn't. For this reason,
we <emphasis>strongly</emphasis> recommend that you make sure your
client and server are at least at version 1.5.</para>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.changesets">
<title>Changesets</title>
<para>Before we proceed further, we should warn you that there's
going to be a lot of discussion of <quote>changes</quote> in
the pages ahead. A lot of people experienced with version
control systems use the terms <quote>change</quote>
and <quote>changeset</quote> interchangeably, and we should
clarify what Subversion understands as
a <firstterm>changeset</firstterm>.</para>
<para>Everyone seems to have a slightly different definition
of changeset, or at least a different
expectation of what it means for a version control system to
have one. For our purposes, let's say that a changeset is just
a collection of changes with a unique name. The changes might
include textual edits to file contents, modifications to tree
structure, or tweaks to metadata. In more common speak, a
changeset is just a patch with a name you can refer to.</para>
<para>In Subversion, a global revision number N names a tree in
the repository: it's the way the repository looked after the
Nth commit. It's also the name of an implicit changeset: if
you compare tree N with tree N−1, you can derive the exact
patch that was committed. For this reason, it's easy to think
of revision N as not just a tree, but a changeset as well. If
you use an issue tracker to manage bugs, you can use the
revision numbers to refer to particular patches that fix
bugs—for example,
<quote>this issue was fixed by r9238.</quote> Somebody
can then run <userinput>svn log -r 9238</userinput> to read about
the exact changeset that fixed the bug, and run
<userinput>svn diff -c 9238</userinput> to see the patch itself.
And (as you'll see shortly)
Subversion's <command>svn merge</command> command is able to use
revision numbers. You can merge specific changesets from one
branch to another by naming them in the merge
arguments: passing <userinput>-c 9238</userinput> to <command>svn merge</command> would merge
changeset r9238 into your working copy.</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchemerge.basicmerging.stayinsync">
<title>Keeping a Branch in Sync</title>
<para>Continuing with our running example, let's suppose that a
week has passed since you started working on your private
branch. Your new feature isn't finished yet, but at the same
time you know that other people on your team have continued to
make important changes in the
project's <filename>/trunk</filename>. It's in your best
interest to replicate those changes to your own branch, just
to make sure they mesh well with your changes. In fact, this
is a best practice: frequently keeping your branch in sync
with the main development line helps
prevent <quote>surprise</quote> conflicts when it comes time
for you to fold your changes back into the trunk.</para>
<para>Subversion is aware of the history of your branch and
knows when it divided away from the trunk. To replicate the
latest, greatest trunk changes to your branch, first make sure
your working copy of the branch
is <quote>clean</quote>—that it has no local
modifications reported by <command>svn status</command>. Then
simply run:</para>
<screen>
$ pwd
/home/user/my-calc-branch
$ svn merge http://svn.example.com/repos/calc/trunk
--- Merging r345 through r356 into '.':
U button.c
U integer.c
</screen>
<para>This basic syntax—<userinput>svn merge
<replaceable>URL</replaceable></userinput>—tells Subversion to merge all recent
changes from the URL to the current working directory (which
is typically the root of your working copy). After running
the prior example, your branch working copy now contains new
local modifications, and these edits are duplications of all
of the changes that have happened on the trunk since you first
created your branch:</para>
<screen>
$ svn status
M .
M button.c
M integer.c
</screen>
<para>At this point, the wise thing to do is look at the changes
carefully with <command>svn diff</command>, and then build and
test your branch. Notice that the current working directory
(<quote><filename>.</filename></quote>) has also been
modified; the <command>svn diff</command> will show that
its <literal>svn:mergeinfo</literal> property has been either
created or modified. This is important merge-related metadata
that you should <emphasis>not</emphasis> touch, since it will
be needed by future <command>svn merge</command> commands.
(We'll learn more about this metadata later in the
chapter.)</para>
<para>After performing the merge, you might also need to resolve
some conflicts (just as you do with <command>svn
update</command>) or possibly make some small edits to get
things working properly. (Remember, just because there are
no <emphasis>syntactic</emphasis> conflicts doesn't mean there
aren't any <emphasis>semantic</emphasis> conflicts!) If you
encounter serious problems, you can always abort the local
changes by running <userinput>svn revert . -R</userinput> (which
will undo all local modifications) and start a
long <quote>what's going on?</quote> discussion with your
collaborators. If things look good, however, you can
submit these changes into the repository:</para>
<screen>
$ svn commit -m "Merged latest trunk changes to my-calc-branch."
Sending .
Sending button.c
Sending integer.c
Transmitting file data ..
Committed revision 357.
</screen>
<para>At this point, your private branch is now <quote>in
sync</quote> with the trunk, so you can rest easier knowing
that as you continue to work in isolation, you're not
drifting too far away from what everyone else is
doing.</para>
<sidebar>
<title>Why Not Use Patches Instead?</title>
<para>A question may be on your mind, especially if you're a
Unix user: why bother to use <command>svn merge</command> at
all? Why not simply use the operating system's
<command>patch</command> command to accomplish the same job?
For example:</para>
<screen>
$ cd my-calc-branch
$ svn diff -r 341:HEAD http://svn.example.com/repos/calc/trunk > patchfile
$ patch -p0 < patchfile
Patching file integer.c using Plan A...
Hunk #1 succeeded at 147.
Hunk #2 succeeded at 164.
Hunk #3 succeeded at 241.
Hunk #4 succeeded at 249.
done
</screen>
<para>In this particular example, there really isn't much
difference. But <command>svn merge</command> has special
abilities that surpass the <command>patch</command> program.
The file format used by <command>patch</command> is quite
limited; it's able to tweak file contents only. There's no
way to represent changes to <emphasis>trees</emphasis>, such
as the addition, removal, or renaming of files and
directories. Nor can the <command>patch</command> program
notice changes to properties. If Sally's change had,
say, added a new directory, the output of <command>svn
diff</command> wouldn't have mentioned it at
all. <command>svn diff</command> outputs only the limited
patch format, so there are some ideas it simply can't
express.</para>
<para>The <command>svn merge</command> command, however, can
express changes in tree structure and properties by directly
applying them to your working copy. Even more important,
this command records the changes that have been duplicated
to your branch so that Subversion is aware of exactly which
changes exist in each location (see
<xref linkend="svn.branchmerge.basicmerging.mergeinfo"/>.)
This is a critical feature that makes branch management
usable; without it, users would have to manually keep notes
on which sets of changes have or haven't been merged
yet.</para>
</sidebar>
<para>Suppose that another week has passed. You've committed
more changes to your branch, and your comrades have continued
to improve the trunk as well. Once again, you'd like to
replicate the latest trunk changes to your branch and bring
yourself in sync. Just run the same merge command
again!</para>
<screen>
$ svn merge http://svn.example.com/repos/calc/trunk
--- Merging r357 through r380 into '.':
U integer.c
U Makefile
A README
</screen>
<para>Subversion knows which trunk changes you've already
replicated to your branch, so it carefully replicates only
those changes you don't yet have. Once again, you'll have to
build, test, and <command>svn commit</command> the local
modifications to your branch.</para>
<para>What happens when you finally finish your work, though?
Your new feature is done, and you're ready to merge your
branch changes back to the trunk (so your team can enjoy the
bounty of your labor). The process is simple. First, bring
your branch in sync with the trunk again, just as you've been
doing all along:</para>
<screen>
$ svn merge http://svn.example.com/repos/calc/trunk
--- Merging r381 through r385 into '.':
U button.c
U README
$ # build, test, ...
$ svn commit -m "Final merge of trunk changes to my-calc-branch."
Sending .
Sending button.c
Sending README
Transmitting file data ..
Committed revision 390.
</screen>
<para>Now, you use <command>svn merge</command> to replicate
your branch changes back into the trunk. You'll need an
up-to-date working copy of <filename>/trunk</filename>. You
can do this by either doing an <command>svn
checkout</command>, dredging up an old trunk working copy from
somewhere on your disk, or using <command>svn
switch</command> (see
<xref linkend="svn.branchmerge.switchwc"/>.) However you get a
trunk working copy, remember that it's a best practice to do
your merge into a working copy that
has <emphasis>no</emphasis> local edits and has been recently
updated (i.e., is not a mixture of local revisions). If your
working copy isn't <quote>clean</quote> in these ways, you can
run into some unnecessary conflict-related headaches
and <command>svn merge</command> will likely return an
error.</para>
<para>Once you have a clean working copy of the trunk, you're
ready to merge your branch back into it:</para>
<screen>
$ pwd
/home/user/calc-trunk
$ svn update # (make sure the working copy is up to date)
At revision 390.
$ svn merge --reintegrate http://svn.example.com/repos/calc/branches/my-calc-branch
--- Merging differences between repository URLs into '.':
U button.c
U integer.c
U Makefile
U .
$ # build, test, verify, ...
$ svn commit -m "Merge my-calc-branch back into trunk!"
Sending .
Sending button.c
Sending integer.c
Sending Makefile
Transmitting file data ..
Committed revision 391.
</screen>
<para>Congratulations, your branch has now been remerged back
into the main line of development. Notice our use of
the <option>--reintegrate</option> option this time around.
The option is critical for reintegrating changes from a branch
back into its original line of development—don't forget
it! It's needed because this sort of <quote>merge
back</quote> is a different sort of work than what you've been
doing up until now. Previously, we had been
asking <command>svn merge</command> to grab the <quote>next
set</quote> of changes from one line of development (the
trunk) and duplicate them to another (your branch). This is
fairly straightforward, and each time Subversion knows how to
pick up where it left off. In our prior examples, you can see
that first it merges the ranges 345:356 from trunk to branch;
later on, it continues by merging the next contiguously
available range, 356:380. When doing the final sync, it
merges the range 380:385.</para>
<para>When merging your branch back to the trunk, however, the
underlying mathematics is quite different. Your feature
branch is now a mishmosh of both duplicated trunk changes and
private branch changes, so there's no simple contiguous range
of revisions to copy over. By specifying
the <option>--reintegrate</option> option, you're asking
Subversion to carefully replicate <emphasis>only</emphasis>
those changes unique to your branch. (And in fact, it does
this by comparing the latest trunk tree with the latest branch
tree: the resulting difference is exactly your branch
changes!)</para>
<para>Now that your private branch is merged to trunk, you may
wish to remove it from the repository:</para>
<screen>
$ svn delete http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Remove my-calc-branch."
Committed revision 392.
</screen>
<para>But wait! Isn't the history of that branch valuable?
What if somebody wants to audit the evolution of your feature
someday and look at all of your branch changes? No need to
worry. Remember that even though your branch is no longer
visible in the <filename>/branches</filename> directory, its
existence is still an immutable part of the repository's
history. A simple <command>svn log</command> command on
the <filename>/branches</filename> URL will show the entire
history of your branch. Your branch can even be resurrected
at some point, should you desire (see
<xref linkend="svn.branchmerge.basicmerging.resurrect"/>).</para>
<para>In Subversion 1.5, once
a <option>--reintegrate</option> merge is done from branch to trunk,
the branch is no longer usable for further work. It's not
able to correctly absorb new trunk changes, nor can it be
properly reintegrated to trunk again. For this reason, if you
want to keep working on your feature branch, we recommend
destroying it and then re-creating it from the trunk:</para>
<screen>
$ svn delete http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Remove my-calc-branch."
Committed revision 392.
$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/branches/new-branch
-m "Create a new branch from trunk."
Committed revision 393.
$ cd my-calc-branch
$ svn switch http://svn.example.com/repos/calc/branches/new-branch
Updated to revision 393.
</screen>
<para>The final command in the prior example—<command>svn
switch</command>—is a way of updating an existing working
copy to reflect a different repository directory. We'll discuss
this more in <xref linkend="svn.branchmerge.switchwc"/>.</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.basicmerging.mergeinfo">
<title>Mergeinfo and Previews</title>
<para>The basic mechanism Subversion uses to track
changesets—that is, which changes have been merged to
which branches—is by recording data in properties.
Specifically, merge data is tracked in
the <literal>svn:mergeinfo</literal> property attached to
files and directories. (If you're not familiar with
Subversion properties, now is the time to skim
<xref linkend="svn.advanced.props"/>.)</para>
<para>You can examine the property, just like any
other:</para>
<screen>
$ cd my-calc-branch
$ svn propget svn:mergeinfo .
/trunk:341-390
</screen>
<para>It is <emphasis>not</emphasis> recommended that you change
the value of this property yourself, unless you really know
what you're doing. This property is automatically maintained
by Subversion whenever you run <command>svn merge</command>.
Its value indicates which changes (at a given path) have been
replicated into the directory in question. In this case, the
path is <filename>/trunk</filename> and the directory which
has received the specific changes
is <filename>/branches/my-calc-branch</filename>.</para>
<para>There's also a subcommand, <command>svn
mergeinfo</command>, which can be helpful in seeing not only
which changesets a directory has absorbed, but also which
changesets it's still eligible to receive. This gives a sort
of preview of the next set of changes that <command>svn
merge</command> will replicate to your branch.</para>
<screen>
$ cd my-calc-branch
# Which changes have already been merged from trunk to branch?
$ svn mergeinfo http://svn.example.com/repos/calc/trunk
r341
r342
r343
…
r388
r389
r390
# Which changes are still eligible to merge from trunk to branch?
$ svn mergeinfo http://svn.example.com/repos/calc/trunk --show-revs eligible
r391
r392
r393
r394
r395
</screen>
<para>The <command>svn mergeinfo</command> command requires
a <quote>source</quote> URL (where the changes would be coming
from), and takes an optional <quote>target</quote> URL (where
the changes would be merged to). If no target URL is given,
it assumes that the current working directory is the
target. In the prior example, because we're querying our
branch working copy, the command assumes we're interested in
receiving changes to <filename>/branches/mybranch</filename>
from the specified trunk URL.</para>
<para>Another way to get a more precise preview of a merge
operation is to use the <option>--dry-run</option>
option:</para>
<screen>
$ svn merge http://svn.example.com/repos/calc/trunk --dry-run
U integer.c
$ svn status
# nothing printed, working copy is still unchanged.
</screen>
<para>The <option>--dry-run</option> option doesn't actually
apply any local changes to the working copy. It shows only
status codes that <emphasis>would</emphasis> be printed in a
real merge. It's useful for getting a <quote>high-level</quote>
preview of the potential merge, for those times
when running <command>svn diff</command> gives too much
detail.</para>
<tip>
<para>After performing a merge operation, but before committing
the results of the merge, you can use <userinput>svn diff
--depth=empty <replaceable>/path/to/merge/target</replaceable></userinput> to see only
the changes to the immediate target of your merge. If your
merge target was a directory, only property differences will
be displayed. This is a handy way to see the changes to the
<literal>svn:mergeinfo</literal> property recorded by the
merge operation, which will remind you about what you've
just merged.</para>
</tip>
<para>Of course, the best way to preview a merge operation is to
just do it! Remember, running <command>svn merge</command>
isn't an inherently risky thing (unless you've made local
modifications to your working copy—but we've already
stressed that you shouldn't be merging into such an
environment). If you don't like the results of the merge,
simply run <userinput>svn revert . -R</userinput> to revert the changes from
your working copy and retry the command with different
options. The merge isn't final until you
actually <command>svn commit</command> the results.</para>
<tip>
<para>While it's perfectly fine to experiment with merges by
running <command>svn merge</command> and <command>svn
revert</command> over and over, you may run into some
annoying (but easily bypassed) roadblocks. For example, if
the merge operation adds a new file (i.e., schedules it for
addition), <command>svn revert</command> won't actually
remove the file; it simply unschedules the addition. You're
left with an unversioned file. If you then attempt to run
the merge again, you may get conflicts due to the
unversioned file <quote>being in the way.</quote> Solution?
After performing a revert, be sure to clean up the working
copy and remove unversioned files and directories. The
output of <command>svn status</command> should be as clean
as possible, ideally showing no output.</para>
</tip>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.basicmerging.undo">
<title>Undoing Changes</title>
<para>An extremely common use for <command>svn merge</command>
is to roll back a change that has already been committed.
Suppose you're working away happily on a working copy of
<filename>/calc/trunk</filename>, and you discover that the
change made way back in revision 303, which changed
<filename>integer.c</filename>, is completely wrong. It never
should have been committed. You can use <command>svn
merge</command> to <quote>undo</quote> the change in your
working copy, and then commit the local modification to the
repository. All you need to do is to specify a
<emphasis>reverse</emphasis> difference. (You can do this by
specifying <option>--revision 303:302</option>, or by an
equivalent <option>--change -303</option>.)</para>
<screen>
$ svn merge -c -303 http://svn.example.com/repos/calc/trunk
--- Reverse-merging r303 into 'integer.c':
U integer.c
$ svn status
M .
M integer.c
$ svn diff
…
# verify that the change is removed
…
$ svn commit -m "Undoing change committed in r303."
Sending integer.c
Transmitting file data .
Committed revision 350.
</screen>
<para>As we mentioned earlier, one way to think about a
repository revision is as a specific changeset. By using the
<option>-r</option> option, you can ask <command>svn
merge</command> to apply a changeset, or a whole range of
changesets, to your working copy. In our case of undoing a
change, we're asking <command>svn merge</command> to apply
changeset #303 to our working copy
<emphasis>backward</emphasis>.</para>
<para>Keep in mind that rolling back a change like this is just
like any other <command>svn merge</command> operation, so you
should use <command>svn status</command> and <command>svn
diff</command> to confirm that your work is in the state you
want it to be in, and then use <command>svn commit</command>
to send the final version to the repository. After
committing, this particular changeset is no longer reflected
in the <literal>HEAD</literal> revision.</para>
<para>Again, you may be thinking: well, that really didn't undo
the commit, did it? The change still exists in revision 303.
If somebody checks out a version of the
<filename>calc</filename> project between revisions 303 and
349, she'll still see the bad change, right?</para>
<para>Yes, that's true. When we talk about
<quote>removing</quote> a change, we're really talking about
removing it from the <literal>HEAD</literal> revision. The
original change still exists in the repository's history. For
most situations, this is good enough. Most people are only
interested in tracking the <literal>HEAD</literal> of a
project anyway. There are special cases, however, where you
really might want to destroy all evidence of the commit.
(Perhaps somebody accidentally committed a confidential
document.) This isn't so easy, it turns out, because
Subversion was deliberately designed to never lose
information. Revisions are immutable trees that build upon
one another. Removing a revision from history would cause a
domino effect, creating chaos in all subsequent revisions and
possibly invalidating all working copies.
<footnote>
<para>The Subversion project has plans, however, to someday
implement a command that would accomplish the task of
permanently deleting information. In the meantime, see
<xref linkend="svn.reposadmin.maint.tk.svndumpfilter"/>
for a possible workaround.</para>
</footnote>
</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.basicmerging.resurrect">
<title>Resurrecting Deleted Items</title>
<para>The great thing about version control systems is that
information is never lost. Even when you delete a file or
directory, it may be gone from the <literal>HEAD</literal>
revision, but the object still exists in earlier revisions.
One of the most common questions new users ask is, <quote>How
do I get my old file or directory back?</quote></para>
<para>The first step is to define
exactly <emphasis>which</emphasis> item you're
trying to resurrect. Here's a useful metaphor: you can think
of every object in the repository as existing in a sort of
two-dimensional coordinate system. The first coordinate is a
particular revision tree, and the second coordinate is a path
within that tree. So every version of your file or directory
can be defined by a specific coordinate pair. (Remember the
<quote>peg revision</quote>
syntax—foo.c@224—mentioned back in
<xref linkend="svn.advanced.pegrevs"/>.) </para>
<para>First, you might need to use <command>svn log</command> to
discover the exact coordinate pair you wish to resurrect. A
good strategy is to run <userinput>svn log --verbose</userinput>
in a directory that used to contain your deleted item. The
<option>--verbose</option> (<option>-v</option>) option shows
a list of all changed items in each revision; all you need to
do is find the revision in which you deleted the file or
directory. You can do this visually, or by using another tool
to examine the log output (via <command>grep</command>, or
perhaps via an incremental search in an editor).</para>
<screen>
$ cd parent-dir
$ svn log -v
…
------------------------------------------------------------------------
r808 | joe | 2003-12-26 14:29:40 -0600 (Fri, 26 Dec 2003) | 3 lines
Changed paths:
D /calc/trunk/real.c
M /calc/trunk/integer.c
Added fast fourier transform functions to integer.c.
Removed real.c because code now in double.c.
…
</screen>
<para>In the example, we're assuming that you're looking for a
deleted file <filename>real.c</filename>. By looking through
the logs of a parent directory, you've spotted that this file
was deleted in revision 808. Therefore, the last version of
the file to exist was in the revision right before that.
Conclusion: you want to resurrect the path
<filename>/calc/trunk/real.c</filename> from revision
807.</para>
<para>That was the hard part—the research. Now that you
know what you want to restore, you have two different
choices.</para>
<para>One option is to use <command>svn merge</command> to apply
revision 808 <quote>in reverse.</quote> (We already
discussed how to undo changes in
<xref linkend="svn.branchmerge.basicmerging.undo"/>.) This
would have the effect of re-adding <filename>real.c</filename>
as a local modification. The file would be scheduled for
addition, and after a commit, the file would again exist
in <literal>HEAD</literal>.</para>
<para>In this particular example, however, this is probably not
the best strategy. Reverse-applying revision 808 would not
only schedule <filename>real.c</filename> for addition, but
the log message indicates that it would also undo certain
changes to <filename>integer.c</filename>, which you don't
want. Certainly, you could reverse-merge revision 808 and
then <command>svn revert</command> the local modifications to
<filename>integer.c</filename>, but this technique doesn't
scale well. What if 90 files were changed in revision
808?</para>
<para>A second, more targeted strategy is not to use
<command>svn merge</command> at all, but rather to use the
<command>svn copy</command> command. Simply copy the exact
revision and path <quote>coordinate pair</quote> from the
repository to your working copy:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk/real.c@807 ./real.c
$ svn status
A + real.c
$ svn commit -m "Resurrected real.c from revision 807, /calc/trunk/real.c."
Adding real.c
Transmitting file data .
Committed revision 1390.
</screen>
<para>The plus sign in the status output indicates that the item
isn't merely scheduled for addition, but scheduled for
addition <quote>with history.</quote> Subversion remembers
where it was copied from. In the future, running <command>svn
log</command> on this file will traverse back through the
file's resurrection and through all the history it had prior
to revision 807. In other words, this new
<filename>real.c</filename> isn't really new; it's a direct
descendant of the original, deleted file. This is usually
considered a good and useful thing. If, however, you wanted
to resurrect the file <emphasis>without</emphasis>
maintaining a historical link to the old file, this technique
works just as well:</para>
<screen>
$ svn cat http://svn.example.com/repos/calc/trunk/real.c@807 > ./real.c
$ svn add real.c
A real.c
$ svn commit -m "Re-created real.c from revision 807."
Adding real.c
Transmitting file data .
Committed revision 1390.
</screen>
<para>Although our example shows us resurrecting a file, note
that these same techniques work just as well for resurrecting
deleted directories. Also note that a resurrection doesn't
have to happen in your working copy—it can happen
entirely in the repository:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk/real.c@807 \
http://svn.example.com/repos/calc/trunk/ \
-m "Resurrect real.c from revision 807."
Committed revision 1390.
$ svn update
A real.c
Updated to revision 1390.
</screen>
</sect2>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.advanced">
<title>Advanced Merging</title>
<para>Here ends the automated magic. Sooner or later, once you
get the hang of branching and merging, you're going to have to
ask Subversion to merge <emphasis>specific</emphasis> changes
from one place to another. To do this, you're going to
have to start passing more complicated arguments to <command>svn
merge</command>. The next section describes the fully expanded
syntax of the command and discusses a number of common
scenarios that require it.</para>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.cherrypicking">
<title>Cherrypicking</title>
<para>Just as the term <quote>changeset</quote> is often used in
version control systems, so is the term
<firstterm>cherrypicking</firstterm>. This word refers to
the act of choosing <emphasis>one</emphasis> specific
changeset from a branch and replicating it to another.
Cherrypicking may also refer to the act of duplicating a
particular set of (not necessarily contiguous!) changesets
from one branch to another. This is in contrast to more
typical merging scenarios, where the <quote>next</quote>
contiguous range of revisions is duplicated
automatically.</para>
<para>Why would people want to replicate just a single change?
It comes up more often than you'd think. For example, let's
go back in time and imagine that you haven't yet merged your
private feature branch back to the trunk. At the
water cooler, you get word that Sally made an interesting
change to <filename>integer.c</filename> on the trunk.
Looking over the history of commits to the trunk, you see that
in revision 355 she fixed a critical bug that directly
impacts the feature you're working on. You might not be ready
to merge all the trunk changes to your branch just yet, but
you certainly need that particular bug fix in order to continue
your work.</para>
<screen>
$ svn diff -c 355 http://svn.example.com/repos/calc/trunk
Index: integer.c
===================================================================
--- integer.c (revision 354)
+++ integer.c (revision 355)
@@ -147,7 +147,7 @@
case 6: sprintf(info->operating_system, "HPFS (OS/2 or NT)"); break;
case 7: sprintf(info->operating_system, "Macintosh"); break;
case 8: sprintf(info->operating_system, "Z-System"); break;
- case 9: sprintf(info->operating_system, "CP/MM");
+ case 9: sprintf(info->operating_system, "CP/M"); break;
case 10: sprintf(info->operating_system, "TOPS-20"); break;
case 11: sprintf(info->operating_system, "NTFS (Windows NT)"); break;
case 12: sprintf(info->operating_system, "QDOS"); break;
</screen>
<para>Just as you used <command>svn diff</command> in the prior
example to examine revision 355, you can pass the same option
to <command>svn merge</command>:</para>
<screen>
$ svn merge -c 355 http://svn.example.com/repos/calc/trunk
U integer.c
$ svn status
M integer.c
</screen>
<para>You can now go through the usual testing procedures before
committing this change to your branch. After the commit,
Subversion marks r355 as having been merged to the branch so
that future <quote>magic</quote> merges that synchronize your
branch with the trunk know to skip over r355. (Merging the
same change to the same branch almost always results in a
conflict!)</para>
<screen>
$ cd my-calc-branch
$ svn propget svn:mergeinfo .
/trunk:341-349,355
# Notice that r355 isn't listed as "eligible" to merge, because
# it's already been merged.
$ svn mergeinfo http://svn.example.com/repos/calc/trunk --show-revs eligible
r350
r351
r352
r353
r354
r356
r357
r358
r359
r360
$ svn merge http://svn.example.com/repos/calc/trunk
--- Merging r350 through r354 into '.':
U .
U integer.c
U Makefile
--- Merging r356 through r360 into '.':
U .
U integer.c
U button.c
</screen>
<para>This use case of replicating
(or <firstterm>backporting</firstterm>) bug fixes from one
branch to another is perhaps the most popular reason for
cherrypicking changes; it comes up all the time, for example,
when a team is maintaining a <quote>release branch</quote> of
software. (We discuss this pattern in
<xref linkend="svn.branchmerge.commonpatterns.release"/>.)</para>
<warning>
<para>Did you notice how, in the last example, the merge
invocation caused two distinct ranges of merges to be
applied? The <command>svn merge</command> command applied
two independent patches to your working copy to
skip over changeset 355, which your branch already
contained. There's nothing inherently wrong with this,
except that it has the potential to make conflict resolution
trickier. If the first range of changes creates
conflicts, you <emphasis>must</emphasis> resolve them
interactively for the merge process to continue and
apply the second range of changes. If you postpone a
conflict from the first wave of changes, the whole merge
command will bail out with an error message.
<footnote>
<para>At least, this is true in Subversion 1.5 at the time
of this writing. This behavior may improve in future
versions of Subversion.</para>
</footnote>
</para>
</warning>
<para>A word of warning: while <command>svn diff</command> and
<command>svn merge</command> are very similar in concept, they
do have different syntax in many cases. Be sure to read about
them in <xref linkend="svn.ref"/> for details, or ask
<command>svn help</command>. For example, <command>svn
merge</command> requires a working copy path as a target, that is,
a place where it should apply the generated patch. If the
target isn't specified, it assumes you are trying to perform
one of the following common operations:</para>
<itemizedlist>
<listitem>
<para>You want to merge directory changes into your current
working directory.</para>
</listitem>
<listitem>
<para>You want to merge the changes in a specific file into
a file by the same name that exists in your current working
directory.</para>
</listitem>
</itemizedlist>
<para>If you are merging a directory and haven't specified a
target path, <command>svn merge</command> assumes the first
case and tries to apply the changes into your current
directory. If you are merging a file, and that file (or a
file by the same name) exists in your current working
directory,
<command>svn merge</command> assumes the second case and tries
to apply the changes to a local file with the same name.</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.advanced.advancedsyntax">
<title>Merge Syntax: Full Disclosure</title>
<para>You've now seen some examples of the <command>svn
merge</command> command, and you're about to see several more.
If you're feeling confused about exactly how merging works,
you're not alone. Many users (especially those new to version
control) are initially perplexed about the proper syntax of
the command and about how and when the feature should be
used. But fear not, this command is actually much simpler
than you think! There's a very easy technique for
understanding exactly how <command>svn merge</command>
behaves.</para>
<para>The main source of confusion is the
<emphasis>name</emphasis> of the command. The term
<quote>merge</quote> somehow denotes that branches are
combined together, or that some sort of mysterious
blending of data is going on. That's not the case. A better
name for the command might have been <command>svn
diff-and-apply</command>, because that's all that happens:
two repository trees are compared, and the differences are
applied to a working copy.</para>
<para>If you're using <command>svn merge</command> to do basic
copying of changes between branches, it will generally do the
right thing automatically. For example, a command such as the
following:</para>
<screen>
$ svn merge http://svn.example.com/repos/calc/some-branch
</screen>
<para>will attempt to duplicate any changes made
on <filename>some-branch</filename> into your current working
directory, which is presumably a working copy that shares some
historical connection to the branch. The command is smart
enough to only duplicate changes that your working copy
doesn't yet have. If you repeat this command once a week, it
will only duplicate the <quote>newest</quote> branch changes
that happened since you last merged.</para>
<para>If you choose to use the <command>svn merge</command>
command in all its full glory by giving it specific revision
ranges to duplicate, the command takes three main
arguments:</para>
<orderedlist>
<listitem><para>An initial repository tree (often called the
<firstterm>left side</firstterm> of the
comparison)</para></listitem>
<listitem><para>A final repository tree (often called the
<firstterm>right side</firstterm> of the
comparison)</para></listitem>
<listitem><para>A working copy to accept the differences as
local changes (often called the <firstterm>target</firstterm>
of the merge)</para></listitem>
</orderedlist>
<para>Once these three arguments are specified, the two trees
are compared, and the differences are applied to the
target working copy as local modifications. When the command
is done, the results are no different than if you had
hand-edited the files or run various <command>svn
add</command> or <command>svn delete</command> commands
yourself. If you like the results, you can commit them. If
you don't like the results, you can simply <command>svn
revert</command> all of the changes.</para>
<para>The syntax of <command>svn merge</command> allows you to
specify the three necessary arguments rather flexibly. Here
are some examples:</para>
<screen>
$ svn merge http://svn.example.com/repos/branch1@150 \
http://svn.example.com/repos/branch2@212 \
my-working-copy
$ svn merge -r 100:200 http://svn.example.com/repos/trunk my-working-copy
$ svn merge -r 100:200 http://svn.example.com/repos/trunk
</screen>
<para>The first syntax lays out all three arguments explicitly,
naming each tree in the form <emphasis>URL@REV</emphasis> and
naming the working copy target. The second syntax can be used
as a shorthand for situations when you're comparing two
different revisions of the same URL. The last syntax shows
how the working copy argument is optional; if omitted, it
defaults to the current directory.</para>
<para>While the first example shows the <quote>full</quote>
syntax of <command>svn merge</command>, it needs to be used
very carefully; it can result in merges which do not record
any <literal>svn:mergeinfo</literal> metadata at all. The
next section talks a bit more about this.</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.nomergedata">
<title>Merges Without Mergeinfo</title>
<para>Subversion tries to generate merge metadata whenever it
can, to make future invocations of <command>svn
merge</command> smarter. There are still situations, however,
where <literal>svn:mergeinfo</literal> data is not created or
changed. Remember to be a bit wary of these scenarios:</para>
<variablelist>
<varlistentry>
<term>Merging unrelated sources</term>
<listitem>
<para>If you
ask <command>svn merge</command> to compare two URLs that
aren't related to each other, a patch will still be
generated and applied to your working copy, but no merging
metadata will be created. There's no common history
between the two sources, and future <quote>smart</quote>
merges depend on that common history.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Merging from foreign repositories</term>
<listitem>
<para>While it's possible to run a
command such as <userinput>svn merge -r 100:200
<replaceable>http://svn.foreignproject.com/repos/trunk</replaceable></userinput>, the
resultant patch will also lack any historical merge
metadata. At time of this writing, Subversion has no way of
representing different repository URLs within
the <literal>svn:mergeinfo</literal> property.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Using <option>--ignore-ancestry</option></term>
<listitem>
<para>If this option is passed to <command>svn
merge</command>, it causes the merging logic to mindlessly
generate differences the same way that <command>svn
diff</command> does, ignoring any historical
relationships. We discuss this later in the chapter in
<xref linkend="svn.branchmerge.advanced.ancestry"/>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Applying reverse merges to a target's natural history</term>
<listitem>
<para>Earlier in this chapter
(<xref linkend="svn.branchmerge.basicmerging.undo"/>)
we discussed how to use <command>svn merge</command>
to apply a <quote>reverse patch</quote> as a way of
rolling back changes. If this technique is used to
undo a change to an object's personal history (e.g.,
commit r5 to the trunk, then immediately roll back r5
using <userinput>svn merge . -c -5</userinput>), this
sort of merge doesn't affect the recorded mergeinfo.
<footnote>
<para>Interestingly, after rolling back a
revision like this, we wouldn't be able to reapply
the revision using <userinput>svn merge . -c 5</userinput>,
since the mergeinfo would already list r5 as being
applied. We would have to use
the <option>--ignore-ancestry</option> option to make
the merge command ignore the existing
mergeinfo!</para>
</footnote>
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.mergeconflicts">
<title>More on Merge Conflicts</title>
<para>Just like the <command>svn update</command> command,
<command>svn merge</command> applies changes to your working
copy. And therefore it's also capable of creating
conflicts. The conflicts produced by <command>svn
merge</command>, however, are sometimes different, and this
section explains those differences.</para>
<para>To begin with, assume that your working copy has no
local edits. When you <command>svn update</command> to a
particular revision, the changes sent by the server will
always apply <quote>cleanly</quote> to your working copy.
The server produces the delta by comparing two trees: a
virtual snapshot of your working copy, and the revision tree
you're interested in. Because the left hand side of the
comparison is exactly equal to what you already have, the
delta is guaranteed to correctly convert your working copy
into the right hand tree.</para>
<para>But <command>svn merge</command> has no such guarantees
and can be much more chaotic: the advanced user can ask the
server to compare <emphasis>any</emphasis> two trees at all,
even ones that are unrelated to the working copy! This means
there's large potential for human error. Users will sometimes
compare the wrong two trees, creating a delta that doesn't
apply cleanly. <command>svn merge</command> will do its best
to apply as much of the delta as possible, but some parts may
be impossible. Just as the Unix
<command>patch</command> command sometimes complains about
<quote>failed hunks,</quote> <command>svn merge</command> will
similarly complain about <quote>skipped
targets</quote>:</para>
<screen>
$ svn merge -r 1288:1351 http://svn.example.com/repos/branch
U foo.c
U bar.c
Skipped missing target: 'baz.c'
U glub.c
U sputter.h
Conflict discovered in 'glorb.h'.
Select: (p) postpone, (df) diff-full, (e) edit,
(h) help for more options:
</screen>
<para>In the previous example, it might be the case that
<filename>baz.c</filename> exists in both snapshots of the
branch being compared, and the resultant delta wants to
change the file's contents, but the file doesn't exist in
the working copy. Whatever the case, the
<quote>skipped</quote> message means that the user is most
likely comparing the wrong two trees; it's the classic
sign of user error. When this happens, it's easy to
recursively revert all the changes created by the merge
(<userinput>svn revert . --recursive</userinput>), delete any
unversioned files or directories left behind after the
revert, and rerun <command>svn merge</command> with
different arguments.</para>
<para>Also notice that the preceeding example shows a conflict
happening on <filename>glorb.h</filename>. We already
stated that the working copy has no local edits: how can a
conflict possibly happen? Again, because the user can use
<command>svn merge</command> to define and apply any old
delta to the working copy, that delta may contain textual
changes that don't cleanly apply to a working file, even if
the file has no local modifications.</para>
<para>Another small difference between <command>svn
update</command> and <command>svn merge</command> is the
names of the full-text files created when a conflict
happens. In <xref linkend="svn.tour.cycle.resolve"/>, we saw
that an update produces files named
<filename>filename.mine</filename>,
<filename>filename.rOLDREV</filename>, and
<filename>filename.rNEWREV</filename>. When <command>svn
merge</command> produces a conflict, though, it creates
three files named <filename>filename.working</filename>,
<filename>filename.left</filename>, and
<filename>filename.right</filename>. In this case, the
terms <quote>left</quote> and <quote>right</quote> are
describing which side of the double-tree comparison the file
came from. In any case, these differing names will help you
distinguish between conflicts that happened as a result of an
update and ones that happened as a result of a
merge.</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.blockchanges">
<title>Blocking Changes</title>
<para>Sometimes there's a particular changeset that you don't
want to be automatically merged. For example, perhaps your
team's policy is to do new development work on
<filename>/trunk</filename>, but to be more conservative about
backporting changes to a stable branch you use for releasing
to the public. On one extreme, you can manually cherrypick
single changesets from the trunk to the branch—just the
changes that are stable enough to pass muster. Maybe things
aren't quite that strict, though; perhaps most of the time
you'd like to just let <command>svn merge</command>
automatically merge most changes from trunk to branch. In
this case, you'd like a way to mask a few specific changes
out, that is, prevent them from ever being automatically
merged.</para>
<para>In Subversion 1.5, the only way to block a changeset is to
make the system believe that the change has
<emphasis>already</emphasis> been merged. To do this, one can
invoke a merge command with the <option>--record-only</option>
option:</para>
<screen>
$ cd my-calc-branch
$ svn propget svn:mergeinfo .
/trunk:1680-3305
# Let's make the metadata list r3328 as already merged.
$ svn merge -c 3328 --record-only http://svn.example.com/repos/calc/trunk
$ svn status
M .
$ svn propget svn:mergeinfo .
/trunk:1680-3305,3328
$ svn commit -m "Block r3328 from being merged to the branch."
…
</screen>
<para>This technique works, but it's also a little bit
dangerous. The main problem is that we're not clearly
differentiating between the ideas of <quote>I already have this
change</quote> and <quote>I don't have this change.</quote>
We're effectively lying to the system, making it think that
the change was previously merged. This puts the
responsibility on you—the user—to remember that
the change wasn't actually merged, it just wasn't wanted.
There's no way to ask Subversion for a list of <quote>blocked
changelists.</quote> If you want to track them (so that you
can unblock them someday). you'll need to record them in a
text file somewhere, or perhaps in an invented property. In
Subversion 1.5, unfortunately, this is the only way to manage
blocked revisions; the plans are to make a better interface
for this in future versions.</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.logblame">
<title>Merge-Sensitive Logs and Annotations</title>
<para>One of the main features of any version control system is
to keep track of who changed what, and when they did it.
The <command>svn log</command> and <command>svn
blame</command> commands are just the tools for this: when
invoked on individual files, they show not only the history of
changesets that affected the file, but also exactly which user
wrote which line of code, and when she did it.</para>
<para>When changes start getting replicated between branches,
however, things start to get complicated. For example, if you
were to ask <command>svn log</command> about the history of
your feature branch, it would show exactly every revision that ever
affected the branch:</para>
<screen>
$ cd my-calc-branch
$ svn log -q
------------------------------------------------------------------------
r390 | user | 2002-11-22 11:01:57 -0600 (Fri, 22 Nov 2002) | 1 line
------------------------------------------------------------------------
r388 | user | 2002-11-21 05:20:00 -0600 (Thu, 21 Nov 2002) | 2 lines
------------------------------------------------------------------------
r381 | user | 2002-11-20 15:07:06 -0600 (Wed, 20 Nov 2002) | 2 lines
------------------------------------------------------------------------
r359 | user | 2002-11-19 19:19:20 -0600 (Tue, 19 Nov 2002) | 2 lines
------------------------------------------------------------------------
r357 | user | 2002-11-15 14:29:52 -0600 (Fri, 15 Nov 2002) | 2 lines
------------------------------------------------------------------------
r343 | user | 2002-11-07 13:50:10 -0600 (Thu, 07 Nov 2002) | 2 lines
------------------------------------------------------------------------
r341 | user | 2002-11-03 07:17:16 -0600 (Sun, 03 Nov 2002) | 2 lines
------------------------------------------------------------------------
r303 | sally | 2002-10-29 21:14:35 -0600 (Tue, 29 Oct 2002) | 2 lines
------------------------------------------------------------------------
r98 | sally | 2002-02-22 15:35:29 -0600 (Fri, 22 Feb 2002) | 2 lines
------------------------------------------------------------------------
</screen>
<para>But is this really an accurate picture of all the changes
that happened on the branch? What's being left out here is
the fact that revisions 390, 381, and 357 were actually the
results of merging changes from the trunk. If you look at one
of these logs in detail, the multiple trunk changesets that
comprised the branch change are nowhere to be seen:</para>
<screen>
$ svn log -v -r 390
------------------------------------------------------------------------
r390 | user | 2002-11-22 11:01:57 -0600 (Fri, 22 Nov 2002) | 1 line
Changed paths:
M /branches/my-calc-branch/button.c
M /branches/my-calc-branch/README
Final merge of trunk changes to my-calc-branch.
</screen>
<para>We happen to know that this merge to the branch was
nothing but a merge of trunk changes. How can we see those
trunk changes as well? The answer is to use the
<option>--use-merge-history</option> (<option>-g</option>)
option. This option expands those <quote>child</quote>
changes that were part of the merge.</para>
<screen>
$ svn log -v -r 390 -g
------------------------------------------------------------------------
r390 | user | 2002-11-22 11:01:57 -0600 (Fri, 22 Nov 2002) | 1 line
Changed paths:
M /branches/my-calc-branch/button.c
M /branches/my-calc-branch/README
Final merge of trunk changes to my-calc-branch.
------------------------------------------------------------------------
r383 | sally | 2002-11-21 03:19:00 -0600 (Thu, 21 Nov 2002) | 2 lines
Changed paths:
M /branches/my-calc-branch/button.c
Merged via: r390
Fix inverse graphic error on button.
------------------------------------------------------------------------
r382 | sally | 2002-11-20 16:57:06 -0600 (Wed, 20 Nov 2002) | 2 lines
Changed paths:
M /branches/my-calc-branch/README
Merged via: r390
Document my last fix in README.
</screen>
<para>By making the log operation use merge history, we see not
just the revision we queried (r390), but also the two revisions
that came along on the ride with it—a couple of changes
made by Sally to the trunk. This is a much more complete
picture of history!</para>
<para>The <command>svn blame</command> command also takes the
<option>--use-merge-history</option> (<option>-g</option>)
option. If this option is neglected, somebody looking at
a line-by-line annotation of <filename>button.c</filename> may
get the mistaken impression that you were responsible for the
lines that fixed a certain error:</para>
<screen>
$ svn blame button.c
…
390 user retval = inverse_func(button, path);
390 user return retval;
390 user }
…
</screen>
<para>And while it's true that you did actually commit those
three lines in revision 390, two of them were actually written
by Sally back in revision 383:</para>
<screen>
$ svn blame button.c -g
…
G 383 sally retval = inverse_func(button, path);
G 383 sally return retval;
390 user }
…
</screen>
<para>Now we know who to <emphasis>really</emphasis> blame for
those two lines of code!</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.ancestry">
<title>Noticing or Ignoring Ancestry</title>
<para>When conversing with a Subversion developer, you might
very likely hear reference to the term
<firstterm>ancestry</firstterm>. This word is used to
describe the relationship between two objects in a
repository: if they're related to each other, one
object is said to be an ancestor of the other.</para>
<para>For example, suppose you commit revision 100, which
includes a change to a file <filename>foo.c</filename>.
Then <filename>foo.c@99</filename> is an
<quote>ancestor</quote> of <filename>foo.c@100</filename>.
On the other hand, suppose you commit the deletion of
<filename>foo.c</filename> in revision 101, and then add a
new file by the same name in revision 102. In this case,
<filename>foo.c@99</filename> and
<filename>foo.c@102</filename> may appear to be related
(they have the same path), but in fact are completely
different objects in the repository. They share no history
or <quote>ancestry.</quote></para>
<para>The reason for bringing this up is to point out an
important difference between <command>svn diff</command> and
<command>svn merge</command>. The former command ignores
ancestry, while the latter command is quite sensitive to it.
For example, if you asked <command>svn diff</command> to
compare revisions 99 and 102 of <filename>foo.c</filename>,
you would see line-based diffs; the <command>diff</command>
command is blindly comparing two paths. But if you asked
<command>svn merge</command> to compare the same two objects,
it would notice that they're unrelated and first attempt to
delete the old file, then add the new file; the output would
indicate a deletion followed by an add:</para>
<screen>
D foo.c
A foo.c
</screen>
<para>Most merges involve comparing trees that are ancestrally
related to one another; therefore, <command>svn
merge</command> defaults to this behavior. Occasionally,
however, you may want the <command>merge</command> command to
compare two unrelated trees. For example, you may have
imported two source-code trees representing different vendor
releases of a software project (see
<xref linkend="svn.advanced.vendorbr"/>). If you ask
<command>svn merge</command> to compare the two trees, you'd
see the entire first tree being deleted, followed by an add
of the entire second tree! In these situations, you'll want
<command>svn merge</command> to do a path-based comparison
only, ignoring any relations between files and directories.
Add the <option>--ignore-ancestry</option> option to your
<command>merge</command> command, and it will behave just
like <command>svn diff</command>. (And conversely, the
<option>--notice-ancestry</option> option will cause
<command>svn diff</command> to behave like the
<command>svn merge</command> command.)</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.moves">
<title>Merges and Moves</title>
<para>A common desire is to refactor source code, especially
in Java-based software projects. Files and directories are
shuffled around and renamed, often causing great disruption
to everyone working on the project. Sounds like a perfect
case to use a branch, doesn't it? Just create a branch,
shuffle things around, and then merge the branch back to the
trunk, right?</para>
<para>Alas, this scenario doesn't work so well right now and
is considered one of Subversion's current weak spots. The
problem is that Subversion's <command>svn update</command>
command isn't as robust as it should be, particularly when
dealing with copy and move operations.</para>
<para>When you use <command>svn copy</command> to duplicate a
file, the repository remembers where the new file came from,
but it fails to transmit that information to the client which
is running <command>svn update</command> or <command>svn
merge</command>. Instead of telling the client, <quote>Copy
that file you already have to this new location,</quote> it
sends down an entirely new file. This can lead to
problems, especially because the same thing happens with
renamed files. A lesser-known fact about Subversion is that
it lacks <quote>true renames</quote>—the <command>svn
move</command> command is nothing more than an aggregation
of <command>svn copy</command> and <command>svn
delete</command>.</para>
<para>For example, suppose that while working on your private
branch, you rename <filename>integer.c</filename>
to <filename>whole.c</filename>. Effectively you've created
a new file in your branch that is a copy of the original
file, and deleted the original file. Meanwhile, back
on <filename>trunk</filename>, Sally has committed some
improvements to <filename>integer.c</filename>. Now you
decide to merge your branch to the trunk:</para>
<screen>
$ cd calc/trunk
$ svn merge --reintegrate http://svn.example.com/repos/calc/branches/my-calc-branch
--- Merging differences between repository URLs into '.':
D integer.c
A whole.c
U .
</screen>
<para>This doesn't look so bad at first glance, but it's also
probably not what you or Sally expected. The merge operation
has deleted the latest version of
the <filename>integer.c</filename> file (the one containing
Sally's latest changes), and blindly added your
new <filename>whole.c</filename> file—which is a
duplicate of the <emphasis>older</emphasis> version
of <filename>integer.c</filename>. The net effect is that
merging your <quote>rename</quote> to the branch has removed
Sally's recent changes from the latest revision!</para>
<para>This isn't true data loss. Sally's changes are still in
the repository's history, but it may not be immediately
obvious that this has happened. The moral of this story is
that until Subversion improves, be very careful about
merging copies and renames from one branch to
another.</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.pre1.5clients">
<title>Blocking Merge-Unaware Clients</title>
<para>If you've just upgraded your server to Subversion 1.5 or
later, there's a significant risk that pre-1.5 Subversion
clients can mess up your automated merge tracking. Why is
this? When a pre-1.5 Subversion client performs <command>svn
merge</command>, it doesn't modify the value of
the <literal>svn:mergeinfo</literal> property at all. So the
subsequent commit, despite being the result of a merge,
doesn't tell the repository about the duplicated
changes—that information is lost. Later on,
when <quote>merge-aware</quote> clients attempt automatic
merging, they're likely to run into all sorts of conflicts
resulting from repeated merges.</para>
<para>If you and your team are relying on the merge-tracking
features of Subversion, you may want to configure your
repository to prevent older clients from committing changes.
The easy way to do this is by inspecting
the <quote>capabilities</quote> parameter in
the <literal>start-commit</literal> hook script. If the
client reports itself as having <literal>mergeinfo</literal>
capabilities, the hook script can allow the commit to start.
If the client doesn't report that capability, have the hook
deny the commit. We'll learn more about hook scripts in the
next chapter; see
<xref linkend="svn.reposadmin.create.hooks"/> and
<xref linkend="svn.ref.reposhooks.start-commit"/> for
details.</para>
</sect2>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.advanced.finalword">
<title>The Final Word on Merge Tracking</title>
<para>The bottom line is that Subversion's merge-tracking
feature has an extremely complex internal implementation, and
the <literal>svn:mergeinfo</literal> property is the only
window the user has into the machinery. Because the feature
is relatively new, a numbers of edge cases and
possible unexpected behaviors may pop up.</para>
<para>For example, sometimes mergeinfo will be generated when
running a simple <command>svn copy</command> or <command>svn
move</command> command. Sometimes mergeinfo will appear on
files that you didn't expect to be touched by an operation.
Sometimes mergeinfo won't be generated at all, when you expect
it to. Furthermore, the management of mergeinfo metadata has
a whole set of taxonomies and behaviors around it, such
as <quote>explicit</quote> versus <quote>implicit</quote>
mergeinfo, <quote>operative</quote>
versus <quote>inoperative</quote> revisions, specific
mechanisms of mergeinfo <quote>elision,</quote> and
even <quote>inheritance</quote> from parent to child
directories.</para>
<para>We've chosen not to cover these detailed topics in this
book for a couple of reasons. First, the level of detail is
absolutely overwhelming for a typical user. Second, as
Subversion continues to improve, we feel that a typical user
<emphasis>shouldn't</emphasis> have to understand these
concepts; they'll eventually fade into the background as pesky
implementation details. All that said, if you enjoy this sort
of thing, you can get a fantastic overview in a paper posted
at CollabNet's website: <ulink
url="http://www.collab.net/community/subversion/articles/merge-info.html"
/>.</para>
<para>For now, if you want to steer clear of bugs and odd
behaviors in automatic merging, the CollabNet article
recommends that you stick to these simple best practices:</para>
<itemizedlist>
<listitem>
<para>For short-term feature branches, follow the simple
procedure described throughout
<xref linkend="svn.branchmerge.basicmerging"/>.</para>
</listitem>
<listitem>
<para>For long-lived release branches (as described in
<xref linkend="svn.branchmerge.commonpatterns"/>),
perform merges only on the root of the branch, not on
subdirectories.</para>
</listitem>
<listitem>
<para>Never merge into working copies with a mixture of
working revision numbers, or with
<quote>switched</quote> subdirectories (as described next
in <xref linkend="svn.branchmerge.switchwc"/>). A merge
target should be a working copy which represents
a <emphasis>single</emphasis> location in the repository
at a single point in time.</para>
</listitem>
<listitem>
<para>Don't ever edit the <literal>svn:mergeinfo</literal>
property directly; use <command>svn
merge</command> with the <option>--record-only</option> option to effect a desired change
to the metadata (as demonstrated in
<xref linkend="svn.branchmerge.advanced.blockchanges"/>).</para>
</listitem>
<listitem>
<para>Always make sure you have complete read access to
all of your merge sources, and that your target working
copy has no sparse directories.</para>
</listitem>
</itemizedlist>
</sect2>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.switchwc">
<title>Traversing Branches</title>
<para>The <command>svn switch</command> command transforms an
existing working copy to reflect a different branch. While this
command isn't strictly necessary for working with branches, it
provides a nice shortcut. In our earlier example,
after creating your private branch, you checked out a fresh
working copy of the new repository directory. Instead, you can
simply ask Subversion to change your working copy of
<filename>/calc/trunk</filename> to mirror the new branch
location:</para>
<screen>
$ cd calc
$ svn info | grep URL
URL: http://svn.example.com/repos/calc/trunk
$ svn switch http://svn.example.com/repos/calc/branches/my-calc-branch
U integer.c
U button.c
U Makefile
Updated to revision 341.
$ svn info | grep URL
URL: http://svn.example.com/repos/calc/branches/my-calc-branch
</screen>
<para><quote>Switching</quote> a working copy that has no local
modifications to a different branch results in the working copy
looking just as it would if you'd done a fresh checkout of the
directory. It's usually more efficient to
use this command, because often branches differ by only a small
degree. The server sends only the minimal set of changes
necessary to make your working copy reflect the branch
directory.</para>
<para>The <command>svn switch</command> command also takes a
<option>--revision</option> (<option>-r</option>) option, so you
need not always move your working copy to the
<literal>HEAD</literal> of the branch.</para>
<para>Of course, most projects are more complicated than our
<filename>calc</filename> example, and contain multiple
subdirectories. Subversion users often follow a specific
algorithm when using branches:</para>
<orderedlist>
<listitem>
<para>Copy the project's entire <quote>trunk</quote> to a
new branch directory.</para>
</listitem>
<listitem>
<para>Switch only <emphasis>part</emphasis> of the trunk
working copy to mirror the branch.</para>
</listitem>
</orderedlist>
<para>In other words, if a user knows that the branch work needs
to happen on only a specific subdirectory, she uses
<command>svn switch</command> to move only that subdirectory to
the branch. (Or sometimes users will switch just a single
working file to the branch!) That way, the user can continue to
receive normal <quote>trunk</quote> updates to most of her
working copy, but the switched portions will remain immune
(unless someone commits a change to her branch). This feature
adds a whole new dimension to the concept of a <quote>mixed
working copy</quote>—not only can working copies contain a
mixture of working revisions, but they can also contain a
mixture of repository locations as well.</para>
<para>If your working copy contains a number of switched subtrees
from different repository locations, it continues to function as
normal. When you update, you'll receive patches to each subtree
as appropriate. When you commit, your local changes will still
be applied as a single, atomic change to the repository.</para>
<para>Note that while it's okay for your working copy to reflect a
mixture of repository locations, these locations must all be
within the <emphasis>same</emphasis> repository. Subversion
repositories aren't yet able to communicate with one another;
that feature is planned for the future.
<footnote>
<para>You <emphasis>can</emphasis>, however, use <command>svn
switch</command> with the <option>--relocate</option> option
if the URL of your server changes and you don't want to
abandon an existing working copy. See <xref
linkend="svn.ref.svn.c.switch"/> for more information and an
example.</para>
</footnote>
</para>
<sidebar>
<title>Switches and Updates</title>
<para>Have you noticed that the output of <command>svn
switch</command> and <command>svn update</command> looks the
same? The switch command is actually a superset of the update
command.</para>
<para>When you run <command>svn update</command>, you're asking
the repository to compare two trees. The repository does so,
and then sends a description of the differences back to the
client. The only difference between <command>svn
switch</command> and <command>svn update</command> is that the
latter command always compares two identical repository
paths.</para>
<para>That is, if your working copy is a mirror of
<filename>/calc/trunk</filename>, <command>svn
update</command> will automatically compare your working copy
of <filename>/calc/trunk</filename> to
<filename>/calc/trunk</filename> in the
<literal>HEAD</literal> revision. If you're switching your
working copy to a branch, <command>svn switch</command>
will compare your working copy of
<filename>/calc/trunk</filename> to some
<emphasis>other</emphasis> branch directory in the
<literal>HEAD</literal> revision.</para>
<para>In other words, an update moves your working copy through
time. A switch moves your working copy through time
<emphasis>and</emphasis> space.</para>
</sidebar>
<para>Because <command>svn switch</command> is essentially a
variant of <command>svn update</command>, it shares the same
behaviors; any local modifications in your working copy are
preserved when new data arrives from the repository.</para>
<tip>
<para>Have you ever found yourself making some complex edits
(in your <filename>/trunk</filename> working copy) and
suddenly realized, <quote>Hey, these changes ought to be in
their own branch?</quote> A great technique to do this can
be summarized in two steps:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/branches/newbranch \
-m "Create branch 'newbranch'."
Committed revision 353.
$ svn switch http://svn.example.com/repos/calc/branches/newbranch
At revision 353.
</screen>
<para>The <command>svn switch</command> command, like
<command>svn update</command>, preserves your local edits.
At this point, your working copy is now a reflection of the
newly created branch, and your next <command>svn
commit</command> invocation will send your changes
there.</para> </tip>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.tags">
<title>Tags</title>
<para>Another common version control concept is a
<firstterm>tag</firstterm>. A tag is just a
<quote>snapshot</quote> of a project in time. In Subversion,
this idea already seems to be everywhere. Each repository
revision is exactly that—a snapshot of the filesystem
after each commit.</para>
<para>However, people often want to give more human-friendly names
to tags, such as <literal>release-1.0</literal>. And they want
to make snapshots of smaller subdirectories of the filesystem.
After all, it's not so easy to remember that release 1.0 of a
piece of software is a particular subdirectory of revision
4822.</para>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.tags.mksimple">
<title>Creating a Simple Tag</title>
<para>Once again, <command>svn copy</command> comes to the
rescue. If you want to create a snapshot of
<filename>/calc/trunk</filename> exactly as it looks in the
<literal>HEAD</literal> revision, make a copy of it:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/tags/release-1.0 \
-m "Tagging the 1.0 release of the 'calc' project."
Committed revision 902.
</screen>
<para>This example assumes that a
<filename>/calc/tags</filename> directory already exists. (If
it doesn't, you can create it using <command>svn
mkdir</command>.) After the copy completes, the new
<filename>release-1.0</filename> directory is forever a
snapshot of how the <filename>/trunk</filename> directory
looked in the <literal>HEAD</literal> revision at the time you
made the copy. Of course, you might want to be more precise
about exactly which revision you copy, in case somebody else
may have committed changes to the project when you weren't
looking. So if you know that revision 901 of
<filename>/calc/trunk</filename> is exactly the snapshot you
want, you can specify it by passing <option>-r 901</option> to
the <command>svn copy</command> command.</para>
<para>But wait a moment: isn't this tag creation procedure the
same procedure we used to create a branch? Yes, in fact, it
is. In Subversion, there's no difference between a tag and a
branch. Both are just ordinary directories that are created
by copying. Just as with branches, the only reason a copied
directory is a <quote>tag</quote> is because
<emphasis>humans</emphasis> have decided to treat it that way:
as long as nobody ever commits to the directory, it forever
remains a snapshot. If people start committing to it, it
becomes a branch.</para>
<para>If you are administering a repository, there are two
approaches you can take to managing tags. The first approach
is <quote>hands off</quote>: as a matter of project policy,
decide where your tags will live, and make sure all users know
how to treat the directories they copy. (That is, make sure
they know not to commit to them.) The second approach is more
paranoid: you can use one of the access control scripts
provided with Subversion to prevent anyone from doing anything
but creating new copies in the tags area (see
<xref linkend="svn.serverconfig"/>). The paranoid approach,
however, isn't usually necessary. If a user accidentally
commits a change to a tag directory, you can simply undo the
change as discussed in the previous section. This is version
control, after all!</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.tags.mkcomplex">
<title>Creating a Complex Tag</title>
<para>Sometimes you may want your <quote>snapshot</quote> to be
more complicated than a single directory at a single
revision.</para>
<para>For example, pretend your project is much larger than our
<filename>calc</filename> example: suppose it contains a
number of subdirectories and many more files. In the course
of your work, you may decide that you need to create a working
copy that is designed to have specific features and bug fixes.
You can accomplish this by selectively backdating files or
directories to particular revisions (using <command>svn update</command>
with the <option>-r</option> option liberally), by switching files and directories to
particular branches (making use of <command>svn
switch</command>), or even just by making a bunch of local
changes. When you're done, your working copy is a hodgepodge
of repository locations from different revisions. But after
testing, you know it's the precise combination of data you
need to tag.</para>
<para>Time to make a snapshot. Copying one URL to another won't
work here. In this case, you want to make a snapshot of your
exact working copy arrangement and store it in the repository.
Luckily, <command>svn copy</command> actually has four
different uses (which you can read about in <xref
linkend="svn.ref"/>), including the ability to copy a
working copy tree to the repository:</para>
<screen>
$ ls
my-working-copy/
$ svn copy my-working-copy \
http://svn.example.com/repos/calc/tags/mytag \
-m "Tag my existing working copy state."
Committed revision 940.
</screen>
<para>Now there is a new directory in the repository,
<filename>/calc/tags/mytag</filename>, which is an exact
snapshot of your working copy—mixed revisions, URLs,
local changes, and all.</para>
<para>Other users have found interesting uses for this feature.
Sometimes there are situations where you have a bunch of local
changes made to your working copy, and you'd like a
collaborator to see them. Instead of running <command>svn
diff</command> and sending a patch file (which won't capture
directory, symlink, or property changes), you can
use <command>svn copy</command> to <quote>upload</quote> your
working copy to a private area of the repository. Your
collaborator can then either check out a verbatim copy of your
working copy or use <command>svn merge</command> to receive
your exact changes.</para>
<para>While this is a nice method for uploading a quick snapshot
of your working copy, note that this is <emphasis>not</emphasis>
a good way to initially create a branch. Branch creation should
be an event unto itself, and this method conflates the creation
of a branch with extra changes to files, all within a single revision.
This makes it very difficult (later on) to identify a single
revision number as a branch point.</para>
</sect2>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.maint">
<title>Branch Maintenance</title>
<para>You may have noticed by now that Subversion is extremely
flexible. Because it implements branches and tags with the same
underlying mechanism (directory copies), and because branches
and tags appear in normal filesystem space, many people find
Subversion intimidating. It's almost <emphasis>too</emphasis>
flexible. In this section, we'll offer some suggestions for
arranging and managing your data over time.</para>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.maint.layout">
<title>Repository Layout</title>
<para>There are some standard, recommended ways to organize a
repository. Most people create a <filename>trunk</filename>
directory to hold the <quote>main line</quote> of development,
a <filename>branches</filename> directory to contain branch
copies, and a <filename>tags</filename> directory to contain
tag copies. If a repository holds only one project,
often people create these top-level directories:</para>
<screen>
/trunk
/branches
/tags
</screen>
<para>If a repository contains multiple projects, admins
typically index their layout by project (see <xref
linkend="svn.reposadmin.projects.chooselayout"/> to read more about
<quote>project roots</quote>):</para>
<screen>
/paint/trunk
/paint/branches
/paint/tags
/calc/trunk
/calc/branches
/calc/tags
</screen>
<para>Of course, you're free to ignore these common layouts.
You can create any sort of variation, whatever works best for
you or your team. Remember that whatever you choose, it's not
a permanent commitment. You can reorganize your repository at
any time. Because branches and tags are ordinary directories,
the <command>svn move</command> command can move or rename
them however you wish. Switching from one layout to another
is just a matter of issuing a series of server-side moves; if
you don't like the way things are organized in the repository,
just juggle the directories around.</para>
<para>Remember, though, that while moving directories may be
easy to do, you need to be considerate of your users as well.
Your juggling can be disorienting to users with existing
working copies. If a user has a working copy of a particular
repository directory, your <command>svn move</command>
operation might remove the path from the latest revision.
When the user next runs <command>svn update</command>, she will
be told that her working copy represents a path that no
longer exists, and the user will be forced to <command>svn
switch</command> to the new location.
</para>
</sect2>
<!-- =============================================================== -->
<sect2 id="svn.branchmerge.maint.lifetime">
<title>Data Lifetimes</title>
<para>Another nice feature of Subversion's model is that
branches and tags can have finite lifetimes, just like any
other versioned item. For example, suppose you eventually
finish all your work on your personal branch of the
<filename>calc</filename> project. After merging all of your
changes back into <filename>/calc/trunk</filename>, there's
no need for your private branch directory to stick around
anymore:</para>
<screen>
$ svn delete http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Removing obsolete branch of calc project."
Committed revision 375.
</screen>
<para>And now your branch is gone. Of course, it's not really
gone: the directory is simply missing from the
<literal>HEAD</literal> revision, no longer distracting
anyone. If you use <command>svn checkout</command>,
<command>svn switch</command>, or <command>svn list</command>
to examine an earlier revision, you'll still be able to see
your old branch.</para>
<para>If browsing your deleted directory isn't enough, you can
always bring it back. Resurrecting data is very easy in
Subversion. If there's a deleted directory (or file) that
you'd like to bring back into <literal>HEAD</literal>, simply
use <command>svn copy</command> to copy it from the old
revision:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/branches/my-calc-branch@374 \
http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Restore my-calc-branch."
Committed revision 376.
</screen>
<para>In our example, your personal branch had a relatively
short lifetime: you may have created it to fix a bug or
implement a new feature. When your task is done, so is the
branch. In software development, though, it's also common to
have two <quote>main</quote> branches running side by side for
very long periods. For example, suppose it's time to release
a stable version of the <filename>calc</filename> project to the
public, and you know it's going to take a couple of months to
shake bugs out of the software. You don't want people to add
new features to the project, but you don't want to tell all
developers to stop programming either. So instead, you create
a <quote>stable</quote> branch of the software that won't
change much:</para>
<screen>
$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/branches/stable-1.0 \
-m "Creating stable branch of calc project."
Committed revision 377.
</screen>
<para>And now developers are free to continue adding
cutting-edge (or experimental) features to
<filename>/calc/trunk</filename>, and you can declare a
project policy that only bug fixes are to be committed to
<filename>/calc/branches/stable-1.0</filename>. That is, as
people continue to work on the trunk, a human selectively
ports bug fixes over to the stable branch. Even after the
stable branch has shipped, you'll probably continue to
maintain the branch for a long time—that is, as long
as you continue to support that release for customers. We'll
discuss this more in the next section.</para>
</sect2>
</sect1>
<!-- ================================================================= -->
<!-- ================================================================= -->
<!-- ================================================================= -->
<sect1 id="svn.branchmerge.commonpatterns">
<title>Common Branching Patterns</title>
<para>There are many different uses for branching and <command>svn
merge</command>, and this section describes the most
common.</para>
<para>Version control is most often used for software
development, so here's a quick peek at two of the most common
branching/merging patterns used by teams of programmers. If
you're not using Subversion for software development, feel
free to skip this section. If you're a software developer
using version control for the first time, pay close attention,
as these patterns are often considered best practices by
experienced folk. These processes aren't specific to
Subversion; they're applicable to any version control system.
Still, it may help to see them described in Subversion
terms.</para>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<sect2 id="svn.branchmerge.commonpatterns.release">
<title>Release Branches</title>
<para>Most software has a typical life cycle: code, test,
release, repeat. There are two problems with this process.
First, developers need to keep writing new features while
quality assurance teams take time to test supposedly stable
versions of the software. New work cannot halt while the
software is tested. Second, the team almost always needs to
support older, released versions of software; if a bug is
discovered in the latest code, it most likely exists in
released versions as well, and customers will want to get
that bug fix without having to wait for a major new
release.</para>
<para>Here's where version control can help. The typical
procedure looks like this:</para>
<orderedlist>
<listitem>
<para><emphasis>Developers commit all new work to the
trunk.</emphasis>
Day-to-day changes are committed to
<filename>/trunk</filename>: new features, bug fixes, and
so on.</para>
</listitem>