1

Say I have 2 branches, develop and p3 (production 3).

Regardless of any commit history on p3, I want to make p3's entire commit history now exactly match develop's commit history.

p3 tries to stay on track with develop, but people constantly merge test branches into p3 and resync it improperly with develop.

How do I make the entire commit history of p3 match develop so I can then call the following?

git push --force-with-lease origin p3
Riveascore
  • 1,724
  • 4
  • 26
  • 46
  • Could you explain in more details what's missing from eftshift0's answer ? FWIW, it looks complete and detailed enough for me. – LeGEC Oct 27 '20 at 18:25

3 Answers3

6

If you want to basically get rid of p3's history

git branch temp p3 # backup current p3
git checkout p3
git reset --hard develop
git push --force origin p3 # replace branch in remote too
prosoitos
  • 6,679
  • 5
  • 27
  • 41
eftshift0
  • 26,375
  • 3
  • 36
  • 60
4

How do I make the entire commit history of p3 match develop

Locally, you mean? First git fetch so that your remote tracking branches are up to date, and git pull both for p3 and for develop, so that your actual local branches are up to date. Then

git branch -f p3 develop

Presto, you rip the name p3 right out of the commit that it currently points to and attach it to the commit that develop points to. They are now absolutely identical branches. The history of p3 is now the history of develop.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

In order to re-create p3 based on the current develop HEAD, you would:

  • make sure to have the latest develop HEAD from the remote origin
  • force p3 to be re-created at origin/develop HEAD commit

That would be using the more recent command git switch (less confusing than the old obsolete git checkout command)

git switch -C p3 develop

As mentioned in the git switch man page:

Similar to --create except that if <new-branch> already exists, it will be reset to <start-point>.
This is a convenient shortcut for:

$ git branch -f <new-branch>
$ git switch <new-branch>

From there, you can force push p3. Without lease (see "git push --force-with-lease vs. --force").

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250