1

A teammate branched to implement a new function and merged it with the main branch, and then someone else's version overwrote his.

Since then, we've added several things to the project and can't just revert, but we need his work.

Is there a way to merge with a previous version?
Basically, how can I merge with an earlier version in Master Branch?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Ganryu
  • 11
  • 2

3 Answers3

1

you can get file content before merge by command

git cat-file -p COMMIT:PATH

and then copy the code from there

max630
  • 8,762
  • 3
  • 30
  • 55
0

Simply use git cherry-pick:

git checkout master
git cherry-pick <old-commit-sha1>

That will re-apply the changes introduced by that commit to the current HEAD.

If the branch had several commits, you can cherry-pick them in one command too.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

If you know the commitID (hash code) then it's simple. Just use git cherry-pick as @VonC suggested in his answer. You can follow below way to achieve it.

  • git fetch origin --> Or change origin with your repo name.
  • git checkout -b newBranch --> I am creating a new branch for safety.
  • git reset --hard origin/branchInServer --> Now new branch should have changes of branchInServer exactly as it is.
  • git log --pretty=format:"%h | %an | %ad | %s" --date=iso --graph -30 --> To print last 30 commits in your branch with graph,date,comment etc. Here it should print your friend's commit and overwritten commit (Change 30 if required)
  • git branch | grep "^*" --> To make sure the branch.

Now, just think this is your output.

* 1112 User1 "last commit in this branch"
* 2234 User2 "second last commit"
* 3344 User3 "OVERWRITTEN commit"
* 4455 User4 "some commit between"
* 0101 Friend1 "Friends commit"
* 5577 User5 "some commit before"
* 9911 User5 "some commit before"

If this is the output you expect, do the following.

  • git reset --hard 4455 (To reset to a commit before override)

OR

  • git reset --hard 9911 (Reset to any commit before and cherry-pick yours)
  • git cherry-pick 0101

Note:
If you don't know what count to put to get the git log with your friend's commit, do this. git log --pretty=format:"%h | %an | %ad | %s" --date=iso --graph | grep "Frend'sName"

smilyface
  • 5,021
  • 8
  • 41
  • 57