git提交代码,解决冲突

项目现在是多人协同开发,git代码管理,容易有冲突

先来一个git pull,发现不能拉取代码,错误信息

emote: Counting objects: 154, done.
remote: Compressing objects: 100% (95/95), done.
remote: Total 154 (delta 91), reused 89 (delta 54)
Receiving objects: 100% (154/154), 24.39 KiB | 4.07 MiB/s, done.
Resolving deltas: 100% (91/91), completed with 9 local objects.
From http://ip.ip.ip.ip/***/***
   f587290..f9d88fc  develop    -> origin/develop
Updating f587290..f9d88fc
error: Your local changes to the following files would be overwritten by merge:
        src/api/host.js
        src/api/index.js
Please commit your changes or stash them before you merge.
Aborting

肯定不会陌生的了

那我就按照它的提示,先commit一下吧!git commit -m "test"

然后又是预期的一样,不能commit,错误信息

On branch develop
Your branch is behind 'origin/develop' by 23 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes not staged for commit:
        modified:   src/api/host.js
        modified:   src/api/index.js

no changes added to commit

它现在又叫我git pull了,有点晕晕的~

我看到了个词 stage,有点眼熟,运行命令看看帮助 git stage -help

好像没有我想要的

然后不小心打成了 git stash 命令

我写的代码不见了!心有点慌!怎么办呢?

查了下万能的百度,说是把我的文件保存起来了,想恢复用 git stash apply 命令。

我就放下心了,试了下,真的可以恢复!好神奇。

git stash 之后没提交的代码就不见了,去了另一个地方,这时我就可以 git pull 了!随心所欲拉代码!

拉完代码再git stash apply 把代码恢复好,这时需要解决冲突

Auto-merging src/api/index.js
CONFLICT (content): Merge conflict in src/api/index.js
Auto-merging src/api/host.js
CONFLICT (content): Merge conflict in src/api/host.js

这时运行git commit -m "test" 还是报错

U       src/api/host.js
U       src/api/index.js
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

根据它的提示,解决完冲突,要使用git add file

那我就用了两行命令

git add src/api/host.js

git add src/api/index.js

不太明白这两行有什么用,加到哪里去啊?

不管了,运行git commit -m "test"

[develop cabfc86] test
 2 files changed, 52 insertions(+), 1 deletion(-)

大功告成!成功提交了代码,开心~

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在团队开发中,当多人对相同的文件进行编辑时,可能会导致代码冲突的问题。解决代码冲突的方法有多种。一种常见的方法是使用工具或git命令来对比不同分支代码的差异,然后将不同分支中的有效代码保留下来,合并成最终的代码,再提交到仓库中。\[1\] 具体的解决步骤如下: 1. 首先,保存本地工作区修改的代码,根据业务需求进行相应的修改,并保存相关代码。\[2\] 2. 然后,将修改后的代码提交到本地服务器仓库,进行commit动作。注意,这一步是提交到本地服务器仓库,而不是远程仓库。\[2\] 3. 接下来,拉取远程服务器仓库的代码。在拉取过程中,可能会出现冲突,这时文件会显示冲突的地方。\[2\] 4. 当出现冲突时,需要先备份工作区文件,将文件恢复与远程服务器一致,然后才能完成pull操作。完成pull后,再将本地修改添加进去,进行commit和push操作。\[3\] 需要注意的是,当远程仓库文件与工作区文件冲突无法完成pull时,需要先备份工作区文件,恢复文件与远程服务器一致,才能完成pull操作。然后再将本地修改添加进去,进行commit和push操作。这种操作在修改文件较多时可能会比较麻烦。\[3\] #### 引用[.reference_title] - *1* [传智教育 | Git代码冲突-不同分支之间的代码冲突](https://blog.csdn.net/cz_00001/article/details/125409249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [GIT代码冲突解决方案,两种方案详细教程](https://blog.csdn.net/yqj234/article/details/120309698)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值