jaybny
jaybny

Reputation: 1028

How do I match code to Git commit, and create branch?

I have a git repository on my "DEV" machine. A while ago, I copied some code to my PROD machine. Since then, there have been many commits in my DEV machine. However there may have been a couple of ad-hock code changes on my PROD machine that was never committed or "Brached" out.

Question: How can I find the commit on DEV that is closest to code on PROD?

I then will create a branch and merge. The objective is to merge all into 1 branch. There are many files on PROD that have never been modified so maybe that will be easier, just search all commits for an exact match.

I use git-extensions.

ty.

Upvotes: 1

Views: 216

Answers (2)

Christopher
Christopher

Reputation: 44244

With no other information (and assuming your PROD machine's install isn't a git repository itself, which would make this quite simple), I'd probably just compare file creation timestamps with your DEV repo's commit history. Presumably your PROD machine's code was copied in a narrow window. It's also likely that you weren't committing while you were doing this copy. Simply use git log to find the commit timestamp right before the copy timestamp, then something like:

git checkout -B new_branch <hash_of_that_commit>    # Checks out 'new_branch' at that commit

Next, diff DEV and PROD, copying PROD changes back onto DEV either incrementally or as a batch. Commit with each increment or batch.

At that point you have a viable topic branch. You can merge or rebase it back into your master branch at your leisure.

Upvotes: 0

dahlbyk
dahlbyk

Reputation: 77530

One option would be to use git bisect to search over your old commits, at each step using a directory diff tool to compare the git working directory with PROD. If the working directory has new changes since PROD you want to keep, mark it good. If the working directory has old versions of files and you want the PROD version instead, mark it bad. Eventually you should find the commit that's closest to where PROD started - create a branch from there, copy over from PROD and you're good to go.

Upvotes: 1

Related Questions