1

enter image description here

The orange shows the local branch, the yellow shows the remote branch.

I rebased my branch locally. Then to push the changes to remote, I performed a pull (because it had informed me that my branch is now behind), and then a push. Am I supposed to do a force push? Or delete remote and then push?

Since the two branches are now "merged", is my best course of action to perform a reset/delete of the merge, delete the remote branch, and then push again?

timhuang77
  • 61
  • 1
  • 1
  • 2
  • 2
    Do not do force push. Ever. It's a bad practice and might (will) lead to headaches when more than 1 person works on a branch. – Andrejs Cainikovs Jan 27 '20 at 09:39
  • 2
    @AndrejsCainikovs How it warms my developer heart to see your advice. So many times here on Stack Overflow I see people commenting and answering "Just force push to get rid of the error message" with no explanation of the consequences. I've seen more than my share of new questions from these people about the consequences, like "I just deleted my coworkers commits apparently, why did this happen and how do we fix it", right after someone told them to "just force push". – Lasse V. Karlsen Jan 27 '20 at 09:44
  • check my answer https://stackoverflow.com/questions/59662452/git-rebase-introducing-a-merge-commit/59662870#59662870 in this question what actually rebase does and instead of taking pull why push forcefully is good. – Prateik Darji Jan 27 '20 at 10:55

1 Answers1

0

Say you have a branch master and a branch feature which starts from some commit on master. Then, you push that branch on the remote:

git checkout master
git checkout -b feature
/* do some stuff, commits, etc */
git push origin feature

Next, a collegue of yours commits on master. You would like to have those changes in your branch feature, and therefore you decide to do:

git checkout master
git pull origin master

git checkout feature
git rebase master

If then you want to push that feature branch onto the remote so it looks identical as your local repository, you need the force flag:

git push -f origin feature

Whether or not this is good practice is another question. You could push it to another branch, as you said for example. With git, there are always many options.

mfnx
  • 2,894
  • 1
  • 12
  • 28