Laxmikant
Laxmikant

Reputation: 2216

Git Pull specific old commit and merge to the latest

One of my team member has merged 5 PRs in one go and it overwritten the some important code which was written a month ago.

Now I have the commit id of that particular code.

How can I pull changes of that particular commit id and include in the recent changes?

Note all the changes are in develop branch now. And that commit id also part of develop branch.

I want to include changes of that particular commit to recent changes of the develop.

Upvotes: 1

Views: 546

Answers (1)

VonC
VonC

Reputation: 1323145

git cherry-pick <SHA1> is a good option, provided that <SHA1> commit includes all the changes you want to restore.

If that commit simply reflects the code you want, but only add a missing ';' as a change, cherry-picking only that one commit won't help.

The alternative approach is to:

  • reset HEAD of that old commit,
  • carefully add all the changes made since that commit, fixing the overwritten code in the process

That is:

git switch -c tmp dev # where all 5 PR are merged
git reset --soft <SHA1> # old commit with correct code
git status

What the status show are all the files impacted by the subsequent commits: add them, fix the overwritten code and commit -in tmp branch).
Then

git switch dev
git merge tmp

Again, if your old commit actually have all the changes you want, cherry-pick is enough.
But if it does not, you can make a commit with all the right changes through the git reset --soft process mentioned above.

Upvotes: 1

Related Questions