Xcode 11 Git Stashing

I’ve never been a big user of the version control integration with Xcode. I like being able to see and compare changes in the editor but I prefer the command-line or an external Git client for branch and repository management.

There is a new feature in Xcode 11 that I have found myself using. Here’s a quick look at how stashing works and a bonus tip!

Git Stashing

In the real world, things can get messy:

Git has a solution for this. It’s called stashing. From the Pro Git book:

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time (even on a different branch).

Stashing with Xcode

Most git clients allow you to do this and now you can also do it directly in Xcode 11. Assuming I’ve made some changes to my source code (see Swift 5.1 two quick tips):

Source code with changes

To save these changes without committing, from the Xcode menu Source Control > Stash Changes...:

Xcode source control menu

Add a message for the stash:

Stash message

Git saves stashed changes to the repository and Xcode lists them in the source control navigator:

Stash in source control navigator

Then when you’re ready, right-clicking on the stash in the navigator gives you options to apply or delete the stash. When you apply a stash you have the option to keep or remove it:

Apply stash

Stashing from the command-line

If you want more control, you always have the option to use the command-line as long you can remember how. Here’s a quick stashing cheat list:

A Bonus Tip

As usual, I’m sure you all know this but just in case…

For those situations where you don’t want to stash a change but just discard it. You can revert a whole file but sometimes I want to keep some changes to a file and discard others. You can do this from the code review assistant editor but I stumbled on a way I like more this week:

Hover over the change bar in the margin of the source code editor to highlight a change:

Highlighted change in source code

Click on the blue change bar to show a popup action menu:

Change action menu

If you select “Show Change” the source code editor shows the original code inline with the change:

Show change in source code

Select “Discard Change” to revert this change without touching any other changes in the file.

Read More

Never Miss A Post

Sign up to get my iOS posts and news direct to your inbox and I'll also send you a PDF of my WWDC Viewing Guide

    Unsubscribe at any time. See privacy policy.

    Archives Categories