Stefano
Stefano

Reputation: 3258

Git sync changes with remote master

I pushed some changes to my remote Git master branch from another location, then on my local master repository I did some changes which edited some files and deleted others and now I have to merge these changes with the changes of the remote repository. The problem happens when I try to push the changes: my commit still uses old files which on the remote branch have been edited and updated. I don't understand how I should download the changes of the remote file and merge them with my changes

EDIT 1

This is the history of the repository. The 6fee6bf commit is an amend commit which adds a new file to the previous commit which was pushed to the remote repository

Upvotes: 1

Views: 4560

Answers (3)

Trevor Norris
Trevor Norris

Reputation: 21089

My opinion would be to make sure you have everything committed on the current master, then create a new branch from there. Afterwards hard reset your local master to remote master. Then either rebase or cherry-pick those changes. Something like the following:

git branch tmp-master
git fetch origin
git reset --hard origin/master

Now you can use the following to easily visualize the differences between the two branches:

git log --decorate=short --oneline --graph --all

Cherry-picking the other commits and fixing conflicts as you go would probably be easiest, but you could also try a rebase. If you need more specific steps, let me know and I'll write 'em up.

Update:

Now that you're here, you can try (from master):

git merge tmp-master

But for cleaner commit history you may want to try the following:

git checkout tmp-master
git rebase master
git checkout master
git merge --ff tmp-master
git branch -d tmp-master
git push origin master

This will take your local commits and reapply them on top of the remote changes. Since you're essentially doing the same work, but from two different machines, you probably don't want a lot of "well, switched machines again" merge commits.

Now, there may be some possible conflicts. There are a couple ways to take care of those. If you have a conflict and know your local changes take priority then do the following (assumed from the tmp-master branch, and have conflicts in the rebase):

git rebase --abort
git branch tmp-master-save
git rebase -X theirs master

Then check if everything looks good continue from the step git checkout master above. Also remember to delete the other tmp-master-save branch. Created it just for safe keeping. When you're new to this it's always better to have a safety mechanism.

Upvotes: 3

Tuxdude
Tuxdude

Reputation: 49473

I would suggest you do this:

git fetch origin

git rebase origin/master master --preserve-merges

The first command fetch would fetch information from the remote repo into your local repo. It would bring in all the changes but it would not merge them automatically with your local copy of any of your branches.

The rebase command would first go back to the common point between your local master and remote, do a fast forward merge of all the changes which came from the remote, then apply your changes on top of it. Using --preserve-merges creates a cleaner history if you have merges in your local copy between branches.

Upvotes: 0

Hulor
Hulor

Reputation: 229

Before you push your commits, you must request for getting changes on the repository you can do it using git pull --rebase command. It will get changes and merge all it can do safely. If some troubles happend, it will say to you to correct conflict yourself, do it and continue the merging using git rebase --continue.

Upvotes: 0

Related Questions