DEV Community

Cover image for 10 More Git Tricks That You Should Know
Jacob Herrington (he/him)
Jacob Herrington (he/him)

Posted on • Updated on

10 More Git Tricks That You Should Know

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.

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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 snippet 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5. List all unmerged branches branches

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 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))'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 <jacobherringtondeveloper@gmail.com>"
Enter fullscreen mode Exit fullscreen mode

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! 👇

Top comments (13)

Collapse
 
vimmer9 profile image
Damir Franusic

I use the following scripts to generate some common files. Although they work, you will probably need to tweak them to meet your needs.

CHANGELOG

#!/bin/bash
git log  --decorate --tags --no-walk > CHANGELOG 2>&1 || { echo "git repository not found!" > CHANGELOG; }

VERSION

#!/bin/bash

# Tag revisions like this:
# $ git tag -a -m "Version 0.2" v0.2 HEAD

VF=VERSION
DEFAULT_VERSION=v1.0

LF='
'
TAG_TYPE="v*"

# First see if there is a version file (included in release tarballs),
# then try git-describe, then default.
if test -d .git -o -f .git &&
    VN=$(git describe --abbrev=4 --long --match=$TAG_TYPE HEAD 2>/dev/null) &&
    case "$VN" in
    *$LF*) (exit 1) ;;
    v[0-9]*)
        git update-index -q --refresh
        test -z "$(git diff-index --name-only HEAD --)" ||
        VN="$VN-mod" ;;
    esac
then
        continue
    #VN=$(echo "$VN" | sed -e 's/-/./g');
else
    VN="$DEFAULT_VERSION"
fi

#VN=$(expr "$VN" : v*'\(.*\)')

# Show the version to the user via stderr
echo >&2 "$VN"

# Parse the existing VERSION-FILE 
if test -r $VF
then
    VC=$(sed -e 's/^version: //' <$VF)
else
    VC=unset
fi

# If version has changed, update VERSION-FILE
test "$VN" = "$VC" || {
    echo "$VN" >$VF
    echo >&2 "($VF updated)"
}

AUTHORS

#!/bin/bash
echo -e "Original Author\n===============\nDamir Franusic <damir.franusic@gmail.com>" > AUTHORS
CONTRIBUTORS=`git log --format='%aN <%aE>' | sort -f | uniq | grep -v "Damir Franusic"`
if [ ! -z "$CONTRIBUTORS" ]; then
    echo -e "\nContributors\n============\n$CONTRIBUTORS" >> AUTHORS
fi
Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Wow! Thanks for sharing 👌

Collapse
 
alikhani97 profile image
Mahdi Alikhani

Thanks for sharing !

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

No problem, hope it's useful 🤠

Collapse
 
alikhani97 profile image
Mahdi Alikhani

it's useful for me a lot, 🤠.

Collapse
 
matthewpersico profile image
Matthew O. Persico

1) You have a couple of typos "snipped" instead of "snippet".
2) $7 is nice, but what I really want is a list of branches that are part of Pull Requests that have been remotely merged. I think you have to use a GitHub API to do that, though.

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Thanks for the typo callout!

Yeah, I'm not sure on that one. Maybe hub could be helpful?

Collapse
 
nqthqn profile image
nqthqn

I like the co author trick

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

It's really useful! I pair a lot so it's nice to be able to include others on the commits we write together.

Collapse
 
vlasales profile image
Vlastimil Pospichal • Edited
Collapse
 
jacobherrington profile image
Jacob Herrington (he/him)

Thanks for sharing!

Collapse
 
parinay profile image
parinay

Thanks !

Collapse
 
mxoliver profile image
Oliver

Oh man that command to create a new repo from a directory is a god send. Thanks so much for writing this