Home Git me baby one more time
Post
Cancel

Git me baby one more time

For anyone who egoistically thought that having mastered the intricacies of interactive rebase in git is the pinnacle of using version control systems, something beyond advanced that only the selected few know, I’m here to tell you that one sunny day, when you expect it the least and when a critical hotfix in production would be most urgent, git gives you a heavenly sign, and the humbling reality hits you with "error: Ref refs/remotes/origin/master is at 2838f0b but expected 18678f3"(or something equally obscure and unexpected) only for you to realize you know nothing about how git objects work internally, and that your perceived expertise is nothing but imaginary.

The actual problem in my case was that I ran git gc --prune on a quite large and busy repository (which I seldom do), and the remote branch was modified in the meantime. Lacking better ideas, I simply deleted the local branch and fetched it anew. There are several other solutions to this particular problem but the main point was that this actually made me realize how little I know about the way git actually works.

This, however is not a lesson on humility, but rather a collection of the interesting but otherwise possibly obscure, yet sometimes quite useful git commands and resources that I’ve learned through time and that I decided to gather after this strange encounter. Some of them are stupid simple, yet weren’t part of my day-to-day workflow.

  1. git update-index --[no-]assume-unchanged <file-pattern> - to explicitly ignore/un-ignore changes to a file
  2. git filter-branch - useful but dangerous
  3. git commit --allow-empty - for dummy commits for example, for triggering CI
  4. git status --ignored - to see ignored files
  5. git cherry-pick --no-commit <commit> - cherry-pick only the changes, without committing them
  6. git gc - garbage collection, to clean up the repository, useful on large repositories
  7. git-fsck - to check the integrity of the repository
  8. git clean -xdf - when your repository gets too dirty with ignored files
  9. git rebase --committer-date-is-author-date - to preserve the original author date when rebasing

Some useful resources:

  1. learngitbranching.js.org
  2. git-scm.com
  3. Advanced Git
  4. gitignore.io
  5. ohshitgit

While these are all interesting and useful, it’s also worth noting that as in almost any other field, the 80-20 rule applies here as well. In 80% of the cases you won’t use more than 20% of the commands. Looking deep into the abyss of git, you might find it’s significantly less than 20% on a daily basis, but git can achieve a great deal of things. Sometimes it surprises and humbles you, but as a developer, more often than not you’ll probably get by with pushing, pulling, committing, the basics.

This post is licensed under CC BY 4.0 by the author.
Contents