10 Advanced Git Tips from American Tire Distributors Advanced Analytics Center of Excellence

Improve your developer workflow

Git is an amazing piece of technology. It’s a tool that virtually every data scientist or developer uses every day, and the ATD analytics team is no different. When you use a version control tool like git, you can simultaneously work on the same code base as your team and then periodically merge your changes together. This can minimize the potential of any conflicts and results in a single, unified project.

Why does ATD’s AACoE use git as opposed to other version control options, such as Apache Subversion (SVN)? Git is lightning fast, thoroughly documented and extremely popular, which means there are many helpful resources online. Like this one!

Many people just scratch the surface of what git can do in their day-to-day routines. Although there are a handful of commands that are the basics of any git workflow (like git add, commit, push), here’s a list of 10 useful tips and tricks that are more advanced.

1. You can see your stashed changes with git stash list, which might return an output that looks like this:

Here we have a single item in the stash, which is at position 0. To apply the changes we stashed, you can use git stash apply stash@{0} which will apply and leave the changes in the stash. You can also use git stash pop stash@{0} to apply and delete. Manually deleting a stashed item can be done with git stash drop stash@{0}. For any of these commands, you would replace the number 0 with the (possibly multiple) stashed items you want to apply.

2. You can use git archive to compress your versioned code

git archive — format zip — output version_0.1.1.zip “0.1.1”

This supports many formats and you can provide a number of refs for what you want to compress. The first example we provided uses the master branch, but you could also provide a commit hash or a tag.

3. The .git folder

Similarly, inside of tags you’ll find tags you add. Inside of remotes, you’ll find another folder for each remote. Usually you’ll have origin, and you may have additional remotes such as upstream if you have forked a repository and then cloned your fork locally, which is the common open-source workflow. Note that refs for the remote are stored statically in your local git repo — there’s no way for this to remain constantly aware of changes that occur in the actual remote repository. This is why if you want to check out or merge from a remote branch, you must do a git fetch or a command that includes fetching like git pull. The fetch step updates .git/refs/remotes/, and you’re now aware of the latest changes in the remote and can checkout or merge accordingly.

4. git log — pretty

hash=$(git log -n 1 — pretty=format:’%H’)
echo “The hash is ${hash}”

The hash is 5465dXXXX

5. git commit — amend

git rm — cached <file to remove>

git commit — amend

Note this was for a new file, as git rm — cached will remove the file from the index, and then git commit — amend recreates a single, unified commit.

6. Use git revert to undo shared history

A follow-up to that is a sort of golden rule for git — “Never modify history that is shared.”

You can modify, rewrite, rebase, etc. your own history however you like, but once you’ve merged those changes into a branch that is shared by others, it’ll cause huge problems if you modify history because other users are not aware.

Rewriting your own history, however, can be a great way to neaten things up. When working on a feature, you could commit all the time with small changes so you have frequent “save points,” and once the feature is done, you simply rebase those commits to form a couple of complete changes as if you made all the steps in one go.

7. Find broken commits with git bisect

8. Undo committed changes to a single file

9. Use the — operator to disambiguate command arguments from refs

10. Use git reflog to recover deleted branches

How do you determine the commit hash of a branch that you just deleted? You can use git reflog, which tracks the movement of HEAD.

We invoked git reflog | grep branch_name:

fe2fc1b HEAD@{39}: checkout: moving from topic/feature1 to branch_name

Which reveals that 39 movements ago, we checked out branch_name

Then, getting back our branch is one line: git checkout -b branch_name fe2fc1b

These git tips are extremely helpful to all members of the AACoE. What are your best git tips?

Who we are as people is who we are as a company. We share the same values in the way we work with each other, our partners and our customers. We are ATD.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store