5 commands to level-up your Git game | Opensource.com

5 commands to level-up your Git game

Get more use out of Git by adding these commands to your repertoire.

Business woman on laptop sitting in front of window
Image credits : 

Image by Mapbox Uncharted ERG, CC-BY 3.0 US


Subscribe now

Get the highlights in your inbox every week.

If you use Git regularly, you might be aware that it has several reputations. It's probably the most popular version-control solution and is used by some of the biggest software projects around to keep track of changes to files. It provides a robust interface to review and incorporate experimental changes into existing documents. It's well-known for its flexibility, thanks to Git hooks. And partly because of its great power, it has earned its reputation for being complex.

You don't have to use all of Git's many features, but if you're looking to delve deeper into Git's subcommands, here are some that you might find useful.

1. Finding out what changed

If you're familiar with Git's basics (fetch, add, commit, push, log, and so on) but you want to learn more, Git subcommands that query are a great, safe place to start. Querying your Git repository (your work tree) doesn't make any changes; it's only a reporting mechanism. You're not risking the integrity of your Git checkout; you're only asking Git about its status and history.

The git whatchanged command (almost a mnemonic itself) is an easy way to see what changed in a commit. A remarkably user-friendly command, it squashes the best features of show and diff-tree and log into one easy-to-remember command.

2. Managing changes with git stash

The more you use Git, the more you use Git. That is, once you've become comfortable with the power of Git, the more often you use its powerful features. Sometimes, you may find yourself in the middle of working with a batch of files when you realize some other task is more urgent. With git stash, you can gather up all the pieces of your work in progress and stash them away for safekeeping. With your workspace decluttered, you can turn your attention to some other task and then reapply stashed files to your work tree later to resume work.

3. Making a linked copy with git worktree

When git stash isn't enough, Git also provides the powerful git worktree command. With it, you can create a new but linked clone of your repository, forming a new branch and setting HEAD to whatever commit you want to base your new work on. In this linked clone, you can work on a task unrelated to what your primary clone is focused on. It's a good way to keep your work in progress safe from unintended changes. When you're finished with your new work tree, you can push your new branch to a remote, bundle the changes into an archive for later, or just fetch the changes from your other tree. Whatever you decide, your workspaces are kept separate, and the changes in one don't have to affect changes in the other until you are ready to merge.

4. Selecting merges with git cherry-pick

It may seem counterintuitive, but the better at Git you get, the more merge conflicts you're likely to encounter. That's because merge conflicts aren't necessarily signs of errors but signs of activity. Getting comfortable with merge conflicts and how to resolve them is an important step in learning Git. The usual methods work well, but sometimes you need greater flexibility in how you merge, and for that, there's git cherry-pick. Cherry-picking merges allows you to be selective in what parts of commits you merge, so you never have to reject a merge request based on a trivial incongruity.

5. Managing $HOME with Git

Managing your home directory with Git has never been easier, and thanks to Git's ability to be selective in what it manages, it's a realistic option for keeping your computers in sync. To work well, though, you must do it judiciously. To get started, read my tips on managing $HOME with Git.

Getting better at Git

Git is a powerful version-control system, and the more comfortable you become with it, the easier it becomes to use it for complex tasks. Try some new Git commands today, and share your favorites in the comments.

Computer screen with files or windows open

These helpful tips will change the way you work with the popular version control system.
Business woman on laptop sitting in front of window

Knowing more about how Git works makes working with Git easier. Learn more in our readers' favorite Git articles of the year.


About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.