Today I Learned Git Part 4

This is part 4 to my series of posts about learning git. Most of the sections here are from various TILs I've found and hand picked ones I found useful.

Excluding Files Locally

Excluding (read: ignoring) files that should not be tracked is generally done by listing such files in a tracked .gitignore file. Though it doesn't make sense to list all kinds of excluded files here. For files that you'd like to exclude that are temporary or are specific to your local environment, there is another option. These files can be added to the .git/info/exclude file as a way of ignoring them locally.

Add specific files or patterns as needed to that file and then save it. Relevant files will no longer show up as untracked files when you git status.

Find The Initial Commit

By definition, the initial commit in a repository has no parents. You can exploit that fact and use rev-list to find the initial commit; a commit with no parents.

git rev-list --max-parents=0 HEAD

The rev-list command lists all commits in reverse chronological order. By restricting them to those with at most 0 parents, you are only going to get root commits. Generally, a repository will only have a single root commit, but it is possible for there to be more than one.

See man git-rev-list for more details.

Git Log since

At the end of each day, I try to record what I did, to jog my memory during the next morning's standup. This is a helpful aid:

git log --since="24 hours ago"

I SSH into my work machine and run this in my project's root directory. Combined with an alias from the Hashrocket Dotmatrix, glg (git log --graph --oneline --decorate --color --all), I get a terse summary of the day's work, ready to be pasted into your note-taking or project management tool of choice:

glg --since="24 hours ago"
 7191b92 (HEAD, origin/master, origin/HEAD, master) Good changes
 3f4d61e More good changes
* ecd9dcd Even more

Git Log With Authors

In my never-ending quest to better summarize my work at the end of the day using computers, I discovered today the Git --author flag. It works like this:

glg --since=midnight
 4ba91a8 (HEAD, origin/checkout, checkout) Add guard for manual entry of employee discount
 3a4e4c9 Seed a coupon and code and auto-apply in preview
* cb1adee Add discount

The alias glg is discussed here.

I use this when multiple developers or teams are committing throughout the day to the same repository, to disambiguate our work from others. Ready to paste into your billing software of choice.

Git Snapshot

To save a snapshot of your current work in git, try this command:

git stash save "snapshot: $(date)" && git stash apply "stash@{0}"

This saves your current work in a timestamped stash, without removing it. In Hashrocket's dotmatrix this command is aliased to git snapshot.

Grab A Single File From A Stash

In git, you can reference a commit SHA or branch to checkout differing versions of files.

git checkout d3d2e38 --

In the same way, you can snag the version of a file as it existed in a stash. Just reference the relevant stash.

git checkout stash@{1} --