Cover Image: Two People Holding Macbook Pro by Christina Morillo
I recently wrote an article called 10 Git Tricks to Save Your Time and Sanity. People seemed to like it, so here are a few more git tricks that are saved as aliases on my machine.
Occasionally, when I want to see exactly how old an Open Source project is, or I'm trying to figure out when a repo was made, I ask git to tell me when the first commit was made.
git log --date-order --format=%cI | tail -1
This is something that I generally don't need, but occasionally I am stuck on a project and need to figure out who I should ask for help.
The easiest way for me to do that is to figure out who worked on the repository most recently. With this command, you can swap out
--since='1 month ago' to whatever timeframe you think makes the most sense.
git shortlog --summary --since='1 month ago'
Sometimes, when you're setting up a new git repository, it makes sense to dump a bunch of stuff into the
.gitignore folder and it can be tedious to type all of it.
This snippet I found online will add everything that isn't currently tracked by git into the .gitignore file.
| grep -P \"^\\t\"
| grep -vF .gitignore
| sed \"s/^\\t//\" >> .gitignore"
The next most obvious thing you might want to do is list those ignored files.
That's exactly what the next snippet does:
git ls-files --others -i --exclude-standard
This snippet lists every branch that hasn't been merged. You can remove the
-a flag if you don't want to include branches on your remotes.
I use this on Solidus extensions to make sure there aren't any extra branches lying around that contain abandoned work.
git checkout master && git branch -a --no-merged
This is a super handy trick. With this command, you can see all of the commits that have touched a file.
Because git is awesome, it will show you the changes to that file even if the name has been changed at some point in its history.
git log --follow -p -- <file_path>
If you find that you have a lot of random branches on your machine that are no longer needed, you can practice good git hygiene by deleting them.
This snippet will check for any branches that have been merged into master and delete them for you automatically.
Be careful with this one, if you are afraid of deleting old branches.
git checkout master
&& git branch --merged master
| grep -v "master"
| xargs -n 1 git branch -d
During the lifespan of a project, it's possible that some branches get abandoned.
The following snippet is a quick way to figure out if you have a branch locally that hasn't been updated in a long time.
git for-each-ref --sort=committerdate refs/heads/
--format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - (%(color:green)%(committerdate:relative)%(color:reset))'
If your project is sufficiently complex, you can use this command to spin a new repo out of a directory in your present repository.
After running this command, you'll probably want to push up your changes to a remote for the new repository.
git filter-branch --prune-empty --subdirectory-filter <folder_name> master
GitHub and GitLab support adding co-authors to your commit when more than one person works on a commit.
Doing this should link the commit on GitHub to multiple authors.
OLD_MSG=$(git log --format=%B -n1)
&& git commit --amend -m "$OLD_MSG"
-m "Co-authored-by: jacobherrington <firstname.lastname@example.org>"
That's it! 10 More Git Tricks That You Should Know 🤠
I'd love to hear about more neat git tricks or alternative approaches to the stuff I've shared here in the comments! 👇