Ubuntu下GitLab冲突解决步骤
首先确保本地已安装Git(未安装则运行sudo apt update && sudo apt install git
),然后将GitLab仓库克隆到本地:
git clone <repository-url>
进入项目目录:cd <project-name>
拉取远程仓库最新更改(避免本地代码过时导致冲突):
git fetch
为避免直接修改主分支(如main
/master
),建议创建新分支进行更改:
git checkout -b <new-branch-name>
(例如:git checkout -b feature/login
)
修改文件后,将更改添加到暂存区:git add .
(或指定具体文件,如git add src/login.py
)
提交更改并备注信息:git commit -m "Add login functionality"
将本地分支推送到远程仓库:git push origin <new-branch-name>
在GitLab项目页面,点击Merge Requests→New merge request,选择源分支(你的新分支)和目标分支(如main
),填写请求描述后提交。
若合并请求中提示存在冲突,可通过以下两种方式解决:
git clone <repository-url> --branch <new-branch-name> --single-branch
cd <project-name>
main
):git fetch origin main
git merge origin/main
git status
(冲突文件会标记为未合并)。<<<<<<< HEAD
当前分支(你的分支)的内容
=======
目标分支(如main)的内容
>>>>>>> origin/main
<<<<<<<
、=======
、>>>>>>>
)。git add <conflicted-file>
(如git add src/login.py
)git commit -m "Resolve merge conflicts"
git push origin <new-branch-name>
回到GitLab合并请求页面,若冲突已解决,Resolve conflicts按钮会消失,此时可点击Merge merge request将分支合并到目标分支。
git pull
),减少多人同时修改同一文件的情况。