Git, Merging from Remote Branch

I did this on Bash on Windows 7 (64bit) against my github account.

I tried to push my change to github, and found it’s not possible.


$ git push -u origin master

Enter passphrase for key ….:
To git@github.com:andrewchaa/JonSkeetCSharp4Examples.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘git@github.com:andrewchaa/JonSkeetCSharp4Examples.git
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. ‘git pull’) before pushing again. See the
‘Note about fast-forwards’ section of ‘git push –help’ for details.

So, I need to merge the change. I remembered I committed some code last night at home.


$ git fetch origin

Then


$ git checkout --track -b originmaster origin/master

Branch originmaster set up to track remote branch master from origin. Switched to a new branch ‘originmaster’

Now you check out your master branch (local)


$ git checkout master

Switched to branch ‘master’
Your branch and ‘origin/master’ have diverged,
and have 1 and 1 different commit(s) each, respectively.

And also checkout (get latest in TFS terms) originmaster


$ git checkout originmaster

Switched to branch ‘originmaster’

Now point to master back to start merge


$ git checkout master

Switched to branch ‘master’
Your branch and ‘origin/master’ have diverged,
and have 1 and 1 different commit(s) each, respectively.

In the mean time, I had to set up beyondcompare for git.


$ git config --global diff.tool bc3

$ git config --global difftool.bc3.path "C:/Program Files (x86)/Beyond Compare 3/bcomp.exe"

$ git config --global merge.tool bc3

$ git config --global mergetool.bc3.path "C:/Program Files (x86)/Beyond Compare 3/bcomp.exe"

You can see on which branch you are


$ git branch

* master

originmaster

Now, finally merge


$ git merge originmaster

Merge made by recursive.

Examples/ArrayExample.cs | 41 ++++++++++++++++++++++++++++++++++++++++
Examples/CollectionsExample.cs | 40 +++++++++++++++++++++++++++++++++++++++
Examples/Examples.csproj | 2 +
3 files changed, 83 insertions(+), 0 deletions(-)
create mode 100644 Examples/ArrayExample.cs
create mode 100644 Examples/CollectionsExample.cs

Then, it’s done.


$ git status

# On branch master
# Your branch is ahead of ‘origin/master’ by 2 commits.
#
nothing to commit (working directory clean)

Then, push back to githum


$ git push -u origin master

Enter passphrase for key ‘…..’:
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.31 KiB, done.
Total 7 (delta 4), reused 0 (delta 0)
To git@github.com:andrewchaa/JonSkeetCSharp4Examples.git
cf1a6ea..59b280f master -> master
Branch master set up to track remote branch master from origin.

To summarise, I use the following commands


$ git fetch origin
$ git checkout --track -b originmaster origin/master
$ git checkout master
$ git checkout originmaster
$ git checkout master
$ git config --global diff.tool bc3
$ git config --global difftool.bc3.path "C:/Program Files (x86)/Beyond Compare 3/bcomp.exe"
$ git config --global merge.tool bc3
$ git config --global mergetool.bc3.path "C:/Program Files (x86)/Beyond Compare 3/bcomp.exe"
$ git branch
$ git merge originmaster
$ git status
$ git push -u origin master

Git, Merging from Remote Branch

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s