monkey intern
monkey intern

Reputation: 726

Git merge took the "wrong" files and now reset nor revert nor merging with specific commit is working to work with them

So I thought I was getting the trick of using git.

Below there is a brief summary of the last commits done in both dev_ser and dev branches.

What I intended to do: I wanted everything to look like in the commit 6388660.

What I did: With both branches up to date from their remotes, I went to dev_ser and merge my branch into that one, so as to have them being the same, and if any conflicts came, solving them before merging into my branch. This did not have the intended effect, since NO merge conflicts arose and I thought foolishly thought "cool, we are done".

What this got me was the "wrong" version of the files, and not the version from commit 6388660

Right after that, I merged that with my branch.

In commands, first I did:

git checkout dev_ser
git merge -s ours dev

git checkout dev
git merge dev_ser

What I have tried to revert the situation: I feel like I am trying too many things and am going to mess the repo.

First I tried to undo the revert:

git revert -m 1 HEAD

But it did nothing, it showed nothing had changed, files where the same, and git log showed the same structure.

After trying to revert without changes (later I have learned revert does not change history?)

git reset --hard HEAD~1

At last I thought, ok, this is the current stat of affairs, but if I git checkout 6388660, I still see the files I want!!

So let's merge into that and try and recover that version:

git merge 6388660

Which shows Already up-to-date.

So... I really do not know what else to do to get the "RIGHT" version of the files.

*   6dfd968 (origin/dev_ser, origin/dev, dev_ser, dev) Merge branch 'dev' into dev_ser
|\  
| * **6388660** (HEAD) delete
| * 858167f update
| * c34cf18 rename
* | a45d748 update
* | 8725b7c data
* | 0615469 start
* | 6414841 data
* | 474a934 update
* | d7de99c new branch data
* | fb0d0b6 delete
* | 7b65fb5 hot
|/  
*   c407635 (origin/master, origin/HEAD, master) Merge branch 'dev' into 'master'
|
|
| [many previous commits which I do not think are relevant]

Update:

What I have ended up doing was forcing the commit in question be the last existing commit that was "right", by git push origin dev --force, but I am not sure this is the right way to do it so not a real answer I think.

Upvotes: 0

Views: 407

Answers (1)

John Pavek
John Pavek

Reputation: 2665

The extra safe way.

If you're concerned about losing work, and want to play it safe, checkout the last hash you know was okay and make a branch from it.

git switch -c <safebranch> <safehash>

From here you can use git log --oneline <bad branch> in combination with git cherry-pick -n <hash> to individually pull in each commit. The -n does not auto commit the cherry-picked code, so you can modify it or run it before each commit. This is time consuming, but safe.

Upvotes: 1

Related Questions