10 More Git Tricks That You Should Know
August 14, 2019
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.
1. Get the date of the earliest commit in a repository
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
2. See the number of commits each person has made in the last month
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'
3. Add all untracked files to .gitignore
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 snipped I found online will add everything that isn’t currently tracked by git into the .gitignore file.
git status | grep -P \"^\\t\" | grep -vF .gitignore | sed \"s/^\\t//\" >> .gitignore"
4. Show all ignored files
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
5. List all unmerged branches branches
This snipped 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
6. List all changes to a specific file, even if it’s been renamed
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>
7. Delete local branches that have been merged into master
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
8. Show the last time each local branch changed
During the lifespan of a project, it’s possible that some branches get abandoned.
The following snipped 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))'
9. Spin a folder out into a new repo
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
10. Add a co-author to your last commit
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 <email@example.com>"
That’s it! 10 More Git Tricks That You Should Know 🤠