Chris Hannah

Git


Idiot Proof Git

Doug Turnbull:

I’m an idiot. And git is hard. A lot of places use a rebase-based Git workflow, and I’ve made git less hard with a set of handy aliases. Put these in your ~/.gitconfig and turn git into an actually less painful command line tool to use.

For people that want to use Git, but either aren't a developer, or just want an easier way to use common functionality via the command line, these may be for you.

There's aliases to update your local code, publish your code, sync your code with the master branch, and a few other helpful commands like opening a PR on GitHub.

A Better Git Command Line Experience

I’ll start by saying that I’m the sort of person that prefers to use the command line for Git commands. But there are still occasions where I just found it annoying, such as managing huge amounts of branches, reading old diffs, and also when making a big commit, and wanting to get a quick overview of what’s being added.

For those occasions, I used Fork. Although I’m sure for my uses, I could have probably used any Git GUI. However, I’m now using a terminal UI called Lazygit. I think it’s incredible. It lets me stay in the terminal, but gives me the functionality (that I use) of a typical GUI app.

This is what it looks like:

Because it’s a terminal app, it’s full of keyboard shortcuts, that make everything super fast. For example, to pull changes, you just use a lowercase p, and to push it’s an uppercase P.

Most of them are that simple that you just tap one key, but they tend to be limited to the active panel. For example, if you want to manage branches, then the branches panel needs to be the active one. Once it’s active, you can use things like space to checkout the selected branch, n to create a new branch, d to delete, o to create a pull request, etc. There’s honestly so many that if you were to check out Lazygit, I’d recommend having a glance at the full list of key bindings.

This is obviously a small example, but here is the experience of committing files:

It’s not limited to the keyboard though, you can navigate Lazygit using the cursor as well:

After installing Lazygit, I haven’t needed to use a GUI application at all. I know some may prefer to use a GUI, but I certainly have found Lazygit so much easier to use. Especially because of the abundance of keyboard shortcuts, and also because it lets me keep my Git activity to a single terminal window.

Lazygit works everywhere, macOS, Linux, or Windows, so if you want to enhance your Git experience in the command line, I’d definitely recommend checking it out.

Find the Right Git Command With Git Explorer

If you’ve ever wanted to do something in Git via the command line, but you’re just not quite sure what the command is. Or you roughly know what you want to do, but don’t know where to start, then Git Explorer is the perfect tool.

All you need to do is select from of them 19 options after “I want to”, and that can be something like “I want to compare two commits” or “I want to configure”. Then you get further options to refine the query, and it shows you the exact command you need.

For example, if you wanted to remove multiple branches that matched a certain pattern, then you just need to select these options:

I want to

Then you get told the command:

git branch | grep <regex pattern> | xargs git branch -D

And also a helpful note to help you understand it:

e.g. git branch | grep “-” | xargs git branch -D will delete all branches that have ‘-‘ in their names or git branch | grep -v “master|staging” | xargs git branch -D will delete all branches except staging and master.

NB: Always put your regex pattern in quotes

I use Git via the command line myself, but there’s always the odd scenario where I can’t quite figure out the command or proper syntax, so this website will be perfect for me. It’s going straight into my bookmarks.

Check out Git Explorer.

Using GitHub and Xcode Together

It’s been just under a year since I published my article on how to connect an Xcode project to a GitHub repository. Since then, Xcode has kept being updated with new Source Control features, and the guide started to break. So I’ve decided to start fresh and show how you can quickly and easily use GitHub to track your Xcode project.

The Xcode used for this guide was version 10.1.

We will first go through initialising a Git repository, finding the Source Control features in Xcode, and then either link it to an existing GitHub remote, or create one directly inside Xcode.

Initialising a Git Repository

You will need to make sure your project is inside a Git repository. The easiest way is to check the “Create Git repository on my Mac” checkbox when first creating the project, but you can also use the git init command1 to create one inside the root folder.


Once your project is being tracked by Git, you will see your project in the Source Control pane on the left of Xcode. It’s the second icon from the left, and you can quickly access it using CMD + 2.

This shows any local branches, tags, and also any remote repositories you have set up, along with remote branches. So you’ll be able to use this pane, along with the Source Control option in the menu bar to manage your repository once it is set up.

Setting Up a Remote

From this stage you have two options, you can link this repository to an already existing remote you have set up, or you can use Xcode’s new tools to create a new one. Either option can be found by right-clicking on the Remotes folder.

I’ll go through both methods.

Using an Existing Remote

For this example, I created a blank repository on GitHub. Once a blank repository is created, they show you a few ways to initialise the repository. However the only thing you’ll need is the URL address inside the Quick setup section. For me, it’s https://github.com/chrishannah/Test-Existing-Remote.git .

So if you go back to Xcode, right click on Remotes, and select Add Existing Remote, a new window will appear from the top prompting for the location. You just need to paste in the URL you got from GitHub, and select Add.

Once you’ve done that, you should see the new remote appear in the Source Control pane, and you’ll be able to commit, push, pull, etc. from the menu bar in Xcode, along with the usual places.

Create New Remote

If you haven’t got a remote repository set up yet, this is the easiest way to do so, and you don’t even have to leave Xcode.

Like before, go back to the Source Control pane, right-click on Remotes, and select Create “Project Name” Remote. You’ll then be presented with a window where you can customise the new repository you will be creating.

First of all, you’ll need to connect your GitHub account if you haven’t already. To do this just click on the Account drop down menu, tap Add, and then enter your GitHub credentials.

You can then enter a repository name, which will also dictate the URL, an optional description, the visibility of the project, and name you will call the remote in Xcode. The default options are usually fine, although you may want to make the repository private. The last field, remote name, can be left as the default “origin. This is just a label you can give to the remote repository, and if you used multiple, it would be helpful to distinguish each of them. Origin is just the conventional name that most developers use.

Xcode will then create the repost on GitHub, and push your code. You should then see the new remote appear in the Source Control pane, and you’ll be able to commit, push, pull, etc. from the menu bar in Xcode.

You’ll also find your code on the remote repository on GitHub.


I hope you found this guide helpful. If not, then please let me know either in the comments below, or on Twitter where I’m @chrishannah.

Creating .gitignore Files the Easy Way

If you’re a programmer, you’re probably aware about version control and Git, and maybe even what a .gitignore file is.

If you don’t:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.

Anyway, creating these files can be annoying to write manually, and there a bunch of templates all over the internet to make this much easier.

I however, found a much better solution for creating these files, and it’s gitignore.io. It’s a website that you can use to generate a .gitignore file, but also a command line tool that you can use, so you never have to leave your terminal.

It has support for operating systems, IDEs, and programming languages. So my standard file will be generated from macOS, Xcode, and Swift, since that’s how I roll.

You can type (with autofill of course) whatever templates you want to make use of straight into the website, and then hit ‘Create’.

For the command line, you’ll have to first install it, and then the gi command will be available. All you need to do is type gi followed by a comma-separated list of the same items you would use on the website.

So mine would be:

gi macos,xcode,swift.

The command would of course, output this out via the standard output, so you can direct it straight into your .gitignore file by writing something like:

gi macos,xcode,swift >> .gitignore

It’s super easy, and it saves a lot of time.

As a little bonus, there’s also a quick video on how to install and use the command line tool.