0

I've made a mistake with Git.

I started to work on a project, created me/branch1, finished it, and merged it to master; than worked on me/branch2, finished it, and merged to master. I did the same with a dozen of other branches.

At some point I realized that all of my work shouldn't have gone into master, but into development branch instead. So I branched out development from master, and ever since worked exclusively in developmet, in the same way I used to work with master: create a branch, work on it, merge it back to development, etc.

This left master in some "undefined" state, outdated and useless, since developmet is the only branch I will work in, even in the far future. Is there a way to "reset" master to some clean, zero state, which would make my initial mistake of working in master instad of in development nearly invisible to outside users, and also would not pose a problem when merging development into master if the time comes?

Note, I was the only one working in this repository.

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • I think you can `git checkout SHA1` where SHA1 will denote the SHA1 of commit you want to go back to. Now, just choose appropriate commit you want to go back to :) – Michał Turczyn Dec 01 '20 at 15:43
  • you tried to apply a workflow (_git flow_ in this case) after you already begun. Just merge a stable version of develop onto master and you'll be fine. Otherwise, you'll have to [delete commits](https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) and force push this deletion. – Daemon Painter Dec 01 '20 at 15:53
  • Unfortunatelly, there is no stable version, nor there will be in the near future. – Danijel Dec 01 '20 at 15:54
  • Some clarification questions- the tip of master is a commit that is currently in development? (There is nothing in master that is not in development?) Everything in development is something you want to keep? You want to make master look like how it did before you did your first merge into it (e.g. me/branch1)? – TTT Dec 01 '20 at 19:39
  • 1
    @TTT Yes. Yes. Yes. Yes. – Danijel Dec 02 '20 at 07:34
  • 1
    @Danijel ok, great. Then I agree with GovindaSakhare's answer. You should be able to reset --hard your master branch to any earlier commit ID that you wish. In the future you will be able to cleanly merge development into master. (Unless new commits go into master before development gets merged in, but that's perfectly normal if it happens and then you would simply resolve any conflicts.) – TTT Dec 02 '20 at 09:29

1 Answers1

2

You can reset back to a certain commit.

git checkout master
git log --oneline

Now pick the hash of the commit to which you want to reset the master branch

git reset --hard commit_hash

You can also time-travel the reset

git reset --hard master@{2.days.ago}

Perhaps, a safe option would be to not perform --hard reset. instead, perform the --soft reset and stash the changes

git reset --soft commit_hash
git stash

now if anything goes wrong, you can simply apply back the stashed changes.

git stash apply
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
  • What will happen when I merge `development` back to `master`? Will this be a problem? – Danijel Dec 01 '20 at 15:52
  • All the changes from the development will be merged into master, provided there are no-conflicts. – Govinda Sakhare Dec 01 '20 at 15:54
  • I plan to revert `master` to empty, initial state. When I merge `development` into empty `master`, what will happen... – Danijel Dec 01 '20 at 15:55
  • I don't think anything could go wrong in your case. But still, if something goes wrong, you can use `reflog` to reset everything. – Govinda Sakhare Dec 01 '20 at 16:02
  • git store history for 30 days, so you can reset whenever within 30 days. After 30 days, you won't be able to recover the lost changes. – Govinda Sakhare Dec 01 '20 at 16:03
  • 1
    Note all the commits would still be on development, so it would be possible to reset back to any desired commit, even after reflog expires. Also, reset --soft with stash is probably unnecessary for the same reason. – TTT Dec 02 '20 at 09:31
  • @TTT Agreed. Stash will be useful only if the `master` has some commits that the `development` branch doesn't have. – Govinda Sakhare Dec 02 '20 at 09:34