Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged git from sorted by
Terminal - Commands tagged git - 97 results
48 function gbl() { git for-each-ref --sort=-committerdate --format='%(committerdate) %(authorname) %(refname)' refs/remotes/origin/|grep -e ".$@"|head -n 10; }
vim `git status --porcelain | sed -ne 's/^ M //p'`
2012-11-21 06:31:46
User: seb1245
Functions: sed vim
Tags: vim git
4

The option --porcelain makes the output of git easier to parse.

This one-liner may not work if there is a space in the modified file name.

vim $(git rev-parse --show-toplevel)/.gitignore
2012-11-20 15:22:35
Functions: vim
Tags: git
0

A little used command, but one I find very useful when needed.

Note: It only works on gitignores in the top level directory.

vim `git status | grep modified | awk '{print $3}'`
2012-11-19 09:48:46
User: TetsuyO
Functions: awk grep vim
Tags: vim git
0

This oneliner gets all the 'modified' files in your git repository, and opens all of them in vim.

Very handy when you're starting to work in the morning and you simply want to review your modified files before committing them.

Maybe there are better ways to do that (and maybe integrated in vim and/or git, who knows), but I found quicker to do this oneliner.

wget --no-check-certificate https://code.google.com/p/msysgit/downloads/list -O - 2>nul | sed -n "0,/.*\(\/\/msysgit.googlecode.com\/files\/Git-.*\.exe\).*/s//http:\1/p" | wget -i - -O Git-Latest.exe
2012-11-14 08:17:50
User: michfield
Functions: sed wget
Tags: git windows wget
-1

This command should be copy-pasted in Windows, but very similar one will work on Linux.

It uses wget and sed.

find . -maxdepth 2 -type d -name '.git' -print0 | while read -d ''; do (cd "$REPLY"; git gc); done
2012-11-07 08:38:33
User: unhammer
Functions: cd find read
Tags: git drivespace
-1

Assumes you've cd'd to the folder in which all your git repos reside; you could run it from ~ without -maxdepth, although that might make find take quite a while longer.

If you have several processor cores, but not that much ram, you might want to run

git config --global pack.threads 1

first, since gc-ing can eat lots of ram.

for k in `git branch -r|awk '{print $1}'`;do echo -e `git show --pretty=format:"%Cgreen%ci_%C(blue)%c r_%Cred%cn_%Creset" $k|head -n 1`$k;done|sort -r|awk -F"_" '{printf("%s %17s %-22s %s\n",$1,$2,$3,$4)}'
^diff^add
2012-10-09 21:51:05
User: binaryten
Tags: git
0

It's useful to run run git st before you commit changes. To see an individual commit it's good practice to type git diff . If you are happy with what you see, to add the file, just type ^diff^add

brd = "! f() { for k in $(git branch $@ | sed 's/^..//; s/ .*//'); do echo "$(git log -1 --pretty='%Cgreen%ci %Cblue(%cr)%Creset ' $k) $k" ; done | sort -r; }; f"
2012-09-28 10:20:51
0

Add this line to your ~/.gitconfig for a git alias "git brd" (i.e., brd = (br)anch+(d)ate) which sorts branches by date. Allows you to pass in limited "git branch" options such as "-r" (remote) or "-a" (all). (Note: forum added "$" prefix to command; obviously in gitconfig there is no "$" prefix.)

git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
git checkout HEAD~2 -- /path/to/dir
alias g='git'
2012-08-31 05:12:56
User: evandrix
Functions: alias
Tags: git
-8

g clone --local --bare . /repo.git

g remote add alias /repo.git

g push alias branch

g log -p filename

g checkout SHA1_rev

g reset --hard

g checkout -b new_branch

g ls-files --deleted

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"
2012-08-23 02:33:22
User: mitzip
2

This command will automatically find the latest version of the file that was deleted and restore it to it's previous location. If, of course, your file was kept in a git repository...

I found this command on http://stackoverflow.com/a/1113140

find . -name ".svn" -type d -exec rm -rf {} \;
2012-08-16 13:04:47
User: gigo6000
Functions: find rm
Tags: svn git rm
-2

This is useful when you are uploading svn project files to a new git repo.

git shortlog -s | sort -rn | head
git checkout -tb mybranch origin/mybranch
git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
2012-06-02 22:04:36
User: hugopeixoto
Functions: sed sort uniq xargs
Tags: statistics git
1

Uses line-porcelain in git blame, which makes it easier to parse the output.

git log --graph --oneline --all --decorate --color
git remote -v | grep fetch | sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | awk {'print "https://github.com/" $1'} | xargs open
2012-04-15 20:48:46
User: brockangelo
Functions: awk grep sed xargs
1

Written for Mac OSX. When you are working in a project and want to open it on Github.com, just type "gh" and your default browser will open with the repo you are in. Works for submodules, and repo's that you don't own.

You'll need to copy / paste this command into a gh.sh file, then create an alias in your bash or zsh profile to the gh.sh script. Detailed instructions here if you still need help:

http://gist.github.com/1917716

for k in $(git branch | sed /\*/d); do echo "$(git log -1 --pretty=format:"%ct" $k) $k"; done | sort -r | awk '{print $2}'
2012-04-07 11:19:00
User: dahuie
Functions: awk echo sed sort
Tags: bash git sed awk
0

Simpler and without all of the coloring gimmicks. This just returns a list of branches with the most recent first. This should be useful for cleaning your remotes.

git submodule foreach git pull --ff-only origin master
curl http://whatthecommit.com/index.txt
tar czf git_mods_circa_dec23.tgz --files-from <(git ls-files -m)
git rev-list --reverse HEAD | awk "/$(git log -n 1 --pretty="format:%h")/ {print NR}"
2011-11-15 21:49:32
User: atoponce
Functions: awk
Tags: git awk
-1

Git uses secure hash sums for its revision numbers. I'm sure this is fine and dandy for ultra-secure computing, but it's less than optimal for humans. Thus, this will give you sequential revision numbers in Git all the way from the first commit.

curl -s 'http://whatthecommit.com/' | grep '<p>' | cut -c4-