Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase causes feature branch to lose tracking information

Tags:

git

github

Whenever I'm rebasing in my feature branch from our 'master' branch, my feature branch seems to lose its tracking information. What's wrong with this workflow?

$ git clone repo_url
$ git co -b feature/my_work_task

Do a bunch of work here

$ 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

Make some additional changes in my feature branch.

$ git commit -am"more changes to update"
$ git push # pushing this to my remote repo

The push to the remote repo fails with the following error:

To [email protected]:/username/my-repo.git
 ! [rejected]        HEAD -> feature/my_work_task (non-fast-forward)
error: failed to push some refs to '[email protected]:/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.

I'll do a 'git pull' as suggested which then leads to:

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>

If I set the upstream information, followed by another attempt to 'git pull', I'll now have all sorts of file conflicts.

What's the best way to have a feature branch that keeps up with the changes from master?

like image 302
Willam Hill Avatar asked Jan 13 '23 01:01

Willam Hill


1 Answers

After you push a branch you can't rebase it.

Rebase 'replays' all your changes over the master branch (git rebase master). This re-writes your index. So, even though your local changes are all preserved and replayed over master (all the various commits regardless of how many times you've rebased) as soon you push the remote server sees a different index and thus your error.

So, once you push your branch you can't rebase master.

Instead:

  • git checkout -b b
  • git commit -am "stuff"
  • git push origin b
  • git checkout master
  • git pull origin master
  • git checkout b
  • git merge master
like image 75
cbrulak Avatar answered Jan 19 '23 10:01

cbrulak