41.1. Create a conflict
In the following example you create a conflict during a merge operation.
The following steps create a merge conflict. It assumes that repo1 and repo2 have the same origin repository defined.
# switch to the first directory
cd ~/repo01
# make changes
echo "Change in the first repository" > mergeconflict.txt
# stage and commit
git add . && git commit -a -m "Will create conflict 1"
# switch to the second directory
cd ~/repo02
# make changes
touch mergeconflict.txt
echo "Change in the second repository" > mergeconflict.txt
# stage and commit
git add . && git commit -a -m "Will create conflict 2"
# push to the master repository
git push
# switch to the first directory
cd ~/repo01
# now try to push from the first directory
# try to push --> assuming that the same remote repository is used,
# you get an error message
git push
As this push would not result in a non-fast-format merge, you receive an error message similar to the following listing.
! [rejected] master -> master (fetch first)
error: failed to push some refs to '../remote-repository.git/'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
To solve this, you need to integrate the remote changes into your local repository. In the following listing the
git fetch
command gets the changes from the remote repository. The git merge
command tries to integrate it into your local repository.# get the changes via a fetch
git fetch origin
# now merge origin/master into the local master
# this creates a merge conflict in your
# local repository
git merge origin/master
This creates the conflict and a message similar to the following.
Auto-merging mergeconflict.txt
CONFLICT (add/add): Merge conflict in mergeconflict.txt
Automatic merge failed; fix conflicts and then commit the result.
The resulting conflict is displayed in ? and solved in ?
If you use the
git pull command it performs the "fetch and merge" or the "fetch and rebase" command together in one step. Whether merge or rebase is used depends on your Git configuration for the branch. See ? for the global configuration. |
41.2. Review the conflict in the file
Git marks the conflicts in the affected files. In the example from ? one file has a conflict and the file looks like the following listing.
<<<<<<< HEAD
Change in the first repository
=======
Change in the second repository
>>>>>>> b29196692f5ebfd10d8a9ca1911c8b08127c85f8
The text above the ======= signs is the conflicting change from your current branch and the text below is the conflicting change from the branch that you are merging in.
41.3. Solve a conflict in a file
In this example you resolve the conflict which was created in ? and apply the change to the Git repository.
To solve the merge conflict you edit the file manually. The following listing shows a possible result.
Change in the first and second repository
Afterwards add the affected file to the staging area and commit the result. This creates the merge commit. You can also push the integrated changes now to the remote repository.
# add the modified file
git add .
# creates the merge commit
git commit -m "Merge changes"
# push the changes to the remote repository
git push
Instead of using the
-m
option in the above example you can also use the git commit
command without this option. In this case the command opens your default editor with the default commit message about the merged conflicts. It is good practice to use this message.
Alternatively, you could use the
git mergetool command. git mergetool
starts a configurable merge tool that displays the changes in a split screen.
Some operating systems may come with a suitable merge tool already installed
or configured for Git.
|
0 comments:
Post a Comment