2

Suppose I have worked on a project for a week and committed my changes every day and pushed them to the remote repo:

Mo - Tu - We - Th - Fr - Sa - Su   (origin/master)

Monday morning I realize that what I wrote on the weekend went in the wrong direction. Is it technically possible to pretend I created a new branch on saturday, like this?

Mo - Tu - We - Th - Fr   (origin/master)
                       \
                         Sa - Su   (origin/experimental)

If this is technically possible, does it violate the rules of changing public repos?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662

1 Answers1

1

You can push your current master branch as experimental:

git checkout master
git checkout -b experimental
git push -u origin experimental

Then you would need to reset and force push master, which can be inconvenient if other contributors have already fetched that branch:

git reset --hard master Fr
git push --force

Or you could revert Sa and Su, using git revert:

git revert -n Sa^..Su
# check, then commit
git add .
git commit -m "Revert Sa and Su"
git push

That way, you create an additional commit which cancels Sa and Su, and doesn't requite a push --force.

Mo - Tu - We - Th - Fr - Re  (origin/master)
                      \
                        Sa - Su   (origin/experimental)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Won't reverting the commits cause issues when merging the branch back into master (similar to reverting a merge commit)? – musiKk Mar 09 '15 at 08:04
  • @musiKk it might, but if they are many collaborators who have already fetched `origin/master`, it is better than trying to force push. – VonC Mar 09 '15 at 08:06
  • I fully agree. I just remember this from the "revert a merge commit" case. You have to remember to re-revert the commits before merging, otherwise the merge ignores these, probably leading to strange conflicts. – musiKk Mar 09 '15 at 08:12
  • @musiKk you mean https://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.txt? This doesn't seem to be *exactly* the same situation though. – VonC Mar 09 '15 at 08:19
  • No, but I think the merge will still ignore the commits that are already present in the history. I'd have to try it out to be 100% sure though. – musiKk Mar 09 '15 at 08:30