1

I attempted to merge 2 projects. Project_old and project_new. One which is ahead by a year into one which is behind. I put the projects into separate folders and did this in both of them:

git init
git add .
git commit -m "Initial commit"

I didn't make any backups of project_old, because I expected git to have it all in the repo in it's old state.

I then added a remote branch to project_old:

git remote add project_newer /path/to/project_new
git fetch project_newer

I then began to merge them both

git merge project_newer/master --allow-unrelated-histories

It produced some merge conflicts, so I fixed them with mergetool.

However, something went wrong with the merge and I ended up overwriting some important files from project_old with that of project_new, or so it seemed.

So I thought ok I will make a copy of project_old in it's failed merged state and do the checkout to initial commit's hash and do the merge again:

git checkout XXXXXX

But, for some reason, this commit only includes 10% of project_old which makes no sense whatsoever. I check the log with git log --stat and I can see that only this 10% is included in the first commit. The next commit in the list is the "Initial commit" of project_new's repo, and the 3rd one is the merged final (the current master).

  1. Is there a way to return project_old to it's complete state it was in at the time of it's first commit?
  2. What went wrong so I can learn to not do this in future?
Kris
  • 11
  • 1
  • 2

1 Answers1

0

Check git reflog or simply git log: it should have the commit it was before the merge.

Try and checkout that commit, instead of the initial one.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I get the following with `git reflog`: `4717fec (HEAD -> master) HEAD@{0}: checkout: moving from master to master 4717fec (HEAD -> master) HEAD@{1}: commit (merge): Merge 1 8ccdef9 HEAD@{2}: commit (initial): Initial commit` I checkout 8ccdef9 with `git checkout 8ccdef9`, and all I have is 10% of the project and even missing half the folder structure. – Kris Mar 02 '20 at 13:52
  • @Kris And what is in 4717fec ? – VonC Mar 02 '20 at 13:54
  • it's this: Merge: 8ccdef9 11b1321, the current master. – Kris Mar 02 '20 at 15:11
  • So do yo see only three commits with `git log --decorate --oneline --graph --all --branches`? – VonC Mar 02 '20 at 15:15
  • @Kris what if you checkout/restore 11b1321? – VonC Mar 02 '20 at 15:30
  • I did that as well, 11b1321 is the hash of the repo project_newer's "Initial commit". Checking that out produces a copy of project_newer (which is an untouched repo except for it's initial commit). – Kris Mar 02 '20 at 15:34
  • So no commits from logs or reflog includes the full content of your repo? – VonC Mar 02 '20 at 15:37
  • That is exactly right, and I confirmed it by doing `git log --stat` to view the list of files in the commit 8ccdef9 and it lists only the partial content. – Kris Mar 02 '20 at 15:45
  • @Kris So... it is like a (big) part of that project was never added/commit, then. – VonC Mar 02 '20 at 15:46
  • Yes indeed, but how is that possible if I did a `git add .` before the initial commit to the empty repo? I checked my `.bash_history` multiple times to confirm the order of what I did and I did this in the same order for both of those projects folders – Kris Mar 02 '20 at 15:49
  • @Kris possibly part of it was ignored: `git check-ignore -v -- a/file/which/should/have been/added`. – VonC Mar 02 '20 at 15:53
  • Using check ignore for a file that should be there produced no output. I just copied the relevent part out of my `.bash_history`: https://pastebin.com/raw/GX9nZg7x. In that example PROJECT OLD CONTENTS was a working project in the current home directory, so it's physically not possible for it to be incomplete... – Kris Mar 02 '20 at 16:02
  • @Kris So you don't have the full content of the old project anywhere? (I mean, outside Git) – VonC Mar 02 '20 at 17:00
  • I do, I am just curious what went wrong and how this happened. – Kris Mar 02 '20 at 18:54
  • @Kris I would try again to add and commit the full content in a new repository, and redo the mer e, to see if you get the same result. – VonC Mar 02 '20 at 20:08