单分支 git 的冲突问题

解决办法:

一、本地修改没有 commit
  1. 希望保存本地改动并拉下最新服务器代码,手动merge

    • 1).保留服务器上的修改
      git stash
      -- 将当前的Git栈信息打印出来
      git stash list

      stash@{0} 就是刚才保存的标记
      
    • 2).暂存了本地修改之后,pull 远端代码
      git pull

    • 3).还原暂存的内容
      git stash pop stash@{0}
      系统自动合并修改的内容,但是其中有冲突,需要解决其中的冲突

    • 4).解决文件中冲突的的部分
      打开冲突的文件

      • Updated upstream 和==========之间的内容就是pull下来的内容
      • ==========和stashed changes之间的内容就是本地修改的内容

      这种情况,git也不知道哪行内容是需要的,所以要自行确定需要的内容,直接编辑冲突了的文件(test.txt),把冲突标记删掉,把冲突解决正确

    • (5) 删除stash
      -- 清除0编号的stash
      git stash drop stash@{0}
      -- 清除所有stash
      git stash clear

    • (6)提交aa/a.txt的修改
      git add aa/a.txt

  2. 如果希望服务器上版本完全覆盖本地修改,使用如下命令回退并更新 
    git reset --hard
    git pull

 
二、本地修改已经 commit ,push 的时候提示错误
mymac:~ root$ git push
To username@github.com:****/test.git
 ! [rejected]        master -> master (fetch first) 
 error: failed to push some refs to 'git@git.****.com:****/djangoProject.git'
 Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again.

提示我们要先执行 git pull, 执行 pull 后可能出现两种情况:
· [1] - 没有冲突,此时 git 会自动帮我们合并代码 ,并显示:

Merge made by the 'recursive' strategy.
 testDir/index.css | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

没有冲突并合并成功,只需再次 push 即可。

 

· [2] - 有冲突,此时 git 会提示冲突的文件:

CONFLICT (content): Merge conflict in testDir/index.css
Automatic merge failed; fix conflicts and then commit the result.

git 自动合并代码失败,因为此时同一处代码有不同的版本,需要用户手动merge 。可以根据提示的冲突文件信息去处理冲突的文件,需要看更详细的信息可以使用 git status

mymac:~ root$ git status
On branch master
Your branch and 'origin/master' have diverged,and have 1 and 7 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

    modified:   dssWebPortal/dssTools.py
    modified:   html/testDir/main.js
    modified:   html/testDir/netHelper.js
    modified:   html/testDir/alert.js
    modified:   html/testDir/index.html
    deleted:    html/testDir/data.json
    modified:   
    ....

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   testDir/index.css

Unmerged paths: both modified: 我们需要解决冲突的文件
Changes to be committed: modified: 在别处被修改了但是没有造成冲突的文件

手动修改冲突的文件,修改完后再用git status查看状态,直到both modified下面没有文件,这时就可以像平时一样使用 git 命令了:
git add .
git commit -m 'fix conflicts'
git push

 
三、 回滚代码到某次记录

这种操作会删掉本地指定记录后面的全部修改,使用前最好做好备份!!!

显示提交的log
git log
-- 也可以指定查看的个数
git log -3

回退命令:
-- 回退到上个版本
$ git reset --hard HEAD^
-- 回退到前3次提交之前,以此类推,回退到n次提交之前
$ git reset --hard HEAD~3
-- *退到/进到 指定commit的sha码,sha码通过上面的 log 命令获取
$ git reset --hard commit_id

强推到远程:
$ git push origin HEAD --force

上一篇:缓存的概念,如何实现缓存(Etag与last-modified优先级)


下一篇:前端HTTP 缓存简单了解