Skip to content

Commit 444e10d

Browse files
committed
Merge branch 'mm/maint-hint-failed-merge'
* mm/maint-hint-failed-merge: user-manual: Document that "git merge" doesn't like uncommited changes. merge-recursive: point the user to commit when file would be overwritten.
2 parents e61f25f + e63ec00 commit 444e10d

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ advice.*::
126126
Directions on how to stage/unstage/add shown in the
127127
output of linkgit:git-status[1] and the template shown
128128
when writing commit messages. Default: true.
129+
commitBeforeMerge::
130+
Advice shown when linkgit:git-merge[1] refuses to
131+
merge to avoid overwritting local changes.
132+
Default: true.
129133
--
130134

131135
core.fileMode::

Documentation/user-manual.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,23 @@ $ git merge branchname
11831183
-------------------------------------------------
11841184

11851185
merges the development in the branch "branchname" into the current
1186-
branch. If there are conflicts--for example, if the same file is
1186+
branch.
1187+
1188+
A merge is made by combining the changes made in "branchname" and the
1189+
changes made up to the latest commit in your current branch since
1190+
their histories forked. The work tree is overwritten by the result of
1191+
the merge when this combining is done cleanly, or overwritten by a
1192+
half-merged results when this combining results in conflicts.
1193+
Therefore, if you have uncommitted changes touching the same files as
1194+
the ones impacted by the merge, Git will refuse to proceed. Most of
1195+
the time, you will want to commit your changes before you can merge,
1196+
and if you don't, then linkgit:git-stash[1] can take these changes
1197+
away while you're doing the merge, and reapply them afterwards.
1198+
1199+
If the changes are independant enough, Git will automatically complete
1200+
the merge and commit the result (or reuse an existing commit in case
1201+
of <<fast-forwards,fast-forward>>, see below). On the other hand,
1202+
if there are conflicts--for example, if the same file is
11871203
modified in two different ways in the remote branch and the local
11881204
branch--then you are warned; the output may look something like this:
11891205

@@ -1679,7 +1695,7 @@ Sharing development with others
16791695
Getting updates with git pull
16801696
-----------------------------
16811697

1682-
After you clone a repository and make a few changes of your own, you
1698+
After you clone a repository and commit a few changes of your own, you
16831699
may wish to check the original repository for updates and merge them
16841700
into your own work.
16851701

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
int advice_push_nonfastforward = 1;
44
int advice_status_hints = 1;
5+
int advice_commit_before_merge = 1;
56

67
static struct {
78
const char *name;
89
int *preference;
910
} advice_config[] = {
1011
{ "pushnonfastforward", &advice_push_nonfastforward },
1112
{ "statushints", &advice_status_hints },
13+
{ "commitbeforemerge", &advice_commit_before_merge },
1214
};
1315

1416
int git_default_advice_config(const char *var, const char *value)

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
extern int advice_push_nonfastforward;
55
extern int advice_status_hints;
6+
extern int advice_commit_before_merge;
67

78
int git_default_advice_config(const char *var, const char *value);
89

merge-recursive.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Fredrik Kuivinen.
44
* The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
55
*/
6+
#include "advice.h"
67
#include "cache.h"
78
#include "cache-tree.h"
89
#include "commit.h"
@@ -171,7 +172,7 @@ static int git_merge_trees(int index_only,
171172
int rc;
172173
struct tree_desc t[3];
173174
struct unpack_trees_options opts;
174-
static const struct unpack_trees_error_msgs msgs = {
175+
struct unpack_trees_error_msgs msgs = {
175176
/* would_overwrite */
176177
"Your local changes to '%s' would be overwritten by merge. Aborting.",
177178
/* not_uptodate_file */
@@ -183,6 +184,11 @@ static int git_merge_trees(int index_only,
183184
/* bind_overlap -- will not happen here */
184185
NULL,
185186
};
187+
if (advice_commit_before_merge) {
188+
msgs.would_overwrite = msgs.not_uptodate_file =
189+
"Your local changes to '%s' would be overwritten by merge. Aborting.\n"
190+
"Please, commit your changes or stash them before you can merge.";
191+
}
186192

187193
memset(&opts, 0, sizeof(opts));
188194
if (index_only)

0 commit comments

Comments
 (0)