How to end a tip of the day series when you need a vacation – 128

Hey readers! I’m going to wrap up the series today. I needย a summer vacation. It was a good run, but an outdoors run is even better! ๐Ÿ™‚

Here’s a recap of the most popular tips and what was next in the queue for me to write about.

Best Tips

In the queue to be written

  • git commit –dry-run // yep it is a thing, but was still figuring out what it does and doesn’t do
  • git push –dry-run // is that even possible?
  • git remote prune origin
  • what is a “tip” in git? I think it is the last commit for a given branch. Or is a Tip what the actual pointer is called that points to the last commit in a branch. I’ve been debating which is which from reading this SO thread.

What every Windows user should know

What to read now

  • If you don’t want to click 127 times back and forth, check out Ed’s O’Reilly course called Git for Visual Studio. (Thanks again Ed!)
  • Make sure you practice using before you need it! The best offense is a good defense, or so I’ve been told. I’d prefer eating popcorn from the stands ๐Ÿ™‚
  • Check out the new GitHub for Windows users course – see the links on the left-hand side and the “continue” at the bottom.
  • Practice, practice, practice using Visualizing Git or some other visualization tool to break any mental models you might have of centralized VCS. There was so much I never got to with remotes, cherry pick,ย  etc using the Visualizing Git. And don’t forgot it is an open source project.
  • I enjoyed reading Git for Humans. It was nice to practice a bit on my own, and then go back to the book to make sure I was understanding the concepts. Again, I think my biggest challenge was unlearning previous expected behaviors from centralized VCS.
  • And when the time is right, go read Pro Git. You can also get the book from Amazon. I really want to understand the underlying plumbing in Git in hopes it will help me break my past mental models.

Thanks again to everyone who helped answer my questions, participated in the comments, or simply told me I could take on this challenge. Although I only made it 35% of my goal (not because of lack of material, but because of the lack of outdoors). I learned a lot about the fundamentals I didn’t realize I was lacking, and that was worth it! I hope this series was useful to you as well!

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 git pull to download and merge new changes from remote branch into your local branch via command line – 125

And now for the exciting conclusion*

As shown in Tip 118 – how to download and merge commits using Pull in Visual Studio, today’s tip rinse and repeats from the command line.

Unlike this past week, you are ready to fetch changes and merge them all in one command: Pull.

First, checkout the desired branch.

Second, run the following

$ git pull

And git will perform the fetch and merge all at the same time.

git pull shown fetching and merging in a

*I think my greatest obstacle in learning git commands is knowing when  a command or subcommand wants 1. a remote 2. a branch name 3. nothing at all.

Has anyone tried nursery rhymes yet to learn git commands in the same way we learn the names of the planets?  I really need to get back into standup comedy.

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 `` 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 is now shown in the local branch. 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

How to fetch down updates from a Git remote repository – 122

In Tip 117 you saw how to fetch down updates from a Git remote in Visual Studio. Note: I said “fetch down” instead of “pull down” because tip 117 only fetches the data. The result is the data for the remote tracking branches is updated, but not applied to your local branch. Hence the “incoming commits” terminology in Visual Studio.

Today you’ll see how to rinse and repeat for the command line.

First, let’s verify the currently listed branches our local remote is aware of.

$ git branch -a

git branch -a showing 3 remote tracking branches

Second, let’s add a new branch to the remote on GitHub.

I-am-a-new-branch (less than a minute ago)

Third, let’s sanity check that no magic sync’ing has happened behind the scenes.

$ git branch -a

And you’ll see the same number of remote tracking branches. (yes, I took a second screenshot. I’m too lazy to think about optimization ๐Ÿ™‚ TBH I have to verify what I’m writing here!

git branch -a still showing 3 remote tracking branches

Fourth, run the git command to fetch

$ git fetch origin

I know you can do just `git fetch` but I don’t like learning shortcuts first.

git fetch origin creates a new tracking branch

I’m not sure if the branch on the left is the remote tracking branch or if it is the one on the right. My guess is the one on the left hand side represents the branch from GitHub and the one on the right represents the remote tracking branch.

Nonetheless, you now have a new remote tracking branch as shown below.