天天看點

git問題:Your branch and 'origin/zq-dev' have diverged,and have 1 and 1 different commits each?如何解決

git:出現Your branch and 'origin/zq-dev' have diverged,and have 1 and 1 different commits each, respectively.

    (use "git pull" to merge the remote branch into yours)(說origin/zq-dev出現分叉)怎麼辦?

1.為防止zq-dev分支内容的丢失,先合并zq-dev到master:git merge zq-dev

2.使用git status檢視此時送出情況

   On branch master,Your branch is ahead of 'origin/zq-dev' by 4 commits.(use "git push" to publish your local commits)

3.使用 git log --graph --pretty=oneline --abbrev-commit指令

*   ae49907 (HEAD -> master) `Merge branch 'zq-dev'

|\

| * 9560204 (origin/zq-dev, zq-dev) modify file

* | 3efb899 2

* | 21af99d 1

* | a2a0994 add content 1

|/

* 80b980f add 9

* 2c3b24f '添加檔案'

發現本地master分支對應的遠端分支為origin/zq-dev,這就對了,(這是我先前使用此指令git push -u origin master :zq-dev 造成的)

4.然後嘗試git push

git push

fatal: The upstream branch of your current branch does not match

the name of your current branch.  To push to the upstream branch

on the remote, use

    git push origin HEAD:zq-dev

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

出現:說目前分支的關聯分支不匹您目前分支的名稱。?(原來我使用過這個指令在zq-dev分支上:git push --set --upstream origin zq-dev,也就是說

  本地zq-dev分支與遠端分支zq-dev相關聯了,但是遠端分支zq-dev是從本地master分支push上去的,隻是名稱不同而已)

5.删除本地zq-dev分支與遠端分支zq-dev

   git branch -D zq-dev

      Deleted branch zq-dev (was 9560204).

   git push origin -d zq-dev

       [deleted]         zq-dev

6.檢視此時所有分支

    git branch -a

      * master

      remotes/origin/master

7.再次然後嘗試: git push

fatal: The upstream branch of your current branch does not match

the name of your current branch.  To push to the upstream branch

on the remote, use

    git push origin HEAD:zq-dev

To push to the branch of the same name on the remote, use

    git push origin HEAD

To choose either option permanently, see push.default in 'git help config'.

8.根據提示使用指令:git push origin HEAD

To github.com:ZSGH123/git_test.git

 ! [rejected]        HEAD -> master (non-fast-forward)

error: failed to push some refs to '[email protected]:ZSGH/git_test.git'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

9.根據提示使用指令:git pull

Your configuration specifies to merge with the ref 'refs/heads/zq-dev'

from the remote, but no such ref was fetched(還是有錯誤)

10. git status

    On branch masterYour branch is based on 'origin/zq-dev', but the upstream is gone.(use "git branch --unset-upstream" to fixup)

11.根據提示使用指令:git branch --unset-upstream

12. 在git status檢視:沒有問題了

     On branch master

     nothing to commit, working tree clean

13.重新push master分支到遠端

 git push

    fatal: The current branch master has no upstream branch.To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

14.根據提示使用指令:git push --set-upstream origin master

To github.com:ZSGH123/git_test.git

 ! [rejected]        master -> master (non-fast-forward)

error: failed to push some refs to '[email protected]:ZSGH123/git_test.git'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

(還是出現錯誤)

15.根據提示使用指令:git pull

 git pull

There is no tracking information for the current branch.

Please specify which branch you want to merge with.

See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

16.根據提示使用指令:git branch --set-upstream-to=origin/master master

Branch 'master' set up to track remote branch 'master' from 'origin'.(提示已經關聯)

17 git status檢視

On branch master

Your branch and 'origin/master' have diverged,

and have 7 and 1 different commits each, respectively.

  (use "git pull" to merge the remote branch into yours)

  (說origin/master出現分叉)

18.使用指令 git rebase origin/master 解決

19.git status檢視

   On branch master

   Your branch is ahead of 'origin/master' by 7 commits.(use "git push" to publish your local commits)

20.根據提示使用指令: git push

Enumerating objects: 22, done.

Counting objects: 100% (22/22), done.

Delta compression using up to 4 threads

Compressing objects: 100% (21/21), done.

Writing objects: 100% (21/21), 4.39 KiB | 749.00 KiB/s, done.

Total 21 (delta 6), reused 0 (delta 0)

remote: Resolving deltas: 100% (6/6), done.

To github.com:ZSGH123/git_test.git

   fe9e915..ede8e31  master -> master(成功)