git rebase导致功能分支丢失跟踪信息

Wil*_*ill 4 git github

每当我在我的"主"分支的功能分支中进行变基础时,我的功能分支似乎丢失了它的跟踪信息.这个工作流程出了什么问题?

$ git clone repo_url
$ git co -b feature/my_work_task
Run Code Online (Sandbox Code Playgroud)

在这里做一堆工作

$ git commit -am"Commit my work"
$ git push # push the branch upstream
$ git checkout master
$ git pull # get whatever changes that have been put into master
$ git co feature/my_work_task
$ git rebase master # everything seems to be good
Run Code Online (Sandbox Code Playgroud)

在我的功能分支中进行一些其他更改.

$ git commit -am"more changes to update"
$ git push # pushing this to my remote repo
Run Code Online (Sandbox Code Playgroud)

推送到远程仓库失败,出现以下错误:

To git@github.com:/username/my-repo.git
 ! [rejected]        HEAD -> feature/my_work_task (non-fast-forward)
error: failed to push some refs to 'git@github.com:/username/my-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

我会按照建议做一个'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 feature/my_work_task origin/<branch>
Run Code Online (Sandbox Code Playgroud)

如果我设置上游信息,然后再尝试'git pull',我现在会遇到各种各样的文件冲突.

有一个功能分支能够跟上master的变化的最佳方法是什么?

cbr*_*lak 9

推动分支后,您无法对其进行修改.

Rebase通过主分支(git rebase master)重放"所有更改" .这会重写您的索引.因此,即使你的本地更改都被保留并通过master重放(所有各种提交,无论你多少次重新设置),只要你推送远程服务器就会看到不同的索引,从而看到你的错误.

所以,一旦你推动你的分支,你就无法改变主人.

代替:

  • git checkout -b b
  • git commit -am "stuff"
  • git push origin b
  • git checkout master
  • git pull origin master
  • git checkout b
  • git 合并 master