-
Notifications
You must be signed in to change notification settings - Fork 1
Resolve conflicts
In my case, the two files were as follows
ben@GregoryHouse:~/tmp/testRepo (experimental *+|MERGING)$ git log --oneline master
6ac62bc Beautifying
c3df0e6 improvement
16ff837 added line in experimental branch
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README
ben@GregoryHouse:~/tmp/testRepo (experimental *+|MERGING)$ git log --oneline experimental
79f82eb it will clash
c3df0e6 improvement
16ff837 added line in experimental branch
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README- Let's find the differences done in each commit commit 79f82eb in the experimental branch:
ben@GregoryHouse:~/tmp/testRepo (experimental *+|MERGING)$ git diff c3df0e6 79f82eb
diff --git a/README b/README
index 7a8e4fe..3b7ac3a 100644
--- a/README
+++ b/README
@@ -7,4 +7,4 @@ Let's learn a bit of git. And in the mean time improve this docu
We can add some lines!
-Line added in experimental branch.
+Line modified in experimental branch.- and changes made in the master branch by commit 6ac62bc:
ben@GregoryHouse:~/tmp/testRepo (experimental *+|MERGING)$ git diff c3df0e6 6ac62bc
diff --git a/README b/README
index 7a8e4fe..6c6e859 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-otsGITTutorial
+OTSGITTutorial
==============
Short git tutorial repository.
@@ -7,4 +7,4 @@ Let's learn a bit of git. And in the mean time improve this docu
We can add some lines!
-Line added in experimental branch.
+Line added in the code.-
Only master has touched the first line and both branches touched the last line. That is where our conflict should come from.
-
Git has a much more convenient way of displaying what went wrong. Just edit the file that was tagged as conflicting:
OTSGITTutorial
==============
Short git tutorial repository.
Let's learn a bit of git. And in the mean time improve this document all the time.
We can add some lines!
<<<<<<< HEAD
Line modified in experimental branch.
=======
Line added in the code.
>>>>>>> master-
Automatically merge lines are replaced by their new values: line 1 has the higher case OTS letters
-
Problematic lines are surrounded by angle brackets, the local version appearing first and the version from the branch we try to merge into ours second. Just edit the file to suit your needs, stage it and commit it.
-
If you commit without a message, the proposed one looks like this:
Merge branch 'master' into experimental
Conflicts:
README
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
# .git/MERGE_HEAD
# and try again.
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch experimental
# Changes to be committed:
#
# modified: README
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# subdir/-
Run gitk again, and you will start to understand the git logo :-)
-
Here we merged the two branches, we could have used
git rebaseisntead, but its use is more complicated and you should wait until you are a bit more familiar with git before understanding what rewriting the history means for you and the other developers surrounding you. -
Let's apply this when trying to push to a repository (first we will add the bare repo to ben's repo):
git remote add sharedRepo ../testRepo-bare.git
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git fetch sharedRepo
From ../testRepo-bare
* [new branch] alternateHistory -> sharedRepo/alternateHistory
* [new branch] experimental -> sharedRepo/experimental
* [new branch] master -> sharedRepo/master
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
git remote add <name> <url>
and then push using the remote name
git push <name>
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git push sharedRepo
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 956 bytes, done.
Total 9 (delta 4), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
To ../testRepo-bare.git
c6c5566..6ac62bc master -> master
! [rejected] experimental -> experimental (non-fast-forward)
error: failed to push some refs to '../testRepo-bare.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.- The push has been rejected! Histories don't match between the two repositories. Indeed:
git log --oneline sharedRepo/experimental
c95e129 Add licence
c3df0e6 improvement
16ff837 added line in experimental branch
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git log --oneline experimental
b361dc9 Merge branch 'master' into experimental
79f82eb it will clash
6ac62bc Beautifying
c3df0e6 improvement
16ff837 added line in experimental branch
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README- The 2 branches diverged after commit c3df0e6.
- Here we must pull the upstream changes before being allowed to push.
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git pull sharedRepo experimental
From ../testRepo-bare
* branch experimental -> FETCH_HEAD
Merge made by the 'recursive' strategy.
LICENSE | 1 +
1 file changed, 1 insertion(+)
create mode 100644 LICENSE- This time the pull did not raise any conflict since no files were edited in both branches.
- Check the history with gitk, and git log if you want:
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git log --oneline
ba92d43 Merge branch 'experimental' of ../testRepo-bare into experimental
b361dc9 Merge branch 'master' into experimental
79f82eb it will clash
6ac62bc Beautifying
c95e129 Add licence
c3df0e6 improvement
16ff837 added line in experimental branch
c6c5566 Add .gitignore, add a line to README
4a9deba First commit: add README- and finally:
ben@GregoryHouse:~/tmp/testRepo (experimental)$ git push sharedRepo experimentalCounting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1013 bytes, done.
Total 8 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
To ../testRepo-bare.git
c95e129..ba92d43 experimental -> experimentalJump to: Git Workflows
Small personal advice:
- It is considered good policy (at least by my not-so-humble self) to avoid rewrite history as much as possible. The safest approach when you don't know what to do is to merge, rather than trying to make you code fit with others by hacking your history. If you committed the wrong thing, admitting you made a mistake, re-doing the changes by hand with a new commit fixing the errors, is always safer than forcing a push when the history doesn't fit your view
