debian-mirror-gitlab/doc/topics/git/merge_conflicts.md

70 lines
1.8 KiB
Markdown
Raw Normal View History

2021-06-08 01:23:25 +05:30
---
2021-09-04 01:27:46 +05:30
stage: Create
group: Source Code
2021-06-08 01:23:25 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
comments: false
---
2021-09-04 01:27:46 +05:30
# Merge conflicts **(FREE)**
2021-06-08 01:23:25 +05:30
- Happen often
- Learning to fix conflicts is hard
- Practice makes perfect
- Force push after fixing conflicts. Be careful!
## Merge conflicts sample workflow
2022-07-16 23:28:13 +05:30
1. Check out a new branch and edit `conflicts.rb`. Add 'Line4' and 'Line5'.
2021-06-08 01:23:25 +05:30
1. Commit and push.
2022-07-16 23:28:13 +05:30
1. Check out `main` and edit `conflicts.rb`. Add 'Line6' and 'Line7' below 'Line3'.
1. Commit and push to `main``.
2021-06-08 01:23:25 +05:30
1. Create a merge request and watch it fail.
2022-07-16 23:28:13 +05:30
1. Rebase our new branch with `main`.
2021-06-08 01:23:25 +05:30
1. Fix conflicts on the `conflicts.rb` file.
1. Stage the file and continue rebasing.
1. Force push the changes.
2022-04-04 11:22:00 +05:30
1. Finally continue with the merge request.
2021-06-08 01:23:25 +05:30
```shell
git checkout -b conflicts_branch
# vi conflicts.rb
# Add 'Line4' and 'Line5'
git commit -am "add line4 and line5"
git push origin conflicts_branch
2022-07-16 23:28:13 +05:30
git checkout main
2021-06-08 01:23:25 +05:30
# vi conflicts.rb
# Add 'Line6' and 'Line7'
git commit -am "add line6 and line7"
2022-07-16 23:28:13 +05:30
git push origin main
2021-06-08 01:23:25 +05:30
```
Create a merge request on the GitLab web UI, and a conflict warning displays.
```shell
git checkout conflicts_branch
git fetch
2022-07-16 23:28:13 +05:30
git rebase main
2021-06-08 01:23:25 +05:30
# Fix conflicts by editing the files.
git add conflicts.rb
# No need to commit this file
git rebase --continue
# Remember that we have rewritten our commit history so we
# need to force push so that our remote branch is restructured
git push origin conflicts_branch -f
```
## Note
- When to use `git merge` and when to use `git rebase`
2022-07-16 23:28:13 +05:30
- Rebase when updating your branch with `main`
- Merge when bringing changes from feature to `main`
2021-06-08 01:23:25 +05:30
- Reference: <https://www.atlassian.com/git/tutorials/merging-vs-rebasing>