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 - 91 results
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-
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k" --`\\t"$k";done|sort
2011-11-02 06:16:22
User: ryan
Functions: echo sed
1

This fixes a bug found in the other scripts which fail when a branch has the same name as a file or directory in the current directory.

git config --local --get remote.origin.url
2011-10-03 09:29:34
User: hced
1

Great way to quickly grasp if a locally cloned repository originates from e.g. github or elsewhere.

git branch | cut -c3- | grep -v "^master$" | while read line; do git branch -d $line; done | grep 'Deleted branch' | awk '{print $3;}' | while read line; do git push <target_remote> :$line; done
2011-08-13 16:58:34
User: gocoogs
Functions: awk cut grep read
0

attempts to delete all local branches. git will fail on any branches not fully merged into local master, so don't worry about losing work. git will return the names of any successfully deleted branches. Find those in the output with grep, then push null repositories to the corresponding names to your target remote.

assumes:

- your local and remote branches are identically named, and there's nothing extra in the remote branch that you still want

- EDIT: you want to keep your local master branch

git add -u
parallel -j4 cd {}\; pwd\; git pull :::: <(git submodule status | awk '{print $2}')
2011-06-20 00:20:26
User: clvv
Functions: awk cd
2

Make sure to run this command in your git toplevel directory. Modify `-j4` as you like. You can also run any arbitrary command beside `git pull` in parallel on all of your git submodules.

git archive HEAD | gzip > ~/Dropbox/archive.tar.gz