V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
337136897
V2EX  ›  问与答

git 提交代码时发现把同事的代码删了, git 却没有提示 merging 是怎么回事?

  •  
  •   337136897 · 2020-04-17 22:43:33 +08:00 · 1343 次点击
    这是一个创建于 1717 天前的主题,其中的信息可能已经有所发展或是发生改变。

    改了 4 个文件,然后 一顿操作如下: 一:先 pull 代码 ,提示有人提交过代码

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git pull
    remote: Enumerating objects: 852, done.
    remote: Counting objects: 100% (852/852), done.
    remote: Compressing objects: 100% (340/340), done.
    remote: Total 852 (delta 357), reused 748 (delta 312)
    Receiving objects: 100% (852/852), 207.41 KiB | 131.00 KiB/s, done.
    Resolving deltas: 100% (357/357), completed with 11 local objects.
    From https://gitlab.charge-spot.com/backend-group/chargespot-business-api
       7bb0b5eb..ae21cc9d  feature-init20200401 -> origin/feature-init20200401
    error: Your local changes to the following files would be overwritten by merge:
            business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/BatteryServiceImpl.java
    Please commit your changes or stash them before you merge.
    Aborting
    Updating 7bb0b5eb..ae21cc9d
    
    

    二: add 和 看一下 status

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git status
    On branch feature-init20200401
    Your branch is behind 'origin/feature-init20200401' by 61 commits, and can be fast-forwarded.
      (use "git pull" to update your local branch)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/BatteryServiceImpl.java
    

    三:commit

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git commit -m '去掉 log'
    [feature-init20200401 c3c59ecc] 去掉 log
     1 file changed, 2 deletions(-)
    
    

    四:commit 后再 pull 一下

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git pull
    Auto-merging business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/BatteryServiceImpl.java
    Merge made by the 'recursive' strategy.
     .../device/client/DeviceOwnerRelClient.java        |   9 ++
     .../fallback/DeviceOwnerRelClientFallBack.java     |   5 +
     .../com/chargespot/device/dto/DeviceModelDTO.java  |  18 +++
     .../chargespot/device/vo/ExportDeviceListVO.java   |   2 +-
     business-device/business-device-biz/pom.xml        |   2 +-
     .../device/mapper/DeviceOwnerRelMapper.java        |  11 ++
     .../device/service/DeviceOwnerRelService.java      |   8 ++
     .../device/service/impl/BatteryServiceImpl.java    |   4 +-
     .../service/impl/DeviceGroupServiceImpl.java       |   4 +-
     .../service/impl/DeviceOperationsServiceImpl.java  |   2 +-
     .../service/impl/DeviceOwnerRelServiceImpl.java    |  20 ++-
     .../device/service/impl/DeviceServiceImpl.java     |   8 +-
     .../device/web/DeviceOwnerRelController.java       |   5 +
     .../mapper/extend/DeviceOwnerRelMapperExtend.xml   |  18 ++-
     .../chargespot/move/model/BusinessDeviceRelVO.java |   3 +
     .../move/service/impl/CabinetGroupServiceImpl.java |   2 +-
     .../service/impl/MemberCouponsServiceImpl.java     |  55 ++++----
     .../move/service/impl/MoveServiceImpl.java         |  61 +++++----
     .../mapper/extend/CabinetMapperExtend.xml          |   3 +-
     .../mapper/extend/MemberCouponsMapperExtend.xml    |   5 +-
     .../order/service/impl/OrderLendServiceImpl.java   |   1 +
     .../mapper/extend/OrderOwnerMapperExtend.xml       |   4 +-
     .../proxy/client/ProxyAccountClient.java           |   2 +
     .../fallback/ProxyAccountClientFailback.java       |   5 +
     .../chargespot/proxy/model/GetDrawAmountDTO.java   |   6 +
     .../chargespot/proxy/config/param/DramParam.java   |   5 +
     .../chargespot/proxy/handler/MqMessageHandler.java |  13 +-
     .../proxy/mapper/ProxyDrawOrderMapper.java         |   8 ++
     .../proxy/service/ProxyAccountService.java         |   7 ++
     .../proxy/service/ProxyOrderBillService.java       |   3 +-
     .../service/impl/ProxyAccountServiceImpl.java      |  86 ++++++++++++-
     .../service/impl/ProxyDrawOrderServiceImpl.java    |  48 ++++---
     .../proxy/service/impl/ProxyIndexServiceImpl.java  | 139 +++++++--------------
     .../service/impl/ProxyOrderBillServiceImpl.java    |   6 +-
     .../proxy/web/ProxyAccountController.java          |   7 ++
     .../mapper/extend/ProxyDrawOrderMapperExtend.xml   |  38 +++++-
     .../mapper/extend/ProxyOrderBillMapperExtend.xml   |   8 +-
     .../business/job/BusinessDeviceEventJob.java       |  40 +++---
     .../impl/BusinessmenCouponsMemberServiceImpl.java  |  13 --
     .../impl/BusinessmenOwnerRelServiceImpl.java       |   4 +-
     .../service/impl/BusinessmenServiceImpl.java       | 111 ++++++++--------
     41 files changed, 506 insertions(+), 293 deletions(-)
     create mode 100644 business-device/business-device-base/src/main/java/com/chargespot/device/dto/DeviceModelDTO.java
    
    

    五:再 add 并且看一下 status(在这里提示修改是正确的,4 个文件)

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git add .
    gi
    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git status
    On branch feature-init20200401
    Your branch is ahead of 'origin/feature-init20200401' by 2 commits.
      (use "git push" to publish your local commits)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/BlackListBatteryServiceImpl.java
            modified:   business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/DeviceOperationsServiceImpl.java
            modified:   business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/DeviceOwnerRelServiceImpl.java
            modified:   business-device/business-device-biz/src/main/java/com/chargespot/device/service/impl/DevicePreWarningDataServiceImpl.java
    
    
    

    最后再 commit 和 push

    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git commit -m '去 log'
    [feature-init20200401 9e87e2c6] 去 log
     4 files changed, 6 insertions(+), 20 deletions(-)
    
    qiann@LAPTOP-DHDFP0PP MINGW64 /c/project/chargespot-business-api (feature-init20200401)
    $ git push origin
    Enumerating objects: 66, done.
    Counting objects: 100% (66/66), done.
    Delta compression using up to 12 threads
    Compressing objects: 100% (33/33), done.
    Writing objects: 100% (42/42), 20.74 KiB | 1.22 MiB/s, done.
    Total 42 (delta 20), reused 0 (delta 0)
    remote:
    remote: To create a merge request for feature-init20200401, visit:
    remote:   https://gitlab.charge-spot.com/backend-group/chargespot-business-api/merge_requests/new?merge_request%5Bsource_branch%5D=feature-init20200401
    remote:
    To https://gitlab.charge-spot.com/backend-group/chargespot-business-api.git
       ae21cc9d..9e87e2c6  feature-init20200401 -> feature-init20200401
    
    
    

    ,然后看了下,把同事代码删了

    1

    是我操作不对吗?为什么会这样,大哥们

    2 条回复    2020-04-20 02:12:36 +08:00
    msg7086
        1
    msg7086  
       2020-04-18 08:53:23 +08:00   ❤️ 3
    新手入门我总是建议请老老实实从一个优秀的 GUI 开始。等你知道了 working directory / stash / stage / commit / merge / merge back / rebase 了以后,再要想比如说装个 B,再考虑纯 CLI 的情况。
    比如 git pull 这样的命令只能处理最简单的拉取情况。如果你要拉取有冲突的 commit,情况会复杂得多,光一个 add 一个 commit 是远远不够的,需要首先根据场景看要做 rebase 还是 merge,然后根据具体操作去做 3-way conflict resolve 。

    你现在的情况是 working directory 有更改,没进 stage 也没上 commit,更没 stash,于是产生了 local change conflict 。然后你做了 add 和 commit,这时候如果你有图形界面的话你可以看到状态是这样的。

    origin/feature-init20200401 -> 7bb0b5eb --- 60 多个提交 ---> ae21cc9d
    feature-init20200401 -> 7bb0b5eb -> c3c59ecc

    然后你又做了 pull --merge,和一个 commit,变成了

    feature-init20200401 -> 7bb0b5eb -> c3c59ecc -(merge ae21cc9d) -> 9e87e2c6

    先不论结果怎么样,中途这条分支叉出来叉进来再叉回去,会让人无法看清每一个提交的用处和每一处修改的位置,万一提交后出了问题,也很难去单独隔离出某一段提交然后回滚。比如你这里,一处修改分了两次提交,一次在 merge point 之前,一次在之后,就很难去隔离测试。

    比较好的做法是,先 stash,然后签出 ae21cc9d,然后 stash pop 并 resolve conflict,然后再做一个单独的「去 log 」提交。
    另一种做法是,先 commit,然后做 pull rebase,在 rebase 过程中 resolve conflict,然后把 rebase 过的 commit 给 push 上去。
    两种做法的结果是完全相同的。

    如果你司没有针对 git 的学习过程的话,建议大家聚在一起好好学一下。瞎基尔用 git 是对 git 强大能力的一种浪费。正确使用 git 会大幅提高源代码管理效率的。

    至于你说把同事代码删了是怎么回事,这个也只需要开个 GUI 工具,往上回溯一下看看每个提交的 diff 就知道怎么回事了。无非是 merge 的时候出了问题吧。
    337136897
        2
    337136897  
    OP
       2020-04-20 02:12:36 +08:00 via iPhone
    @msg7086 感谢大佬的回复,好详细,学习了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1511 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 16:45 · PVG 00:45 · LAX 08:45 · JFK 11:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.