首先介绍一下背景。

如果有一个工程A,开始时test.txt 的内容如下

chenfool
hello world

作者通过 git clone 的方式,将这个项目download 到本地。

此时,作者手贱,直接在web 端,将test.txt 修改成为

chenfool

hello world

过了一段时间后,作者突然想起要修改本地的test.txt文件,在本地 clone 版本中,对test.txt 文件修改如下

chenfool

hello world

然后想将本地的test.txt 提交到远程中,在push 时,会报大概如下的错

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.

执行 git status 查看,它就告诉你有一个commit 还没有提交呢

On branch master
Your branch is ahead of 'origin/master' by commit.
(use "git push" to publish your local commits) nothing to commit, working tree clean

此时,你可以尝试一下使用 git pull ,将远程代码和本地代码强行 merge 一下,如果能够正常merge ,则是其他问题导致了错误,如果是冲突,就可以往下看了

执行 git pull 时,会报告错误 (有一些敏感信息被作者抹除了,可能和读者真实执行有出入)

remote: Counting objects: , done.
remote: Compressing objects: % (/), done.
remote: Total (delta ), reused (delta )
Unpacking objects: % (/), done.
From http://192.168.1.1/chenfool/
4c7cd0c..5b8e16f master -> origin/master
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

此时再执行 git status 命令,就可以看到真正的错误了

On branch master
Your branch and 'origin/master' have diverged,
and have and different commits each, respectively.
(use "git pull" to merge the remote branch into yours) You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge) Unmerged paths:
(use "git add <file>..." to mark resolution) both modified: test.txt

打印的信息说得很明白,就是test.txt 这个文件,被多个人修改了,并且无法实现智能合并,需要人工干预了。

我们可以怎么做呢?解决的方式简单粗暴,就是对比local 和remote 两端的差异,然后选择自己想要的内容,然后提交。

这里作者交代一下,因为作者的环境为mac,所以比对文件差异使用 Kaleidoscope 可视化工具。

Kaleidoscope 软件读者可以在这里了解更多集成的信息。

使用 git mergetool test.txt 命令,git 会自动调用 Kaleidoscope 软件,让读者自由对比其中差异,以及修改自己想要提交的内容。

读者可以发现,左手边是local 文件的内容,右手边是remote 文件的内容,而中间则是让读者自由编辑的部分。整个界面非常的友好。

读者在中间部分正确修改好内容后,直接保存退出。

此时,在local 本地,会发现test.txt 文件变成了两个,一个名为 test.txt ,一个名为 test.txt.orig。

test.txt 是修改后的文件,也是我们即将要提交的文件。

test.txt.orig 文件则是记录了原来 local 和 remote 文件差异的文件。实际上到这个步骤,test.txt.orig 已经没有啥用处,读者们可以自行将其删除。

开始提交了,读者会发现实际上就是重新add 和commit 了一次,但是它确实能够解决提交冲突的问题。

git add test.txt
git commit -m "for test"
git push origin master
  • 题外话

如果读者只是非常渴望可以将本地的文件覆盖 remote 的文件,可以通过一下方式来进行操作。

在 git push origin master 失败后,首先备份一下本地即将提交的文件,未来要用。

然后 git pull ,将remote 的内容download 本地。

此时,用户就可以通过 git status 了解是哪些文件发生了冲突。

然后再将刚才备份的文件直接覆盖本地刚刚 git pull 后的文件。

再依次执行 git add 、 git commit 和 git push origin master,就可以简单粗暴地将 local 文件覆盖 remote 文件。

git 提交解决本地与远程冲突的更多相关文章

  1. git 提交解决冲突(转载)

    转载 git 提交解决冲突 http://www.cnblogs.com/qinbb/p/5972308.html   一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. ...

  2. Git提交到多个远程仓库(多看两个文档)

    Git提交到多个远程仓库(多看两个文档) 一.总结 一句话总结: 二. Git提交到多个远程仓库(多看两个文档) 有两种做法,先看第一种 一.通过命令行进行操作 例如我有下面两个仓库: Mybatis ...

  3. git 强推本地分支覆盖远程分支

    git 强推本地分支覆盖远程分支git push origin 分支名 --force

  4. git 提交解决冲突

    一:git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突. error: You have not concluded your merge (MERGE_HEAD exists) ...

  5. git上传(本地和远程有冲突时)

    一. 冲突的产生:在上次git同步(上传)之后,本地和远程均有更改 二. 处理 1. 丢弃本地,采用远程: git checkout 冲突文件及其路径 如: git checkout bzrobot_ ...

  6. 【原】git如何删除本地和远程的仓库

    今天操作git时遇到一个小问题:如何删除本地和远程的仓库,在网上巴拉一番解决了这个问题. 方法1: $rm testfile$git add -u $git commit -m "delet ...

  7. Git常用命令+本地连接远程仓库

    一.git命令整理 git config --global user.email "邮箱名":绑定GitHub邮箱 git config --global user.name &q ...

  8. Git 解决本地远端版本冲突

    简单粗暴.... git push -u origin master -f

  9. git branch --set-upstream 本地关联远程分支

    最近使用git pull的时候多次碰见下面的情况: There is no tracking information for the current branch.Please specify whi ...

随机推荐

  1. centos 静态拨号

    本人系统centos6.5:虚拟机太丑,固ssh. centos的与联网相关的配置文件在 $ /etc/sysconfig/network-scripts DHCP方式-联网 打开文件 $ vim / ...

  2. python中的排序函数

    1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1)  cmp参数 cmp接受一 ...

  3. 【leetcode刷题笔记】Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  4. OpenCV - Android Studio 中集成Opencv环境(不包含opencv_contrib部分)

    OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,支持的运行环境也是非常的多,这篇文章主要讲的是Android环境集成OpenCV(IDE是Android Studio,我想Eclip ...

  5. Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)

    题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...

  6. Gym - 101196:F Removal Game(区间DP)

    题意:一个环状数组,给定可以删去一个数,代价的相邻两个数的gcd,求最小代价. 思路:区间DP即可,dp[i][j]表示[i,j]区间只剩下i和j时的最小代价,那么dp[i][j]=min  dp[i ...

  7. ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)

    Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal ...

  8. bzoj 4537: [Hnoi2016]最小公倍数 分块+并查集

    题目大意: 给定一张n个点m条边的无向图,每条边有两种权.每次询问某两个点之间是否存在一条路径上的边的两种权的最大值分别等于给定值. n,q <= 50000. m <= 100000 题 ...

  9. Poj 1061 青蛙的约会(扩展欧几里得解线性同余式)

    一.Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  10. kubeadm 搭建 K8S集群

    kubeadm是K8s官方推荐的快速搭建K8s集群的方法. 环境: Ubuntu 16.04 1 安装docker Install Docker from Ubuntu’s repositories: ...