0

I have a repo consisting of several commits when master is pointing to the last commit.

v1 -- v2 -- v3 -- v4

              |
              V
            master 

Would like to set for example v2 as the new master

v1 -- v2 -- v3 -- v4

    |
    V
  master

But haven´t found a solution which works.

p.s.: This is not a duplicate of How to revert Git repository to a previous commit? because i

  1. don´t want to temporary switch to another commit
  2. don´t Hard delete unpublished commits
  3. Undo published commits with new commits
  4. and i don´t want to revert something

As described, i simply want to make an older version/commit to be the new master

Coliban
  • 601
  • 2
  • 9
  • 24
  • 3
    Possible duplicate of [How to revert Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – 1615903 Oct 19 '17 at 07:00
  • 2
    Just take 5 minutes and read the above link. But maybe you should elaborate on exactly why you think you need to do this. – Tim Biegeleisen Oct 19 '17 at 07:01
  • I´ve tried the recommended thread, but that is not working. I can check out the v2 commit, but i am not able to set the master to this version since git still preserves the old master and a "git reset --hard v2commitID" will not change the master in .git/refs/heads/master. Therefore a "commit" only tells that there is nothing to commit. – Coliban Oct 19 '17 at 07:09
  • Your question is unclear, you want to commit the changes to your local master branch? – danglingpointer Oct 19 '17 at 07:12
  • @LethalProgrammer: what is unclear? As i asked in my original question, i want to set the master to v2. – Coliban Oct 19 '17 at 07:14
  • I didn’t understood you’re question. V2 is the latest master you pulled from origin Or v4? – danglingpointer Oct 19 '17 at 07:18
  • @LethalProgrammer, v4 is the latest master. I want to set v2 as master – Coliban Oct 19 '17 at 07:23

2 Answers2

2

You can set the current branch to any commit with reset --hard (throws away current changes!). If you want to change the server version you have to push it (with --force or --force-with-lease). This does rewrite the history of the server. Be careful!

git checkout master
git reset --hard {TARGET COMMIT}

git push --force-with-lease

If you want to keep the current Version as v4 make a branch or tag that points to that version before reset;

git branch releases/v4

If you can not force push due to the server configuration - you must change the server configuration to allow pushing with force.

If you want to undo the changes of v4 and v3 you can aswell revert the changes (which means undo the changes as a new commit) check this link Revert multiple git commits

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • Console, thank you, when i do this, i get "remote: GitLab: You are not allowed to force push code to a protected branch on this project." and master is still pointing to the newest version v4. (?). And besides of that, i don´t want to throw away anything, just set the master to an old version. Thanks – Coliban Oct 19 '17 at 07:30
  • @Coliban: Well, the error message tells it all. When push-forcing v2 you are effectively deleting v3 and v4, and the GitLab configuration does not allow you to do that. You can speak to the administrator of your server or alternatively git-revert the v3 and v4 commits and push normally. – rodrigo Oct 19 '17 at 07:38
  • force push is danger and it's not widely encouraged. You should aware what you're doing. Are you sure you don't want v3 and v4? – danglingpointer Oct 19 '17 at 07:42
  • As in my original sketch, i want preserve all commits and set only the master to v2. – Coliban Oct 19 '17 at 07:45
0

Since the repository on GitLab, in your case, prevents a forced-push of master, you can't perform it as @Console suggested - which is the right way to do it. Because of that, another option you can test is to commit v2 as a new commit.

You can do it in many ways. here's one possible way (do it with careful): git checkout -b v2based <v2hash> git checkout master git merge v2based -s theirs //validate here that the HEAD is equal to the origin v2 git push origin master git branch -D v2based

yorammi
  • 6,272
  • 1
  • 28
  • 34