Merge conflicts
So far we have only made changes in separate files. However, it can happen that you have made changes to the same file with 2 people, and that a conflict occurs.
In the project
folder, edit the text in hello.txt
and push it to the remote repository:
project_1 $ echo "edit in project" > hello.txtproject_1 $ git add .project_1 $ git commit -m "changed hello.txt"[main f1c4e08] changed hello.txt 1 file changed, 1 insertion(+), 1 deletion(-)
project_1 $ git pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 12 threadsCompressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.Total 3 (delta 1), reused 0 (delta 0)remote: Resolving deltas: 100% (1/1), completed with 1 local object.To git@github.com:demouser/hellogit.git 8f2400d..f1c4e08 main -> main
Also edit the text of hello.txt in the project2 folder
project_2 $ echo "edit in project2" > hello.txtproject_2 $ git add .project_2 $ git commit -m "changed hello.txt in project2"[main 45b7ea9] chenged hello.txt in project2 1 file changed, 1 insertion(+), 1 deletion(-)
project_2 $ git pushTo git@github.com:demouser/hellogit ! [rejected] main -> main (fetch first)error: failed to push some refs to 'git@github.com:demouser/hellogit'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
We first pull before we can push
project_2 $ git pull --rebaseremote: Enumerating objects: 5, done.remote: Counting objects: 100% (5/5), done.remote: Compressing objects: 100% (1/1), done.remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0Unpacking objects: 100% (3/3), done.From git@github.com:demouser/hellogit 8f2400d..f1c4e08 main -> origin/mainFirst, rewinding head to replay your work on top of it...Applying: chenged hello.txt in project2Using index info to reconstruct a base tree...M hello.txtFalling back to patching base and 3-way merge...Auto-merging hello.txtCONFLICT (content): Merge conflict in hello.txterror: Failed to merge in the changes.Patch failed at 0001 chenged hello.txt in project2hint: Use 'git am --show-current-patch' to see the failed patchResolve all conflicts manually, mark them as resolved with"git add/rm <conflicted_files>", then run "git rebase --continue".You can instead skip this commit: run "git rebase --skip". To abort and get back to the state before "git rebase", run "git rebase --abort".
We get a merge conflict, because we changed the same file in both repositories. Now, we need to resolve this conflict before the rebase can continue.
Via git status
you can request a list of the merge conflicts:
project_2 $ git statusrebase in progress; onto f1c4e08You are currently rebasing branch 'main' on 'f1c4e08'. (fix conflicts and then run "git rebase --continue") (use "git rebase --skip" to skip this patch) (use "git rebase --abort" to check out the original branch)
Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution)
both modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")
You will be notified that you are rebasing. At “Unmerged paths” you see the files that are in conflict. You must first resolve the conflict by editing the file before continuing with the rebase.
Open the txt-file in a text editor. You notice that the content from project
and project2
is present:
<<<<<<< HEADedit in project=======edit in project2>>>>>>> changed hello.txt in project2
Adjust the text to:
edit in projectand edit in project2
Add this to the rebase action and use the ‘continue’ flag
project_2 $ git add hello.txtproject_2 $ git rebase --continueApplying: changed hello.txt in project2
Now when you execute git status, you will see that you are on the main branch again, and you are 1 commit ahead of the remote:
project_2 $ git statusOn branch mainYour branch is ahead of 'origin/main' by 1 commit.nothing to commit (working directory clean)
Now you can push again to the remote
project_2 $ git pushCounting objects: 5, done.Delta compression using up to 8 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 326 bytes, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:demouser/hellogit 7f3b200..920c81f main -> main
Execute git pull
in the other folder so that both are synced again.
Visually resolve conflicts
If you like a more visual way, you can resolve merge conflicts in VS Code