一些实用的 git 扩展命令
来自 https://github.com/tj/git-extras/blob/master/Commands.md
git abortgit aliasgit archive-filegit authorsgit browsegit browse-cigit bulkgit brvgit changeloggit cleargit clear-softgit coauthorgit commits-sincegit contribgit countgit cpgit create-branchgit delete-branchgit delete-merged-branchesgit delete-squashed-branchesgit delete-submodulegit delete-taggit deltagit effortgit extrasgit featuregit force-clonegit forkgit fresh-branchgit gh-pagesgit graftgit guiltgit ignoregit ignore-iogit infogit local-commitsgit lockgit lockedgit magicgit merge-intogit merge-repogit missinggit mrgit obliterategit pastegit prgit psykorebasegit pull-requestgit reauthorgit rebase-patchgit releasegit rename-branchgit rename-taggit rename-remotegit replgit reset-filegit rootgit rscpgit scpgit sedgit setupgit show-merged-branchesgit show-treegit show-unmerged-branchesgit stampgit squashgit standupgit summarygit syncgit touchgit undogit unlockgit utimes
git extras
The main git-extras command.
Output the current --version:
$ git extras --version
List available commands:
$ git extras --help
Update to the latest git-extras:
$ git extras update
git gh-pages
Sets up the gh-pages branch. (See GitHub Pages documentation.)
git feature
Create/Merge the given feature branch name:
$ git feature dependencies
To Setup a remote tracking branch:
$ git feature dependencies -r upstream
Note: If no remote name is passed with the -r option, it will push to origin.
Afterwards, the same command will check it out:
$ git checkout master
$ git feature dependencies
When finished, we can feature finish to merge it into the current branch:
$ git checkout master
$ git feature finish dependencies
Note: If a remote is setup to track the branch, it will be deleted.
git contrib
Output author's contributions to a project:
$ git contrib visionmedia
visionmedia (18):
Export STATUS_CODES
Replaced several Array.prototype.slice.call() calls with Array.prototype.unshift.call()
Moved help msg to node-repl
Added multiple arg support for sys.puts(), print(), etc.
Fix stack output on socket error
...
git summary
Outputs a repo or path summary:
$ git summary
project : git-extras
repo age : 10 months ago
last active : 3 weeks ago
active on : 93 days
commits : 163
files : 93
uncommitted : 3
authors :
97 Tj Holowaychuk 59.5%
37 Jonhnny Weslley 22.7%
8 Kenneth Reitz 4.9%
5 Aggelos Orfanakos 3.1%
3 Jonathan "Duke" Leto 1.8%
2 Gert Van Gool 1.2%
2 Domenico Rotiroti 1.2%
2 Devin Withers 1.2%
2 TJ Holowaychuk 1.2%
1 Nick Campbell 0.6%
1 Alex McHale 0.6%
1 Jason Young 0.6%
1 Jens K. Mueller 0.6%
1 Guillermo Rauch 0.6%
This command can also take a commitish, and will print a summary for commits in the commitish range:
$ git summary v42..
This command can also take an options --line, will print a summary by lines
$ git summary --line
project : git-extras
lines : 8420
authors :
2905 Tj Holowaychuk 34.5%
1901 Jonhnny Weslley 22.6%
1474 nickl- 17.5%
653 Leila Muhtasib 7.8%
275 Tony 3.3%
267 Jesús Espino 3.2%
199 Philipp Klose 2.4%
180 Michael Komitee 2.1%
178 Tom Vincent 2.1%
119 TJ Holowaychuk 1.4%
114 Damian Krzeminski 1.4%
66 Kenneth Reitz 0.8%
22 Not Committed Yet 0.3%
17 David Baumgold 0.2%
12 Brian J Brennan 0.1%
6 Leandro López 0.1%
6 Jan Krueger 0.1%
6 Gunnlaugur Thor Briem 0.1%
3 Hogan Long 0.0%
3 Curtis McEnroe 0.0%
3 Alex McHale 0.0%
3 Aggelos Orfanakos 0.0%
2 Phally 0.0%
2 NANRI 0.0%
2 Moritz Grauel 0.0%
1 Jean Jordaan 0.0%
1 Daniel Schildt 0.0%
The --line option can also take a path, which will print a filtered summary for that folder or file.
git effort
Displays "effort" statistics, currently just the number of commits per file, showing highlighting where the most activity is. The "active days" column is the total number of days which contributed modifications to this file.
node (master): git effort --above 15 {src,lib}/*
If you wish to ignore files with commits <= a value you may use --above:
$ git effort --above 5
If you wish to see only the commits in the last month you may use --since (it supports the same syntax like git log --since):
$ git effort -- --since='last month'
By default git ls-files is used, however you may pass one or more files to git-effort(1), for example:
$ git effort bin/* lib/*
git bulk
git bulk adds convenient support for operations that you want to execute on multiple git repositories.
- simply register workspaces that contain multiple git repos in their directory structure
- run any git command on the repositories of the registered workspaces in one command to
git bulk - use the "guarded mode" to check on each execution
usage: git bulk [-g] ([-a]|[-w <ws-name>]) <git command>
git bulk --addworkspace <ws-name> <ws-root-directory> (--from <URL or file>)
git bulk --removeworkspace <ws-name>
git bulk --addcurrent <ws-name>
git bulk --purge
git bulk --listall
Register a workspace so that git bulk knows about it (notice that <ws-root-directory> must be absolute path):
$ git bulk --addworkspace personal ~/workspaces/personal
With option --from the URL to a single repository or a file containing multiple URLs can be added and they will be cloned directly into the workspace. Suitable for the initial setup of a multi-repo project.
$ git bulk --addworkspace projectX ~/workspaces/projectx --from https://github.com/x/project-x.git
# OR with a file containing many repositories on each line:
$ git bulk --addworkspace projectX ~/workspaces/projectx --from ~/workspaces/repositories.txt
with repositories.txt be like:
https://github.com/x/project-x-1.git
https://github.com/x/project-x-2.git
https://github.com/x/project-x-3.git
Register the current directory as a workspace to git bulk
$ git bulk --addcurrent personal
List all registered workspaces:
$ git bulk --listall
bulkworkspaces.personal /Users/niklasschlimm/workspaces/personal
Run a git command on the repositories of the current workspace:
$ git bulk fetch

Run a git command on one specific workspace and its repositories:
$ git bulk -w personal fetch
Run a git command on all workspaces and their repositories:
$ git bulk -a fetch
Run a git command but ask user for confirmation on every execution (guarded mode):
$ git bulk -g fetch
Remove a registered workspace:
$ git bulk --removeworkspace personal
Remove all registered workspaces:
$ git bulk --purge
git brv
Pretty listing of branches sorted by the date of their last commit.
$ git brv
2020-01-14 adds-git-brv fork/adds-git-brv 1ca0d76 Fixes #700: Adds git-brv
2020-01-08 master origin/master 265b03e Merge pull request #816 from spacewander/git-sed-pathspec
git repl
Git read-eval-print-loop. Lets you run git commands without typing 'git'.
Commands can be prefixed with an exclamation mark (!) to be interpreted as a regular command.
Type exit or quit to end the repl session.
$ git repl
git version 2.9.2
git-extras version 3.0.0
type 'ls' to ls files below current directory,
'!command' to execute any command or just 'subcommand' to execute any git subcommand
git (master)> ls-files
History.md
Makefile
Readme.md
bin/git-changelog
bin/git-count
bin/git-delete-branch
bin/git-delete-tag
bin/git-ignore
bin/git-release
git (master)> !echo Straight from the shell!
Straight from the shell!
git (master)> quit
git coauthor
Add a co-author to the last commit
$ git coauthor user user@email.com
[master b62ceae] Add documentation files
Date: Sat Aug 17 17:33:53 2019 -0500
2 files changed, 145 insertions(+), 0 deletions(-)
create mode 100644 README.md
create mode 100644 CONTRIBUTING.md
$ git log -1
commit b62ceae2685e6ece071f3c3754e9b77fd0a35c88 (HEAD -> master)
Author: user person <userperson@email.com>
Date: Sat Aug 17 17:33:53 2019 -0500
Add documentation files
Co-authored-by: user <user@email.com>
git commits-since
List commits since date (defaults to "last week"):
$ git commits-since
... changes since last week
TJ Holowaychuk - Fixed readme
TJ Holowaychuk - Added git-repl
TJ Holowaychuk - Added git-delete-tag
TJ Holowaychuk - Added git-delete-branch
$ git commits-since yesterday
... changes since yesterday
TJ Holowaychuk - Fixed readme
git count
Output commit count:
$ git count
total 1844
Output detailed commit count:
$ git count --all
visionmedia (1285)
Tj Holowaychuk (430)
Aaron Heckmann (48)
csausdev (34)
ciaranj (26)
Guillermo Rauch (6)
Brian McKinney (2)
Nick Poulden (2)
Benny Wong (2)
Justin Lilly (1)
isaacs (1)
Adam Sanderson (1)
Viktor Kelemen (1)
Gregory Ritter (1)
Greg Ritter (1)
ewoudj (1)
James Herdman (1)
Matt Colyer (1)
total 1844
git fork
Fork the given github <repo>. Like clone but forks first.
$ git fork https://github.com/LearnBoost/expect.js
or just:
$ git fork LearnBoost/expect.js
Does the following:
- forks the repo (prompts for github username and pass)
- clones the repo into the current directory
- adds the original repo as a remote so can track upstream changes
- all remotes refs use git over ssh if configured, otherwise https will be used
$ cd expect.js && git remote -v
origin git@github.com:<user>/expect.js (fetch)
origin git@github.com:<user>/expect.js (push)
upstream git@github.com:LearnBoost/expect.js (fetch)
upstream git@github.com:LearnBoost/expect.js (push)
git force-clone
If the clone target directory exists and is a git repository, reset its contents to a clone of the remote.
$ git force-clone [-b {branch_name}] {remote_url} {destination_path}
$ git force-clone -b master https://github.com/tj/git-extras ./target-directory
CAUTION: If the repository exists, this will destroy all local changes to the repository - changed files will be reset and local branches will be removed.
git release
Release commit with the given <tag> and other options:
$ git release 0.1.0
If you are using semver in your project, you could also use the command below:
(Run git help release for more information)
$ git release --semver major/minor/patch
Does the following:
- Executes .git/hooks/pre-release.sh (if present), passing it the given tag and remain arguments
- Commits changes (to changelog etc) with message "Release <tag>"
- Tags with the given <tag>
- Push the branch / tags
- Executes .git/hooks/post-release.sh (if present), passing it the given tag and remain arguments
git rename-branch
Rename a branch locally, and sync to remote via git push.
# renames any branch
$ git rename-branch old-name new-name
# renames current branch
$ git rename-branch new-name
git rename-tag
Rename a tag (locally and remotely).
$ git tag test
$ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
To git@myserver.com:myuser/myrepository.git
* [new tag] test -> test
$ git tag
test
$ git rename-tag test test2
Deleted tag 'test' (was 1111111)
Total 0 (delta 0), reused 0 (delta 0)
To git@myserver.com:myuser/myrepository.git
* [new tag] test2 -> test2
remote: warning: Deleting a non-existent ref.
To git@myserver.com:myuser/myrepository.git
- [deleted] refs/tag/test
$ git tag
test2
git rename-remote
Rename a git remote regardless of name conflict, and then list current git remotes.
$ git remote -v
origin git@myserver.com:myuser/foo.git (fetch)
origin git@myserver.com:myuser/foo.git (push)
upstream git@myserver.com:myuser/bar.git (fetch)
upstream git@myserver.com:myuser/bar.git (push)
$ git-rename-remote upstream origin
origin git@myserver.com:myuser/bar.git (fetch)
origin git@myserver.com:myuser/bar.git (push)
git reauthor
Rewrite history to change author's identity.
Replace the personal email and name of Jack to his work ones
$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --correct-name 'Jack Foobar'
Replace the email and name of Jack to the ones defined in the Git config
$ git reauthor --old-email jack@perso.me --use-config
Replace only the email of Jack (keep the name already used)
$ git reauthor --old-email jack@perso --correct-email jack@perso.me
Change only the committer email of Jack (keep the author email already used)
$ git reauthor --old-email jack@perso.me --correct-email jack@work.com --type committer
Set Jack's identity as the only one of the whole repository
$ git reauthor --all --correct-email jack@perso.me --correct-name Jack
Set Jack as the only committer of the whole repository (keeps authors)
$ git reauthor --all --correct-email jack@perso.me --correct-name Jack --type committer
git alias
Define, search and show aliases.
Define a new alias:
$ git alias last "cat-file commit HEAD"
Search for aliases that match a pattern (one argument):
$ git alias ^la
last = cat-file commit HEAD
Show all aliases (no arguments):
$ git alias
s = status
amend = commit --amend
rank = shortlog -sn --no-merges
whatis = show -s --pretty='tformat:%h (%s, %ad)' --date=short
whois = !sh -c 'git log -i -1 --pretty="format:%an <%ae>
git ignore
Too lazy to open up .gitignore? Me too!
$ git ignore build "*.o" "*.log"
... added 'build'
... added '*.o'
... added '*.log'
Without any patterns, git-ignore displays currently ignored patterns in both your global and your local .gitignore files:
$ git ignore
Global gitignore: /Users/foo/.gitignore_global
*~
.metadata
---------------------------------
Local gitignore: .gitignore
build
*.o
*.log