search

Home  >  Q&A  >  body text

如何解决git 2个地方的代码不一致.

初始情况是, a和b的代码库是完全相同的.

1, a修改了代码.然后成功push到 bitbucket
2, a做了更改是: 程序中的目录app/storage/sessions/刚开始没有加入到.gitignore中,所以a在.gitignore中加了这个目录.其它的是一些程序的代码.
3, b本身有代码,但没有修改.因为a更新了,所以b进行pull操作,可能是因为.gitignore冲突,git有提示到git reset --hard这一句,以为是会重置(reset这英语),就选了另一项(忘了选了什么了),然后进行平常的pull,commit,提示pull到bitbucket成功了,但看代码却没有看到a所做的更改.没在意,b就继续修改代码,push到远程.b这边提示这个.

b以为自已看懂了,选择了
$ git add --ignore-removal app/storage/sessions/

然后..上bitbucket一看,发现刚才a所做的更改全被删除了..

例如:像图片中这些是a加入的东西, 还有很多其他的代码,...现在全被删除掉了..

怎么恢复a成功push的更改,同时也保留b的这次更改呢? a的commit有160个文件改动,b的也有60多...除了a和b2人商讨根据代码一条条的对照思路进行恢复,有没有比较好的方案呢? 谢谢啦 ...


更新:

按照@nightire的回答, a这边进行了如下操作

# git add .
# git commit -m "big wrong! for last your git-commits"
On branch php-v0.0.1
Your branch is up-to-date with 'origin/php-v0.0.1'.

nothing to commit, working directory clean

所以,a修改了一个文件,加了一个空行,然后重新# git push --force orgin php-v0.0.1:php-v0.0.1

接着,b这边进行如下操作

$ git pull origin php-v0.0.1:php-v0.0.1
Password for 'https://Deloz@bitbucket.org':
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 4), reused 2 (delta 0)
Unpacking objects: 100% (6/6), done.
From https://bitbucket.org/deloz/xxxxx
 ! [rejected]        php-v0.0.1 -> php-v0.0.1  (non-fast-forward)
 + c344522...14bd65e php-v0.0.1 -> origin/php-v0.0.1  (forced update)

好像b又操作错了,这分支好像不对呀,然后b又来了一次不加参数了pull,如下

$ git pull
Password for 'https://Deloz@bitbucket.org':
Merge made by the 'recursive' strategy.
 app/database/migrations/xxxxxx.php | 1 +
 1 file changed, 1 insertion(+)

此时,b这边仍然没有a加入的文件和a之前(b出错前,a修改并push成功的代码)的修改.(这次a是加了一个空行,b这边也成功反映出来了.)...


再更新:

根据大家的看法和方案
a操作

# git log --pretty=oneline
14bd65ee1b14c1f06be98b22bc6cb5feb2039656 big wrong! for last your git-commits
3c96695b53f7d4e8dd78c9025044cea1d4cc0472 这个是正确的时候a所push的
00f0cdd6b8861d64a7930b823f552888edebdc11 xxxxxxxxxx
668fde30ad1d5d76b550f572ba6b64ae14a1e715 xxxxxx
8a80bb0eb9387db68119bbd7e0d15677ed7aa1f1 xxxxx
7ca8599be1ceb1dde6766315543209bf1bb761ce xxxxx^
bba184c4f324ed99b3fb580c103aba3505a688a5 xxxxxxxxx
458182037cfa54b8447b617c119d134ada250462 xxxxxxxxx
0029b46ac999f2c4d59c8cfb194ed1c2653fa1f4 xxxxxxn

b使用了git revert commit,代码没回滚到正确的commit去..应该是用错了..

$ git log --pretty=oneline
06661f005bc306796d2101633f68c2d01cd6bac3 re push (b push上来的)
3e34c6f8c177c67d487c7858720ac383e77738e2 Revert "Revert "big wrong! for last you  (b push上来的)
bcf07a9836aaaa9bdd505094b0859c13322f4519 Revert "big wrong! for last your git-co (b push上来的)
885c117de5ef67f430e0d3f8b0b1647a1cd14ee8 Merge branch 'php-v0.0.1' of https://bi (b push上来的)
14bd65ee1b14c1f06be98b22bc6cb5feb2039656 big wrong! for last your git-commits(a加空行后 push上来的)
c344522e2cba0e85e4b14393d596b992d47f1821 add column to table  (b push上来的)
3c96695b53f7d4e8dd78c9025044cea1d4cc0472 这个是正确的时候a所push的
00f0cdd6b8861d64a7930b823f552888edebdc11 xxxxxxxxxx
668fde30ad1d5d76b550f572ba6b64ae14a1e715 xxxxxx
8a80bb0eb9387db68119bbd7e0d15677ed7aa1f1 xxxxx
7ca8599be1ceb1dde6766315543209bf1bb761ce xxxxx^
bba184c4f324ed99b3fb580c103aba3505a688a5 xxxxxxxxx
458182037cfa54b8447b617c119d134ada250462 xxxxxxxxx
0029b46ac999f2c4d59c8cfb194ed1c2653fa1f4 xxxxxxn

接着
a执行

//备份一下当前a的分支
# git branch php_backup

//把a的分支回滚到 3c96695b53f7d4e8dd78c9025044cea1d4cc0472  因为上一次操作是 push --force了...
# git reset --hard 3c96695b53f7d4e8dd78c9025044cea1d4cc0472
HEAD is now at 3c96695 change some views

//删除远程的bitbucket上的分支
# git push origin :php-v0.0.1
Password for 'https://deloz@bitbucket.org':
To https://deloz@bitbucket.org/deloz/xxx.git
 - [deleted]         php-v0.0.1

//把正确的3c96695b53f7d4e8dd78c9025044cea1d4cc0472, push到远程bitbucket上
# git push origin php-v0.0.1
Password for 'https://deloz@bitbucket.org':
Counting objects: 256, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (220/220), done.
Writing objects: 100% (256/256), 785.09 KiB | 0 bytes/s, done.
Total 256 (delta 64), reused 158 (delta 26)
To https://deloz@bitbucket.org/deloz/xxx.git
 * [new branch]      php-v0.0.1 -> php-v0.0.1

然后, 根据@Guang Chen的方法, b操作

//备份一下B的分支
$ git branch php_bbbb
//把b的分支回滚到 3c96695b53f7d4e8dd78c9025044cea1d4cc0472
$ git reset --hard 3c96695b53f7d4e8dd78c9025044cea1d4cc0472
$ git fetch origin
$ git rebase origin/php-v0.0.1

//看一下log, b和a的一样的状态了.
$ git log --pretty=oneline
3c96695b53f7d4e8dd78c9025044cea1d4cc0472 这个是正确的时候a所push的
00f0cdd6b8861d64a7930b823f552888edebdc11 xxxxxxxxxx
668fde30ad1d5d76b550f572ba6b64ae14a1e715 xxxxxx
8a80bb0eb9387db68119bbd7e0d15677ed7aa1f1 xxxxx
7ca8599be1ceb1dde6766315543209bf1bb761ce xxxxx^
bba184c4f324ed99b3fb580c103aba3505a688a5 xxxxxxxxx
458182037cfa54b8447b617c119d134ada250462 xxxxxxxxx
0029b46ac999f2c4d59c8cfb194ed1c2653fa1f4 xxxxxxn


$ git status
On branch php-v0.0.1
Your branch is up-to-date with 'origin/php-v0.0.1'.

nothing to commit, working directory clean

剩下的就是分支php_bbb里边的不同代码...
使用git diff php-v0.0.1 php_bbbb >> 11.txt ,让b去修改了...
(/ □ )

黄舟黄舟2772 days ago1037

reply all(3)I'll reply

  • 阿神

    阿神2017-04-25 09:05:31

    Before pulling origin master, your b has a commit such as abcde

    # 在b机器上
    $ git reset abcde --hard 
    

    Back to the way it was before b pull
    And because your a has never been pulled, you can now think that you are back to your original appearance, except for the submission of a that adds a blank line

    That is to say, you should be in the same state now as you were in 2.
    At this time your b is also a clean workspace (no unstaged change)
    If you are convinced that the current state is indeed the same as in 2, then execute on b

    $ git fetch origin
    $ git rebase origin/php-v0.0.1 
    

    You will be reminded that there is a conflict, such as .gitignore
    Then open the .gitignore file, resolve the conflict, and execute again

    $ git add .gitignore #添加解决了冲突的文件
    $ git commit #直接:wq即可
    

    If something unexpected happens during the rebase process, please don’t do it casually, just give up git rebase --abort and then add more questions

    reply
    0
  • 怪我咯

    怪我咯2017-04-25 09:05:31

    I think it’s not that complicated...

    A, the latest local update is still OK, right? I mean after B's last push, A didn't pull, right? If this is the case, let A push --force directly to overwrite B's last push, and then B pull again. Don't make a mistake this time, it will be fine in the future.

    If A has already pulled the latest (that is, wrong) code, then reset it locally to the originally correct one, and then repeat the above.

    In a word, the advantage of distribution is that no matter what happens to the central repo, the commits you want can always be found.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-25 09:05:31

    Because I’m dizzy and not very used to the git command line, I won’t give a specific answer.

    I just want to say that in this situation, git pull这条命令就不应该用,fetch & rebaseis the right way

    In addition, the usage posture of Git is more problematic

    • There is a problem with two people developing on the same branch at the same time. In most cases, they should each play in their own feature branches. Their unfinished functions should be in their own branches. Pulling other people's code should always be done. Does not affect own branches.
    • There is a problem with a commit of more than 10 files. Or from a time perspective, there is a problem if the coding work that took more than half a day was not submitted

    Compared with solving the problem of messing up the existing code (this is a small matter that can always be solved), it is more important to popularize the correct git posture within the team

    Reference: A successful Git branching model http://www.juvenxu.com/2010/11/28/a-successful-git-branching-model/

    reply
    0
  • Cancelreply