Tag: remotes

How to handle a failed push when the remote contains work you don’t have locally – 127

In Visual Studio, if the remote contains work that you don’t have locally, and you try to do a push to that branch, you’ll see the following in Visual Studio Team Explorer:

Failed to push to the remote repository. See output window for more details.

The output window contains more information:

output window - the remote contains work that you don't have locally

You’ll resolve this by clicking Pull

Pull from Team Explorer

Visual Studio will automatically do any merges. Since there are were no conflicts, the auto-merge was successful and created a merge commit.

Push outgoing commits

Now you can simply Push these changes up to the remote.

Successfully pushed to origin/master

From the command line

Here’s what the corresponding scary message looks like from command line.

Updates were rejected because the remote contains work from CLI

You’ll first want to Git Pull – which will result with Notepad prompting me to update my merge commit message if needed.

And now in Mortal Kombat fashion (I spelled Kombat right this time), you need to Finish It!

and do a Git Push.

How to view newly added branches on a remote – 126

A few weeks ago I tried to figure out how to do this, but to no avail. I couldn’t find an answer because the solution is a more of a generic “catch-all” command to get lots of info about a remote, including figuring out which branches were newly added to a remote. 

$ git remote show origin

git remote show origin showing saraford-patch-1 as a new remote branch

Note how next to the circled `saraford-patch-1` you see the text `new (next featch will store in remotes/origin). Now I know what new branches (if any) I’ll “fetch down” the next time I do a fetch. 

Obviously this command shows other goodness, like seeing which local branch talks to which remote branch, and more importantly, what “origin” (or some other specified remote) points to.

Thanks to Jeff (blog comments) for the pointer to `git remote show origin` which answered my question!

How to merge from a remote tracking branch into your local branch in command line – 124

Today’s tip is the continuation from Tip 118 – how to download and merge from a remote branch into Visual Studio, but using the command line.

Let’s suppose you have a local branch that’s tracking the remote branch (see yesterday’s tip). And let’s say someone has made an update to that remote branch (e.g. they made a commit `newfile.md` via the GitHub UI to the branch). Now you want to get those changes into your local branch.

First, we’ll put this week’s earlier tip to good use by doing a `git fetch` just to get those changes onto your computer.

Now you want to merge those changes into your local branch.

First, make sure you’ve checked out the desired branch to receive the merge.

Second, do the merge from the remote tracking branch (since it has the data)

$ git merge origin/<branch-name>

git merge origin/I-am-a-new-branch

And now if you do a `dir` or `ls` you’ll see the changes. In my example, the newfile.md is now shown in the local branch.

newfile.md shown in local branch

How to checkout a remote branch for the first time via the command line – 123

In Tip 116 you saw you to create a local copy of a remote branch in VS. In today’s tip, you’ll learn how to do this from the command line.

First, let’s verify our current list of branches.

git branch -a showing only 1 local branch 

Next, you can use the following git command (provided you only have one remote)

$ git checkout I-am-a-new-branch

It seems that you don’t have to specify the `origin/` part. Git knows to look for corresponding tracking branches.

And you’ll have your local branch.

creating a new local branch from a remote tracking branch

As I’ve mentioned time and time again, I hate shortcuts when learning things for first time. This SO answer tells me there’s a more complete way.

$ git checkout -b <branch-name> origin/<branch-name>

I gave this a try with my gh-pages branch and sure enough it worked!

git checkout -b gh-pages origin/gh-pages