Git example, creating a conflict between branches then merging

The cat and ls commands are used in several places just to show what the directory and file contents currently look like in the current branch.

(1) set up the repository
501> mkdir gitExpt 502> cd gitExpt 503> git init-db Initialized empty Git repository in /home/faculty/wesselsd/expt/gitExpt/.git/
(2) create a file, add it to the repository, and commit
506> mkdir src 507> vi src/test.h 508> cat src/test.h master's test file 510> git add src/test.h 511> git commit -m "added a master version of test.h" [master (root-commit) 98427c4] added a master version of test.h 1 file changed, 3 insertions(+) create mode 100644 src/test.h
(3) create a branch, check it out, add a file, and commit
512> git branch testMerge 513> git checkout testMerge Switched to branch 'testMerge' 514> ls ./ ../ .git/ src/ 515> ls src ./ ../ test.h 516> vi src/test.c 517> git add src/test.c 518> git commit -m "added the test source file" [testMerge feb8544] added the test source file 1 file changed, 7 insertions(+) create mode 100644 src/test.c 520> cat src/test.c #include "test.h" int main() { return 0; }
(4) go back to the master branch and create a different version of the same file, add it, and commit
521> git checkout master Switched to branch 'master' 522> ls ./ ../ .git/ src/ 523> ls src ./ ../ test.h 524> vi src/test.c 525> cat src/test.c // needed a source code file 526> git add src/test.c 527> git commit -m "added the source test file we needed" 1 file changed, 1 insertion(+) create mode 100644 src/test.c
(5) go back to the branch and try a merge
528> git checkout testMerge Switched to branch 'testMerge' 529> cat src/test.c #include "test.h" int main() { return 0; } 530> git merge master Auto-merging src/test.c CONFLICT (add/add): Merge conflict in src/test.c Automatic merge failed; fix conflicts and then commit the result.
(6) edit the file: you'll see git has highlighted the differences between the conflicting file versions, using <<<<<<< HEAD ... what the branch version looks like... ======= ... what the master version looks like... >>>>>>> master remove what git added, and keep whatever content you want, save the file
531> cat src/test.c <<<<<<< HEAD #include "test.h" int main() { return 0; } ======= // needed a source code file >>>>>>> master 532> vi src/test.c 533> cat src/test.c // needed a source code file #include "test.h" int main() { return 0; }
(7) MAKE SURE YOU ADD THE EDITED FILE, then commit
534> git add src/test.c 535> git commit -m "included all contents of test.c from master and branch" [testMerge d79b096] included all contents of test.c from master and branch 536> git status # On branch testMerge nothing to commit (working directory clean)
(8) The conflicts have been resolved and the content of both branches has been merged in the branch, Let's go back to the master and see that it is still unchanged
537> git checkout master Switched to branch 'master' 538> cat src/test.c // needed a source code file 539> git status # On branch master nothing to commit (working directory clean)

IMPORTANT NOTES:
Having detected a conflict, or conflicts, git won't let you do much of anything until you resolve them.

That means that in the branch that attempted the merge (or pull) you'll need to

  1. edit all the appropriate files (i.e. edit them AFTER the conflict was highlighted),
  2. save the newly-revised version(s),
  3. add them (it won't be happy until you have edited, saved, and added them),
and then it should complete the operation.