-1

How can i push my Master branch to my old branch?

i am trying to use:

git push origin master:my_branch but i keep getting an error of

 ! [rejected]        master -> my_branch (non-fast-forward)
error: failed to push some refs to
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.

i am still new in using Git. i am trying to use rebase, but i am having a hard time on understanding it. can anyone help me. any help would be really appreciated.

Janessa Bautista
  • 294
  • 1
  • 13
  • do you want to merge changes in your branch to master branch? or the master branch changes to your branch? – Rahul R. Feb 14 '20 at 05:54
  • i want to push master to my branch, so that the outdated branch will be updated already. – Janessa Bautista Feb 14 '20 at 05:55
  • That's a very strange way to update a branch. The normal procedure would be to merge (or rebase) master into "your branch" locally, and then push your branch. – Christian Fosli Feb 14 '20 at 06:01
  • Edit: If you don't care about the contents of "your branch", then a force push, as suggested below, will work fine. But at that point you might as well just delete "your branch" and create a new one off of master – Christian Fosli Feb 14 '20 at 06:09
  • hi sir, i tried to use rebase, i really do have a hardtime to it. sorry sir. – Janessa Bautista Feb 14 '20 at 06:17
  • Does this answer your question? [Cannot push to GitHub - keeps saying need merge](https://stackoverflow.com/questions/10298291/cannot-push-to-github-keeps-saying-need-merge) – phd Feb 14 '20 at 07:47
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd Feb 14 '20 at 07:47

1 Answers1

0

The branch where you want to push to has commits that xou don't have locally.

If you want to have all commits, you can merge them by doing git pull my_branch origin first(and resolve possible conflicts).

If you want to overwrite the upstream branch, you can use git push -f origin master:my_branch

A force push will overwrite changes from other users. You can do a git push --force-with-lease origin master:my_branch if you only want to do it if no other user pushes to that branch during that time.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • ```git push --force-with-lease origin master:my_branch``` by using this command, will my outdated branch will be updated just like the master? – Janessa Bautista Feb 14 '20 at 05:56
  • A force push (`-f`) will completely overwrite the branch. But this may be a problem if other users have pushed to that branch if you don't know of this. `--force-with-lease` will fail in that case. – dan1st Feb 14 '20 at 05:59
  • i am the only one who push with the outdated branch. i just want to make that old branch to be like the master branch. – Janessa Bautista Feb 14 '20 at 06:02
  • Then use `git push -f origin master:my_branch`. – dan1st Feb 14 '20 at 06:10
  • thank you sir. i did try the rebase, and i had really a hard time. sorry sir. – Janessa Bautista Feb 14 '20 at 06:17