-1

I would like to go back in the past for master, but keeping posterior changes (merged or not into master) as branches (possible mergeable in the future)

A picture to be clear (commits+branches) : enter image description here

Before : master-in-c6 + branch3

After : master-in-c1 + branch4 + branch3

troubadour
  • 253
  • 3
  • 10
  • 1
    What does "posterior changes" mean? Also, please be aware that groups of commits are not branches, a branch is only a pointer to _one_ commit. – evolutionxbox May 05 '21 at 14:09
  • You can create a new branch on any commit and also point/reset master to any commit. – Felix Kling May 05 '21 at 14:10
  • If you try and merge `c1` into `master` you'll probably find that it will say "already up-to-date" because the commit that `master` points to already has `c1` as an ancestor. – evolutionxbox May 05 '21 at 14:12
  • I fix : c6 as new branch4 --- posterior : in the future regarding c1 – troubadour May 05 '21 at 14:15
  • Sounds like regret type 3. https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard – matt May 05 '21 at 16:53
  • Side note: *anterior* and *posterior* seem like anatomical terms for front and back, and hence don't really apply well here. From a graph theory perspective you can use *predecessor* and *successor*, but even here there are pitfalls, because Git's graphs are backwards from the more typical diagrams. Git uses *ancestor* and *descendant*, from genealogy, which is the way to go, since Git also uses the term *parent* for the immediate ancestor of some commit. – torek May 06 '21 at 01:29
  • posterior just means "in the future" compare with the time of c1 commit. – troubadour May 06 '21 at 08:34

1 Answers1

2

Is this what you are looking for?

git branch posterior master # create a new branch that is a copy of master
git branch -D master # delete master
git branch master [hash of c1] # create a new branch called master at c1

here is another way to do pretty much the same thing

git status # make sure there are not local changes
git checkout master
git branch posterior # make a copy of your current position
# or just a tag if that's what you want:
git tag posterior
git reset --hard [hash of c1] # point master at its new location go with it
Alex028502
  • 3,486
  • 2
  • 23
  • 50
  • You don't realy need to create the `posterior` branch, since you will be able to see all those commits in what you have called `branch3` if I understand correctly – Alex028502 May 05 '21 at 17:49
  • Thank you Alex, I've tried EXACTLY the second way ("pretty much"). Just after the `git reset --hard c1Hash` I did a `git push origin master` (yes because I do want to change my gitlab. But I got : `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.` Done on a test project, not in mine, so nothing is done in the project in question. – troubadour May 06 '21 at 08:29
  • if you want to change it on origin, you have to go `git push origin master --force-with-lease`, and that will replace `master` on origin (github/gitlab) with whatever your local one is... (and then also go `git push origin posterior` to save that on origin). You might have to find a setting somewhere that allows force pushes on the main branch – Alex028502 May 06 '21 at 09:38
  • Yes Alex `remote: GitLab: You are not allowed to force push code to a protected branch on this project.` a request in on (to be owner and to unprotect master), to be continued ... thanks a lot. – troubadour May 06 '21 at 14:06
  • https://i.stack.imgur.com/vsxym.png Just temporarily make a different branch the default branch in the gitlab settings, force push to master, and then change the default branch back to master. I'm pretty sure it'll only stop you force pushing to the default branch – Alex028502 May 06 '21 at 16:34
  • https://i.stack.imgur.com/hp51E.png even better I just found "allow force push" under the "protected branches" section of the "settings" page in the gitlab repo configuration – Alex028502 May 06 '21 at 16:36
  • Great it works Alex :) I didn't forget to `git push origin posterior` for the new branch (branch4 in my example), thanks a lot ! – troubadour May 10 '21 at 08:35