How to merge old version of file with new version?
Asked by David Newey:

I had edited a file and pushed it to the repository. Another developer did not pull the repository files to his tree and edited the file and pushed it to the repository, over-writing my changes. How do I merge both versions or access both so I can merge them manually?


Answers:
author: Tim Crall
body: If your commit is still there, you should be able to do this

1) Checkout your commit

2) Create (and checkout) a branch at that commit

3) Make an inconsequential change to the code (maybe add a blank line somewhere)

4) Stage and commit the change

3) Checkout the original branch ("master" or whatever branch you were both committing to in the first place)

4) Merge the branch you created into the current branch

5) If necessary, resolve any conflicts

6) If you had to resolve conflicts, stage and commit the changes.

At command line, it would look like this:

git checkout \<commitID\>

git checkout -b \<newBranchName\>

\<edit file to make an inconsequential change\>

git commit -a -m "creating a branch to merge from"

git checkout master

git merge \<newBranchName\>

\<edit file to resolve conflicts\>

git commit -a

You can of course follow the same steps in SourceTree.

comments: author: Tim Crall
body: If he did do a force push and thus your commit is gone altogether on the remote server but still present on your local clone, you probably want to do something where you create a branch on your local clone, pull his changes into that branch, and then merge the two branches.


author: izharaazmi
body: **First thing**, you should never allow two or more developers to push to same branch ever. Let them start work with their own branch and commit on the same. Later the branches can be merged by any of the responsible developer.

**Second**, If you two were on same branch then the other developer should not be able to push, because the remote (Bitbucket etc) host would disallow any such reference mismatch.

Suppose you started work at commit A. He too started at **A** . Then you committed and pushed creating commit **B** . Now when he tries to push on the same branch he wont be able to do so, because **A --\> B** reference already exists, **A --\> C** can only be created if it is on a different branch.

Therefore he must pull**B** and then commit and push and hence creating the flow **A --\> B --\> C**.

Here it is possible that he actually pulled your commit, but overlooked your changes and committed his own code and therefore your changes lost.

**Now answer to your real question:**

To do the merge**manually** just archive (*right click on the commit and select **Archive*** ) the sources from sourcetree for both commits viz. yours and his. Use some diff / compare tool to compare them and merge. Personally I prefer***Beyond Compare*** by ***Scooter Software**,* but you can choose whatever you feel comfortable.  

When you have your final merged version, simply commit it to the repository.  

Note that once you push your changes, it sits in the repository permanently. You can never ever accidentally overwrite those files. This feature always comes handy if you ever get something messed in your files.  

Let me know if I could address your issue. If not I'd be glad to assist if you precisely provide me the scenario with you.

comments: author: Tim Crall
body: It's possible that the second developer actually did overwrite the commit if he did a "force push" (push -f)You should almost never do a force push and many people configure their repos to disallow them (I know you can do this on Stash, I don't know about BitBucket but I would guess you could).


author: izharaazmi
body: I have replied to your question assuming that both of you have committed over the same branch in the repo (read more in the answer). However, if the commits are on different branch you have nothing wrong going. Git merge does the same thing what you need to do.