Home

Awesome

git-extra-commands

License [Contributors Build Status Awesomebot Megalinter Code Climate Issue Count GitHub stars

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

Table of Contents

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

git-extra-commands is a ZSH plugin that packages some extra git helper scripts I've found. This collection (and the scripts that I wrote in it) is licensed with the Apache Version 2 license.

This collection doesn't actually require ZSH, but packaging it as a ZSH plugin makes it more convenient for people using a ZSH framework to use this collection.

However, some of the scripts in this collection came from other sources and may have different licensing - if they do, their license information is included inline in the script source.

If you wrote one of these scripts and want it removed from this collection, please either make a PR and/or file an issue against the repository and I will remove it.

Included Scripts

ScriptOriginal SourceDescription
git-add-matchGit: programmatic stagingAllows you to use expect to automatically stage hunks matching a search term
git-add-username-remoteRyan Tomayko's dotfilesAdds a remote for the current repository for the given GitHub username.
git-ageKristoffer Gronlund's wikiA git-blame viewer, written using PyGTK.
git-amend-allJohn Wiegley's git scriptsAdds all modified and deleted files, except the new files and adds them to the recent commit by amending it.
git-atticLeah Neukirchen's blogDisplays a list of deleted files in your repository. The output is designed to be copy and pasted: Pass the second field to git show to display the file contents, or just select the hash without ^ to see the commit where removal happened.
git-authorsMichael Markert's dotfiles (as git-changes)List authors in the repository in descending commit-count order.
git-big-fileMislav Marohnić's dotfilesShow files in the repository larger than a threshold size.
git-branch-clean@Hefeweizen"branch clean" - rm various copies of a branch (e.g. foo-wip; origin/foo).
git-branch-dateJoe Block jpb@unixorn.netList branches in commit-date order.
git-branch-diffJoe Block jpb@unixorn.netDiffs your current HEAD with the default branch of the origin remote.
git-branch-nameJoe Block jpb@unixorn.netPrints the current branch name in automation-friendly format.
git-branch-rebaserVengada Rangaraju krangaraju@castlighthealth.comKicks off an interactive rebase of all the commits on your branch. Including pushed commits, so be careful.
git-branch-statusJohn Wiegley's git scriptsColorized status report on all branches in your repository.
git-branches-that-touchMislav Marohnić's dotfilesShows which branches touch files under a path that are remote, unmerged, have a commit in the last six months and whose name doesn't start with 'enterprise-'.
git-capture-wip@HefeweizenCapture in-progress file changes.
git-change-authorMichael Demmer in jut-io/git-scriptsChange one author/email in the history to another.
git-change-logJohn Wiegley's git-scriptsTransform git log output into a complete Changelog for projects that haven't been maintaining one.
git-changesMichael Markert's dotfilesSymlink to git-authors. List authors in the repository in descending commit-count order.
git-checkout-branchesJohn Wiegley's git scriptsChecks out all remote branches.
git-checkout-by-dateStack OverflowLets you checkout several files at once at the version specified by a date.
git-checkout-commitFrom the fzf wikiUses fzf to checkout a commit, showing the commit diff as preview.
git-checkout-default-branchI got tired of keeping track of which repositories use main, master or something else as default branchChecks out the default branch of the origin remote so you don't have to remember which repositories use master, main or whatever.
git-checkout-prBased on gist.github.com/gnarf/5406589Check out a PR locally.
git-checkout-previewFrom the fzf wikiUses fzf to checkout a branch, showing what commits diverge between the branches.
git-checkout-tagJoe Block jpb@unixorn.netCheck out a tag into a branch named after the tag.
git-children-ofJohn Wiegley's git-scriptsShows the children of a given git commit.
git-churnGary Bernhardt's dotfilesShow which files are getting changed most often in the repository.
git-clone-subsetRodrigo Silva (MestreLion) linux@rodrigosilva.comUses git clone and git filter-branch to remove from the clone all files but the ones requested, along with their associated commit history.
git-commaLeah Neukirchen's blogAdds and commits a file in one command.
git-commit-browserFrom the fzf wikiUses fzf to browse commit history.
git-conflictsSeth Messer's bits and bobs repositoryShow files with conflicts.
git-copy-branch-nameZach Holman's dotfilesCopy the current branch's name to the clipboard (macOS Only).
git-creditgit-credit-authorZach Holman's dotfiles
git-current-branchJoe Block jpb@unixorn.netPrints the name of the current branch with no odd characters or formatting, mainly useful in automation scripts.
git-current-commitJoe Block jpb@unixorn.netPrints the hash of the current commit with no odd characters or formatting, mainly useful in automation scripts.
git-cut-branchRyan Tomayko's dotfilesCreate a new branch pointed at HEAD and reset the current branch to the head of its tracking branch.
git-delete-dangling-commitsStack OverflowClean up dangling commits that are not on any branch.
git-delete-local-mergedFrom a deleted post by @tekkubDelete all local branches that have been merged into HEAD.
git-delete-merged-branches?Purges all branches that have been merged to a target branch (defaults to branches merged to master).
git-delete-remote-branchJoe Block jpb@unixorn.netDelete a branch from a specified remote.
git-delete-squashed-and-merged-branchesPaul Irish's dotfilesPurges all branches that have been squashed and merged to a target branch (defaults to branches merged to master). Requires pygithub.
git-delete-tagJoe Block jpb@unixorn.netDelete a tag, both locally and from the origin remote.
git-diff-lastSebastian SchuberthShow the last change made to a file in the repository.
git-divergenceGary Bernhardt's dotfilesShows differences between local branch and its tracking branch.
git-edit-conflictsJoe Block jpb@unixorn.netEdit the files that are marked as conflicted during a merge/rebase in your $EDITOR/$VISUAL.
git-fetch-prsPretty sure I saw this on slack, but can't recall which oneGet all Pull Request branches from a given remote by refspec.
git-filesJake Zimmerman's blogList the files different between the current branch and $REVIEW_BRANCH, which if unset defaults to the repository's default branch.
git-find-dirtyMatthew McCullough's scripts repository.
git-flushJohn Wiegley's git-scriptsCompact your repository by dropping all reflogs, stashes, and other cruft that may be bloating your pack files.
git-force-mtimesJohn Wiegley's git-scriptsSets modification times of all files in the repository their last change date based on git's log. Useful to avoid too new dates after a checkout confusing make or rake.
git-forestJan EngelhardtPrints a text-based tree visualisation of your repository. Requires Git.pm.
git-functionlogJoe Block jpb@unixorn.netAllows you to get a git log of a particular function, not a file.
git-fzf-addFuzzy Finding in Bash with fzfUse fzf to select files to add to git.
git-fzf-log-browserFuzzy Finding in Bash with fzfUse fzf to browse the repository's git log.
git-fzf-pickaxe-browserFuzzy Finding in Bash with fzfUse fzf to display a git log list that has been filtered with pickaxe for a search term.
git-fzf-reflog-browserFuzzy Finding in Bash with fzfUse fzf to browse the repository's git reflog list that can be filtered by entering a fuzzy term at the prompt. Navigation up and down the hash list will preview the changes of each hash.
git-gitJoe Block jpb@unixorn.netTyping git git foo will make git do a git foo instead of complaining.
git-github-open?Open GitHub file/blob page for FILE on LINE.
git-gitlab-mrNoel Cower's gistOpen a merge request on GitLab.
git-history-graphStack OverflowPretty git log, single line per commit, with branch graphing.
git-ignored?Show files being ignored by git in the repository.
git-improved-mergeMislav Marohnić's dotfilesSophisticated git merge with integrated CI check and automatic cleanup.
git-incomingMichael Markert's dotfilesShow commits in the tracking branch that are not in the local branch.
git-jumpCowboy's dotfilesReplays git commits by moving forward / backward through a branch's history.
git-latest-tagJoe Block jpb@unixorn.netFetch tags from all remotes and show the most recent tag name.
git-linesNeil Killeen nkilleen@castlighthealth.comGives you a list of author names with the number of lines last updated by that user in files in the current directory tree with the extension specified.
git-log-single-fileJoe Block jpb@unixorn.netShow the log for a single file.
git-ls-branch-filesJoe BlockList files changed between a specified branch and the current branch.
git-ls-object-refsRyan Tomayko's dotfilesFind references to an object with SHA1 in refs, commits, and trees. All of them.
git-maildiffSanjeev Kumar's blogpostA simple git command to email diff in color to reviewer/ co-worker.
git-make-gitignore#commandline-fu on coffeeopsPrints a language-specific .gitignore file using gitignore.io.
git-maxpackJohn Wiegley's git-scriptsCompress a repository's pack files as much as possible.
git-merged-branchesSergei Boiko's Git housekeeping tutorial: clean-up outdated branches in local and remote repositories articleLists all branches that have been merged.
git-move-commitsCorey Oordt's git-scriptsgit move-commits num-commits correct-branch moves the last n commits to correct-branch (creating it if necessary).
git-neckLeah Neukirchen's blogShow commits from the HEAD until the first branching point. Companion script for git-trail.
git-nukeZach Holman's dotfilesNukes a branch locally and on the origin remote.
git-object-deflateRyan Tomayko's dotfilesDeflate an loose object file and write to standard output.
git-oldest-common-ancestorLee Dohm's dotfilesFinds the oldest common ancestor commit between two branches.
git-open-jira?If the branch name starts with JIRAPROJECT-NNNN, will open that issue in jira. Assumes that your JIRA server location is in ~/.jira-server or in the $JIRA_SERVER environment variable.
git-origin-headDon't recall, maybe twitterPrints the name of the origin remote's default branch. Not every repository uses main or master.
git-outgoingMichael Markert's dotfilesShow commits that are on the local branch that have not been pushed to the tracking branch.
git-overwrittenMislav Marohnić's dotfilesAggregates git blame information about original owners of lines changed or removed in the '<base>...<head>' diff.
git-pie-ifyJeeBak Kim's gistgit pie-ify pattern replacement
git-plotrepoMatthew McCullogh's scripts collectionUses dot to draw a graph of the repository.
git-pr-fetchJoe Block jpb@unixorn.netFetch PR branches by refspec from one of a repository's remotes.
git-pr-listJoe Block jpb@unixorn.netLists pull requests. Requires gh.
git-promoteTrevor's Improving My git Workflow blog post (404 now)Promotes a local topic branch to a remote tracking branch of the same name.
git-prune-branchesMichael Demmer in jut-io/git-scriptsDeletes each fully merged branch after prompting for confirmation, than asks if you want the deleted branches deleted from your upstream remotes.
git-pruneallRyan Tomayko's dotfilesPrune branches from specified remotes, or all remotes when no remote is specified.
git-publishMichael Markert's dotfilesgit publish remote [remote-branch].
git-purge-from-historyDavid Underhill’s blogPermanently delete files or folders from your git repository.
git-pylintJoe Block jpb@unixorn.netRuns pylint on any .py files modified or added in the git status output.
git-rank-contributorsWilliam Morgan wmorgan-git-wt-add@masanjin.netRummages through the changelog and orders contributors by the size of the diffs they're responsible for.
git-rebase-authorsMislav Marohnić's dotfilesAdds authorship info to interactive git rebase output.
git-rebase-theirsRodrigo Silva (MestreLion) linux@rodrigosilva.comResolve rebase conflicts by favoring 'theirs' version.
git-recentMineShows information about most recent commit on all local branches.
git-recently-checkedout-branchesMislav Marohnić's dotfilesShows timestamp and name of recently checked-out branches in reverse chronological order.
git-ref-recentY combinator articleShows the date, branch name, commit hash, and commit subject of branches, from most recently modified to oldest branches.
git-relRyan Tomayko's dotfilesShows the relationship between the current branch and ref. With no ref, the current branch's remote tracking branch is used.
git-relatedMislav Marohnić's dotfilesShow other files that often get changed in commits that touch <file>.
git-release-tagMislav Marohnić's dotfilesCreate a GitHub release for a specified tag. The release notes are automatically populated with the contents of git log since the last tagged version.
git-remote-default-branchMineShows the default branch for a specified remote, defaults to origin when no remote is specified.
git-remove-conflicts ours or theirs FILES...John Wiegley's git scriptsAutomatically resolves conflicts by applying the changes from current branch (ours) or remote branch (theirs).
git-rename-branchesRodrigo Silva (MestreLion) linux@rodrigosilva.comRename multiple branches that start with a given name.
git-replace-authorStack OverflowUses git-filter-branch to rewrite all commits with one name to use another name and email.
git-reset-with-fireJoe Block jpb@unixorn.netHard reset the working directory, then zap any files not known to git.
git-restore-mtimeRodrigo Silva (MestreLion) linux@rodrigosilva.comChange mtime of files based on commit date of last change.
git-reupZach Holman's dotfilesLike git pull but show a short and sexy log of changes after merging or rebasing.
git-rm-deleted-from-repoJoe Block jpb@unixorn.netRemoves files you deleted with rm from the repository for you.
git-root-directoryJoe Block jpb@unixorn.netPrints the path to the root of the git repository you're in.
git-run-command-on-revisionsGary Bernhardt's dotfilesRuns a given command over a range of git revisions.
git-semversDaniel HoherdList all the tags in a repo that are semver compliant
git-shamendDanielle Sucher's git-shamend blog postAmends your staged changes as a fixup (keeping the pre-existing commit message) to the specified commit, or HEAD if no revision is specified.
git-show-overwrittenMislav Marohnić's dotfilesAggregates git blame information about the original owners of lines changed or removed in the '<base>...<head>' diff.
git-shrink-repoBased on gimbo/gimbo-git.zshShrinks your clone of a git repository.
git-side-by-sideGreg V's vmware-archive/git_scripts@hefeweizen on the coffeeops slack
git-spA. Schwarz's git-sp"Simple push", single short command to commit, and push. Use -a flag to add all files to commit.
git-srNoel Cower's git-srUse fzf to switch to a different git ref.
git-statsJake Zimmerman's blogDisplays stats for the files different between the current branch and $REVIEW_BRANCH, which if unset defaults to the repository's default branch.
git-submodule-lsList all submodules in your project
git-submodule-pathsgit-submodule --helpShow path to all submodules in your checkout, with their current commit SHA
git-submodule-rmGreg V's dotfiles & Pascal SommerAllows you to remove a submodule easily with git submodule-rm path/to/submodule.
git-superpullGreg V's vmware-archive/git_scriptsPulls, then does a git submodule init and git submodule update.
git-switch-branchAndrew Steele's dotfilesMake it easier to switch to a branch by a substring of its name. More useful if you are good about deleting branches which have been merged upstream and if your branch names include unique identifiers like ticket/issue numbers or feature names.
git-tag-and-sign?Create and sign a new tag.
git-tag-diffJohn Wiegley's git-scriptsShow the differences between local tags and ones on the remote, if any.
git-thanksMislav Marohnić's dotfilesList the contributors to a repository in descending commit order, even if their contribution has been completely replaced.
git-trackZach Holman's dotfilesSets up your branch to track a remote branch. Assumes you mean origin/localbranchname.
git-trailLeah Neukirchen's blogShow all branching points in the repository's git history so you can see how to reach commits in the current branch from other branches.
git-undelete?Undeletes a file.
git-undo-push?Undoes your last push to branch $1 of origin.
git-undoStack OverflowUndoes your last commit but keeps the changes in place.
git-unpushedZach Holman's dotfilesShow the diff of everything you haven't pushed to the origin remote yet.
git-unreleasedMislav Marohnić's dotfilesShows git commits since the last tagged version.
git-upZach Holman's dotfilesLike git pull but show a short and sexy log of changes after merging or rebasing.
git-upstream-nameJoe Block jpb@unixorn.netPrint the name of the current branch's upstream.
git-upstream-syncJoe Block jpb@unixorn.netFetches upstream/yourforkname and rebases it into your local fork, then pushes to your origin remote.
git-what-the-hell-just-happenedGary Bernhardt's dotfilesShow what just happened.
git-when-mergedMichael Haggerty git-when-mergedFind when a commit was merged into one or more branches.
git-where-prMislav Marohnić's dotfilesOpens the Pull Request on GitHub where a specified commit originated.
git-whereMislav Marohnić's dotfilesShows where a particular commit falls between releases.
git-whoamiPeter EisentrautShows what username & email you have configured for the repository you're in.
git-winnerGarry Dolley https://github.com/up_the_irons/git-winnerShows what authors have made the most commits, both by number of commits and by number of lines changed.
git-wordinessNoel CowerShows how wordy people's commit messages are. Useful for shaming the folks who commit atrocities like git commit -m fixup.
git-wtfWilliam Morgan wmorgan-git-wt-add@masanjin.netgit-wtf displays the state of your repository in a readable, easy-to-scan format. It's useful for getting a summary of how a branch relates to a remote server, and for wrangling many topic branches.

Useful git aliases

Here are some helpful aliases for your ~/.gitconfig

aliasDescription
ahead-of-master = log --oneline origin/master..HEADShow commits that haven't made it to master yet.
fetch-pull-requests = fetch origin '+refs/pull/*/head:refs/remotes/origin/pull/*'Fetch pull requests from GitHub so you can git checkout pull/123 and test them locally.
roots = log --all --oneline --decorate --max-parents=0Show the root commits.
unpushed = log @{u}..Show which commits have not been pushed to the tracking branch and are safe to amend/rebase.
work-in-progress = rebase -i @{u}Starts an interactive rebase of all the commits you haven't pushed yet. Handy for collapsing a bunch of work-in-progress snapshots into logical commits before pushing, without having to worry about accidentally squashing a commit someone else has already referred to.

Installing

Pre-requisites

Zgenom

If you're using Zgenom:

  1. Add zgenom load unixorn/git-extra-commands to your .zshrc along with your other zgenom load commands.
  2. zgenom reset && zgenom save

Antigen

If you're using Antigen:

  1. Add antigen bundle unixorn/git-extra-commands@main to your .zshrc where you've listed your other plugins.
  2. Close and reopen your Terminal/iTerm window to refresh context and use the plugin. Alternatively, you can run antigen bundle unixorn/git-extra-commands in a running shell to have antigen load the new plugin.

oh-my-zsh

If you're using oh-my-zsh:

  1. Clone the repository into a new git-extra-commands directory in oh-my-zsh's plugin folder:

git clone https://github.com/unixorn/git-extra-commands.git $ZSH_CUSTOM/plugins/git-extra-commands

  1. Edit your ~/.zshrc and add git-extra-commands – same as clone directory – to the list of plugins to enable:

plugins=( ... git-extra-commands )

  1. Then, restart your terminal application to refresh context and use the plugin. Alternatively, you can source your current shell configuration:

source ~/.zshrc

Bash / Manual Installation

Nothing here actually requires you to use ZSH or zgen, that's just a convenient distribution method for anyone using a ZSH framework.

If you aren't using any ZSH frameworks, or if you're using bash, fish or another shell, do the following steps:

  1. git clone this repository
  2. Add cloneDirectory/bin to your $PATH in your shell's startup file.

Other useful git stuff

Articles / Blog posts / Books

External Git Utilities

Miscellaneous Tips

Make it easier to check out default branch

Many repositories are switching away from using master as the default branch name. You can do git config --global alias.co-default '!'"git checkout \$(git branch -r | awk -F/ '/HEAD/ {print \$NF}')" to add a co-default alias that will determine what the repository's default branch is for you.

Alternatively, add the following aliases from a tweet by @jnesselr to your .gitconfig file:

cdef = "!git checkout $(git originhead)"
originhead = "!git remote show origin | grep 'HEAD branch' | cut -d ' ' -f5"

This queries the remote to get the current up to date default branch, so it will work even if the remote's default branch changes after you did your initial checkout.

Have git cope with typos

Do git config --global help.autocorrect 1

git will now try to figure out what you meant, for example:

$ git commt
WARNING: You called a Git command named 'commt', which does not exist.
Continuing in 0.1 seconds, assuming that you meant 'commit'.

Rewrite git:// with https://

git config --global url."https://github".insteadOf git://github

or replace with ssh

Use ssh instead of https://

git config --global url."git@github.com:".insteadOf "https://github.com/"

Credit: @grawity & @hansdg1 by way of @Kovrinic's gist

Contributing

Thanks

Many thanks to all the contributors over the years.

<a href="https://github.com/unixorn/git-extra-commands/graphs/contributors"> <img src="https://contributors-img.web.app/image?repo=unixorn/git-extra-commands" /> </a>

Made with contributors-img.