Useful Git Commands

Pablo Garcia
3 min readMar 24, 2022

This is my personal “uncommon but very useful git commands” cheat sheet. I use the command line constantly and I prefer not to use aliases unless I must. But what is “uncommon”? Well, anything that I don’t use that often and thus tends to be forgotten.

git stash | by name

Stash changes using a name:

git stash push -m "name"
git stash apply $(git stash list | grep -m 1 "name" | cut -d: -f1)

Because the apply command is long, you can use these functions by adding them to your .zshrc as seen here.

function gitstashall() {
git add .
git stash push -m "zsh_stash_name_$1"
}
function gitstashapply() {
git stash apply $(git stash list | grep -m 1 "zsh_stash_name_$1" | cut -d: -f1)
git reset
}

git log | search

If you need to find a specific string through the git history, instead of git bisect, we can run:

git log --color -p -S "whatever you want"
git log --color -p -S whatever_you_want

In this case it will look for all logs (including diffs) where the string “whatever you want” or “whatever you want” was found.

You can also reverse if you want the first commit that made the change: https://stackoverflow.com/a/31621921/4984618

Or even look for source through branches by using the --all parameter (start from every branch) and the --source parameter (show which of those branches led to finding that commit): https://stackoverflow.com/a/5816177/4984618

git log | omit (filter out) author

git log --no-merges --invert-grep --author=l10 --pretty=oneline --abbrev-commit release...develop > tmp.txt

This will filter out l10 commits (by using --invert-grep) and merges between develop and release. You can also use different --pretty flags, e.g. --pretty=format:"[%h — %an] %s:

git log --no-merges --invert-grep --author=l10 --pretty=format:"[%h - %an] %s" --abbrev-commit release...develop > tmp.txt

git -c | change committer

git -c "user.name=Your Name" -c "user.email=Your email"

This is helpful when rebasing or cherry-picking someone else's branch:

git -c "user.name=John Doe" -c "user.email=jdoe@email.com" cherry-pick f125e1d11a08aae09173302cb72b90f5ad3eb4b3

Or when needing to change the coauthor for the last commit:

git -c "user.name=John Doe" -c "user.email=jdoe@email.com" commit --amend --no-edit

git show | full commit

git show b03623227ed1264e3cac4e6bb4878d96b91aa484 --pretty=fuller

This is helpful when you want to see the coauthor and more information from the commit.

git filter-branch | fix a user across commits

Use bash script in: https://help.github.com/en/github/using-git/changing-author-info

git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "pgarciacamou@paypal.com" ];
then
GIT_AUTHOR_NAME="Pablo Garcia";
GIT_AUTHOR_EMAIL="pgarciacamou@gmail.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' ff58e08379170f0d306f2c3b74b3571e67fe8973..HEAD

git commit | add co-authors

git commit -m "feat: Add Open Banking featureCo-authored-by: John Doe <jdoe@email.com>
Co-authored-by: Jane Doe <jane@email.com>
Co-authored-by: Pablo Garcia <pgarciacamou@gmail.com>"

Notice: that this is a multi-line commit with a trailing " at the end of the commit.

git check-ignore

Use git check-ignore -v <pattern> if a file is being ignored when adding it, e.g.

$ git add Icon.svg
The following paths are ignored by one of your .gitignore files:
Icon.svg
Use -f if you really want to add them.
$ git check-ignore -v client/src/icons/Icon.svg.js
.gitignore:4:Icon? client/src/icons/Icon.svg.js

This tells you which line the file is being ignored by and the gitignore which is ignoring it. In the case above, the file started with Icon (i.e. regex: /^Icon.*$/), which was causing to ignore it.

See: https://stackoverflow.com/questions/9436405/git-is-ignoring-files-that-arent-in-gitignore

--

--

Pablo Garcia

Staff Sofware Architect 2 at PayPal, M.S. in Computer Science w/specialization in Computing Systems, B.Eng. in Computer Software.