Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A suite of useful Git commands that aid with scripting or every day command line usage

License

NotificationsYou must be signed in to change notification settings

fox-forks/git-toolbelt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

git-toolbelt logo

Installation instructions

$ brew tap nvie/tap$ brew install nvie/tap/git-toolbelt

If not using Homebrew, you will need to haveGNU coreutilsinstalled, for therealpath utility. Git for Windows users see#29.

git-toolbelt

Helper tools to make everyday life with Git much easier. Commands marked with⭐️ are my personal favorites and are commands I use almost every day.

Everyday helpful commands:

Statistics:

Commands to help novices out:

Commands that simplify scripting. These commands typically only return exitcodes and have no output.

Advanced usage:

git current-branch

Returns the name of the current branch, if any. Why doesn't this come with git?

$git current-branchmaster

Alias togit rev-parse --abbrev-ref HEAD.

git main-branch

Returns the name of the default main branch for this repository. Historicallymaster, but could also bemain if you've changed the default branch name.Since there's no way of reliably telling what the default branch name is fora repo, this script will probe for the existence of local branches named eithermain ormaster. The first one found is used.

$git main-branchmaster

git sha

Returns the SHA value for the specified object, or the current branch head, ifnothing is provided.

$git sha<some-object>

Typical example:

$git sha HEADf688d7543c5d52f5f78b3db1b0dd1616059299a4$git sha -s HEADf688d75

Shows the commit SHA for the latest commit.

git modified

Returns a list of locally modified files. In contrast to git status, it doesnot include any detailed file status, and never includes non-existing files.

This makes it ideal for the following use-case:

$vim (git modified)

If you want to locally modified files that are already staged, too, use:

$vim (git modified -i)

git modified-since

Like git-modified, but for printing a list of files that have been modifiedsince master (or whatever commit specified). In contrast to git status, itdoes not include any detailed file status, and never includes non-existingfiles.

Opens all files modified on your branch (since you branched offmaster).

$vim (git modified-since)

git separator

Adds a commit with a message of only ---'s, so that it visually separatescommits in the history. This is incredibly useful when doing more complexrebase operations. (They should be used as a temporary measure, and ideallytaken out of the history again when done rebasing.)

git spinoff

Inspired by Magit'sspinoff command. Creates and checks outa new branch starting at and tracking the current branch. Thatbranch in turn is reset to the last commit it shares with itsupstream. If the current branch has no upstream or no unpushedcommits, then the new branch is created anyway and the previouslycurrent branch is not touched.

This is useful to create a feature branch after work has alreadybegan on the old branch (likely but not necessarily "master").

git push-current

Pushed the current branch out toorigin, and makes sure to setup tracking ofthe remote branch. Shorthand forgit push -u origin <current-branch>.

Accepts options, too, so you can use

$git push-current -f

to force-push.

git is-headless

Tests ifHEAD is pointing to a branch head, or is detached.

git diff-since

Shows the differences made on the current branch, compared to the main branch(or the given branch).

git local-branches / git remote-branches / git active-branches

Returns a list of local or remote branches, but contrary to Git's defaultcommands for this, returns them machine-processable. In the case of remotebranches, can be asked to return only the branches in a specific remote.

Forgit active-branches, a branch is deemed "active" if its head points toa commit authored in the last 3 weeks, by default. An arbitrary date can bespecified using eithergit active-branches -s <date> or-a <date>(mnemonic: "since" or "after"), using any date formatsupported bygit log.

git local-branch-exists / git remote-branch-exists / git tag-exists

Tests if the given local branch, remote branch, or tag exists.

git recent-branches

Returns a list of local branches, ordered by recency:

$ git recent-branchesfoomasterbarqux

git remote-tracking-branch

Print the name of the remote tracking branch of the current orgiven local branch name, if one exists. Errors otherwise.

git local-commits / git has-local-commits

Returns a list of commits that are still in your local repo, but haven't beenpushed toorigin.git has-local-commits is the scriptable equivalent thatonly returns an exit code if such commits exist.

git contains / git is-ancestor

Tests if X is merged into Y:

$ git contains X Y  # does X contain Y?$ git is-ancestor X Y  # is X an ancestor of Y?

CAVEAT:Even though they might look like opposites,X contains Y does not meannot (X is-ancestor Y), since (1) X and Y can point to the same commit, or thebranches may have no common history and thus be unrelated completely.

git stage-all

Mimics the index / staging area to match the working tree exactly. Adds files,removes files, etc.

Alias togit add --all.

git unstage-all

Unstages everything. Leaves the working tree intact.

Alias togit reset HEAD.

git undo-merge

Ever created a merge accidentally, or decided that you didn't want to mergeafter all? You can undo the last merge usinggit undo-merge.

git undo-commit

Ever committed too soon, or by accident? Or on the wrong branch? You can nowundo your last commit and you won't lose any data. All the changes in thecommit will be staged (like right before the commit) and the commit itself isgone.

git cleanup

Deletes all branches that have already been merged into the main branch. Keepsother branches lying around. Removes branches both locally and in the originremote. Will be most conservative with deletions.

git fixup

Amend all local staged changes into the last commit. Ideal for fixing typo's,when you don't want to re-edit the commit message.

$ git commit -m "Something cool."$ vim somefile.txt  # fix typo$ git add somefile.txt$ git fixup  # merge this little change back into the last commit

git fixup-with

Interactively lets you pick a commit to fixup with. (Usesfzf for theinteractive picking. Usebrew install fzf to install this tool separately.)Use-r to trigger an interactive rebase right afterwards.

git workon

Convenience command for quickly switching to a branch<name>. If such localbranch does not exist, but there is a remote branch namedorigin/<name>, thena local branch is created and the remote is tracked.

git delouse

Say you want to rebuild your last commit, but want to keep the commit message.git delouse empties the last commit on the current branch and places allchanges back into the working tree.

Since the commit remains in history, you can now rebuild the commit by "gitamend"'ing or "git fixup"'ing, instead of making new commits.

git shatter-by-file

Splits the last commit into N+1 commits, where N is the number of files in thelast commit. The first commit is an empty commit with the original commitmessage and author details, and the following commits add (or delete) one fileeach, keeping the subject line of the original commit message.

After runninggit shatter-by-file, you'll typically want to rungit rebase --interactive to start fixing up changes to files, etc. For that purpose, theoriginal commit message is kept in there (in the empty first commit), so makesure to use it.

git cleave

Splits the last commit into 2 or more commits. Takes one or more regex values(which are fed togrep -Ee), and will split the last commit by file pathsmatching each of the regexes.

For example:

$ git cleave client/ server/

Will split the last commit into 2 (or 3) commits. The first one will containall the files containingclient/, the second will contain all the filesmatchingserver/. If there are files that don't match either of those, thena 3rd commit will be made with the "remainder".

Another example:

$ git cleave '.*\.js$'

This will split off all Javascript files from a commit.

git commit-to

Ever been on a branch and really wanted to quickly commit a change toa different branch? Given that this is possible without merge conflicts, gitcommit-to will allow you to do so, without checking out the branch necessarily.

$ git branch  master* mybranch$ git statusM foo.txtM bar.txt$ git add foo.txt$ git commit-to master -m "Add foo to master."$ git add bar.txt$ git commit -m "Add bar to mybranch."

git cherry-pick-to

Every been on a branch, just made a commit, but really want that commitavailable on other branches as well? You can now cherry-pick this commit to anybranch, staying on the current branch. (Given the change won't lead to a mergeconflict.)

$ git branch  master* mybranch$ git add foo.txt$ git commit -m "Really useful thing."$ git cherry-pick-to master HEAD$ git branch  # did not switch branches  master* mybranch

git is-repo

Helper function that determines whether the current directory has a Git repoassociated to it. Scriptable equivalent ofgit repo.

git root / git repo

git root prints the root location of the working tree.

$ cd /path/to/worktree$ cd some/dir/in/worktree$ pwd/path/to/worktree/some/project/dir$ git root/path/to/worktree

git repo prints the location of the Git directory, typically.git, butcould differ based on your setup. Will return with a non-zero exit code if notin a repo.

$ cd /path/to/my/worktree$ git repo.git$ cd /tmp$ git repofatal: Not a git repository (or any of the parent directories): .git

git initial-commit

git initial-commit prints the initial commit for the repo.

$ git initial-commit48c94a6a29e9e52ab63ce0fab578101ddc56a04f

git has-local-changes / git is-clean / git is-dirty

Helper function that determines whether there are local changes in the workingtree, by returning a 0 (local changes) or 1 (no local changes) exit code.

git drop-local-changes

Don't care about your local working copy's state and really want to revert backto whatever is recorded in the history? git drop-local-changes lets you dothis.

This covers aborting rebases, undoing partial merges, resetting the index andremoving any unknown local files from the work tree. Anything that is alreadycommitted remains safe.

??? issue a git pull, too? Typical beginners will want this.

git stash-everything

The stash behaviour you (probably) always wanted. This actually stasheseverything what's in your index, in your working tree, and even stashes awayyour untracked files, leaving a totally clean working tree.

Using "git stash pop" will recover all changes, including index state, locallymodified files, and untracked files.

git update-all

Updates all local branch heads to the remote's equivalent. This is the same aschecking out all local branches one-by-one and pulling the latest upstreamchanges. Will only update if a pull succeeds cleanly (i.e. is a fast-forwardpull).

git-merged / git-unmerged / git-merge-status

This trio of subcommands makes it easy to inspect merge status of localbranches. Use them to check whether any local branches have or haven't beenmerged into the target branch (defaults to master).

git-merge-status is a useful command that presents both lists in a singleoverview (not for machine processing).

git-branches-containing

This command, "git branches-containing []" returns a list of brancheswhich contain the specified '' (defaults to 'HEAD').

git-branches-containing is useful to see if a branch has been merged, and,if so, which releases contain the feature/fix (if you use releasebranches).

git-committer-info

Shows contribution stats for the given committer, like "most productive day","most productive hour", "average commit size", etc.

TODO: git force-checkout

Don't care about your local working copy's state and really want to switch toanother branch? git force-checkout lets you do this.

Switching branches can be prevented by git. For good reasons, mostly. Git isdesigned to prevent you from losing data potentially. Examples are there arelocal unmerged files, or some files that would be overwritten by doing thecheckout.

By using force-checkout you basically give git the finger, and check outa branch anyway.You do agree to lose data when using this command.

$ git checkout mastererror: Your local changes to the following files would be overwritten by checkout:    foo/bar.txtPlease, commit your changes or stash them before you can switch branches.Aborting$ git force-checkout masterSwitched to branch 'master'

git conflicts

Generates a summary for all local branches that will merge uncleanly—i.e. willlead to merge conflicts later on.

$ git branch  develop* mybranch  master  other-branch$ git conflictsdevelop... merges cleanlymaster...  merges cleanlyother-branch... CONFLICTS AHEAD

git-skip / git-unskip / git-show-skipped

Git supports marking files "skip worktree", meaning any change in the filelocally will not be shown in status reports, or be added when you stage allfiles. This feature can be useful to toggle some switches locally, orexperiment with different settings, without running the risk of accidentallycommitting this local data (that should remain untouched in the repo).

Notice that status reports won't show these files anymore, so it's also easilyto lose track of these marked assumptions, and you probably run into weirdissues if you don't remember this. (This is the reason why I put these scriptsin the "advanced" category.)

Basic usage:

$ git status M foo.txt M bar.txt M qux.txt$ git skip foo.txt$ git status M bar.txt M qux.txt$ git show-skippedfoo.txt$ git commit -am 'Commit everything.'$ git statusnothing to commit, working directory clean$ git is-clean && echo "clean" || echo "not clean"not clean$ git unskip -a$ git status M foo.txt

As you can see,git-is-clean is aware of any lurking "skipped" files, andwon't report a clean working tree, as these assumed unchanged files often blockthe ability to check out different branches.

git wip

Commits all local changes under a commit message of "WIP". Great for quicklycreating "savepoint" commits. If there is a mix of staged changes, andunstaged changes, and new files, will commit each of these as a separatecommit, all titled "WIP". Effectively, runninggit-wip once will potentiallylead to anywhere between 0 and 3 "WIP" commits being created.

About

A suite of useful Git commands that aid with scripting or every day command line usage

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell100.0%

[8]ページ先頭

©2009-2025 Movatter.jp