How to merge old version of file with new version?

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?

3 answers

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. 

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).

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.

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.

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.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Brian Ganninger
Published Jan 23, 2018 in Sourcetree

Tip from the team: workflow and keyboard shortcuts

Supported Platforms macOS Sourcetree has a lot to offer and, like many developer tools, finding and using it all can be a challenge, especially for a new user. Everyone might not love ...

798 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you