interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitmergeconflicts.adoc

339 lines · 9888 bytes Raw Blame History
1gitmergeconflicts(7)
2====================
3
4NAME
5----
6gitmergeconflicts - Guide to handling merge conflicts
7
8DESCRIPTION
9-----------
10
11Merge conflicts can happen during a `git merge`, `git rebase`, `git
12cherry-pick`, `git pull`, `git revert`, or any other command that does
13a merge. All of those commands use the same merge algorithm, and the
14process for resolving a merge conflict is always very similar.
15
16There are 3 ways to handle a merge conflict:
17
18* Resolve the conflict. (see <<resolve,HOW TO RESOLVE A MERGE CONFLICT>>
19 below for details)
20* Stop the operation and return your branch to its original state
21 with the appropriate `--abort` command, for example `git merge --abort`
22 or `git rebase --abort`. See <<git_status,EXAMPLE: GIT STATUS OUTPUT>> below
23 for how to find the command to run.
24* Or (if you're in a multi-step operation like a rebase) skip the problematic
25 commit with the appropriate `--skip` command.
26
27WHAT IS A MERGE CONFLICT?
28-------------------------
29
30When Git merges two commits together, it looks at the changes that
31each side has made and combines those changes. For example, if one side
32edited lines 1-5 of `hello.py` and the other side edited lines 20-25 of
33the same file, then it can easily combine them since there's no overlap.
34
35But if both sides edited overlapping lines of the same file (for example
36one side edited lines 1-5 and the other edited lines 3-6), Git will
37not try to guess how to combine those changes. This is called a "merge
38conflict".
39
40When this happens, Git shows you both sides' edits and asks you to pick
41how to resolve them. It:
42
43* Stages all of the files which were successfully merged
44* For the files with conflicts, it marks them as conflicted, puts both
45 sides' edits in the file, and leaves <<markers, merge conflict markers>>
46 that you need to resolve.
47
48[NOTE]
49Even if Git successfully merges both sides with no conflicts, it's
50possible to end up with code that doesn't work. For example, this can
51happen if one side renames a function, and the other side adds a call to
52the old function. This is called a "semantic merge conflict".
53
54[[markers]]
55MERGE CONFLICT MARKERS
56----------------------
57
58When there's a merge conflict, Git will update the conflicted file
59to include merge conflict markers `<<<<<<<`, `=======`, and `>>>>>>>`.
60For example, here's a merge conflict where both sides edited a list of
61fruits in different ways -- one side has "cherry" and the other side
62has "banana":
63
64----
65FRUITS = [
66 "apple",
67<<<<<<< HEAD
68 "cherry",
69=======
70 "banana",
71>>>>>>> add-fruit
72 "mango",
73 "orange",
74]
75----
76
77The lines from one side of the merge conflict is between `<<<<<<<` and
78`=======`, and the lines from the other side is between `=======` and
79`>>>>>>>`. See <<ours,"OURS" AND "THEIRS">> below for a full explanation
80of which side is which.
81
82
83[[resolve]]
84HOW TO RESOLVE A MERGE CONFLICT
85-------------------------------
86
87The process for resolving a merge conflict is:
88
891. Run `git status` to get a list of files with merge conflicts.
902. For each one, find the conflict markers
91 (the `<<<<<<<`, `=======`, `>>>>>>>`) and edit the file to
92 fix the conflict.
933. Run `git add FILENAME` for each file to mark the conflict as resolved.
944. Run the appropriate `--continue` command to continue the operation
95 that was interrupted by the conflict, for example `git merge --continue`
96 or `git rebase --continue`. See <<git_status,EXAMPLE: GIT STATUS OUTPUT>>
97 below for how to find the command to run.
98+
99Note: During a `git merge`, `git commit` and `git merge --continue` do
100the same thing.
101
102
103[[example]]
104EXAMPLE OF RESOLVING A MERGE CONFLICT
105-------------------------------------
106
107If you see this in your code during a merge conflict:
108
109----
110FRUITS = [
111 "apple",
112<<<<<<< HEAD
113 "cherry",
114=======
115 "banana",
116>>>>>>> add-fruit
117 "mango",
118 "orange",
119]
120----
121
122Then you might edit that part of the code like this,
123which includes the fruits from both sides of the conflict:
124
125----
126FRUITS = [
127 "apple",
128 "banana",
129 "cherry",
130 "mango",
131 "orange",
132]
133----
134
135
136[[tools]]
137TOOLS FOR HANDLING MERGE CONFLICTS
138----------------------------------
139
140Here are some ways to get extra context while handling a merge conflict:
141
142* There are many graphical "merge tools" for Git, which will normally
143 show you the different versions of the code side by side.
144 If you have a mergetool configured, `git mergetool` will launch it.
145 See also `merge.tool` in linkgit:git-config[1] for a list of
146 the mergetools Git supports.
147
148* You can set the configuration variable `merge.conflictstyle=diff3`.
149 See <<diff3,DIFF3>> below for more.
150
151* `git log --merge --patch <filename>` will list all commits which
152 caused the merge conflict for `<filename>`, and the diff
153 of how they changed the file.
154
155* Look at the original files. `git show :1:filename` shows the
156 common ancestor, `git show :2:filename` shows the "ours"
157 version, and `git show :3:filename` shows the "theirs"
158 version. See <<ours,"OURS" AND "THEIRS">> below for more on
159 "ours" and "theirs".
160
161Here are some ways to track your progress while handling a conflict:
162
163* Use `git status` to get a list of files with conflicts
164
165* Use `git diff --check` to make sure you haven't left any merge
166 conflict markers in a file by accident. It will print "leftover
167 conflict marker" if it finds any.
168
169* Use `git diff AUTO_MERGE` to show what changes you've made so far to
170 resolve the conflicts.
171 See <<automerge,EXAMPLE OF USING `AUTO_MERGE`>> below for more.
172
173[[git_status]]
174EXAMPLE: GIT STATUS OUTPUT
175--------------------------
176
177When you're in a merge conflict, you can find out what commands to run
178to handle the conflict by running `git status`.
179
180For example, this `git status` output tells you that:
181
182* `git rebase --abort` will safely bring your branch back to its
183 original state
184* you should run `git rebase --continue` when you're done resolving all
185 the conflicts
186* the conflicts in `fruits.py` haven't been marked as resolved
187
188----
189$ git status
190You are currently rebasing branch 'main' on '58a9fcc'.
191 (fix conflicts and then run "git rebase --continue")
192 (use "git rebase --skip" to skip this patch)
193 (use "git rebase --abort" to check out the original branch)
194
195Unmerged paths:
196 (use "git restore --staged <file>..." to unstage)
197 (use "git add <file>..." to mark resolution)
198 both modified: fruits.py
199----
200
201
202[[diff3]]
203DIFF3
204-----
205
206By default, Git doesn't include the original code when formatting
207a merge conflict. To include the original code, you can set the
208configuration option `merge.conflictstyle` to `diff3`.
209This extra context can make it much easier to understand what's
210happening in a merge conflict.
211
212Here's an example of what a merge conflict would look like when using
213`diff3`. There's an extra section (between `|||||||` and `=======`)
214with the original code.
215
216----
217FRUITS = [
218 "apple",
219<<<<<<< HEAD
220 "cherry",
221 "mango",
222||||||| 1c22e48
223 "banana",
224 "cherry",
225 "mangoooo",
226=======
227 "banana",
228 "mango",
229>>>>>>> add-fruit
230 "orange",
231]
232----
233
234With this view, you can see that both sides fixed a spelling
235mistake in "mango", and each removed one fruit from the list.
236This makes it more clear how to resolve the merge conflict: since one
237side removed `banana` and one side removed `cherry`, it would probably
238make sense to remove both from the list, like this:
239
240----
241FRUITS = [
242 "apple",
243 "mango",
244 "orange",
245]
246----
247
248[NOTE]
249`zdiff3` is an alternative to `diff3` that makes the merge conflict
250shorter by making it more ambiguous. See linkgit:git-config[1] for more.
251
252[[ours]]
253"OURS" AND "THEIRS"
254-------------------
255
256Sometimes during a merge conflict, Git will use the terms "ours" and
257"theirs" (or "us" and "them"). For example, `git status` might say that
258a file was `deleted by us`.
259
260"Ours" and "theirs" are both commits: "ours" is the current
261`HEAD` commit, and "theirs" is the other side being merged.
262
263The first part of a merge conflict (between `<<<<<<<` and `=======`) is
264from the "ours" side, and the second part (between `=======` and
265`>>>>>>>`) is from the "theirs" side.
266
267----
268FRUITS = [
269 "apple",
270<<<<<<< HEAD
271 "cherry", <- ours
272=======
273 "banana", <- theirs
274>>>>>>> add-fruit
275 "mango",
276 "orange",
277]
278----
279
280During a rebase, it can seem "upside down" because the "ours" commit is
281from the branch you're rebasing on (for instance `main` in `git rebase
282main`).
283
284These terms in Git all mean the same thing when dealing with a merge
285conflict:
286
287* "common ancestor" and "base". The files from this commit are "in stage 1".
288* "ours", "us", and `HEAD`. The files from this commit are "in stage 2".
289* "theirs", "them". The files from this commit are "in stage 3".
290
291If you're confused about what something like "deleted by us" means, it's
292often easiest to use some of the tools from
293<<tools,TOOLS FOR HANDLING MERGE CONFLICTS>> above to get more context.
294Finding the commit that deleted the file and seeing why is usually more
295helpful than trying to abstractly reason through what "us" means.
296
297[[automerge]]
298EXAMPLE OF USING `AUTO_MERGE`
299-----------------------------
300
301`git diff AUTO_MERGE` will show what changes you've made so far to
302resolve conflicts. `AUTO_MERGE` is a reference that Git creates during a
303merge. It contains the result of running the merge algorithm.
304
305For example, if we resolved the conflict by adding both "banana" and
306"cherry" in order, the diff would look like this:
307
308----
309 FRUITS = [
310 "apple",
311-<<<<<<< HEAD
312- "cherry",
313-=======
314 "banana",
315->>>>>>> add-fruit
316+ "cherry",
317 "mango",
318 "orange",
319 ]
320----
321
322[NOTE]
323`AUTO_MERGE` is only set if you're using the default Git merge algorithm.
324
325
326SEE ALSO
327--------
328
329linkgit:git-revert[1]
330linkgit:git-merge[1]
331linkgit:git-rebase[1]
332linkgit:git-cherry-pick[1]
333linkgit:git-pull[1]
334linkgit:git-diff[1]
335
336GIT
337---
338
339Part of the linkgit:git[1] suite