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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

Commands tagged git from sorted by
Terminal - Commands tagged git - 106 results
git diff $(git log --pretty=format:%h -2 --reverse | tr "\n" " ")
2015-07-07 17:50:50
Functions: diff tr
Tags: git
1

A great command to assign to an alias, allowing you to git diff the last two commits in git.

for file in $( git ls-files ); do echo $file; touch -t $(git --no-pager log --date=local -1 --format="%ct" $file | php -r 'echo @date( "YmdHi.s", trim( file_get_contents( "php://stdin" ) ) );') $file; done
git rev-list --reverse --topo-order master... | while read rev; do git checkout preview; git cherry-pick $rev || break; done
2015-04-23 14:28:06
User: shadyvb
Functions: read
Tags: git preview
-2

Creating feature-branches off master, and trying to merge them in an integration branch (preview), sometimes causes conflicts because the feature-branch might hold changes from 'master' that aren't on preview yet. So this ensures only the commits added to the feature-branch are moved to integration (preview).

Note: This assumes you're currently on the feature-branch. Adjust 'master/preview' branch names to suit your environment.

git diff --word-diff --color-words | aha > index.html && firefox index.html
vim `git diff --name-only`
2015-02-25 11:12:49
User: mbudde
Functions: diff vim
Tags: vim git
1

For editing files added to the index:

vim `git diff --name-only --cached`

To edit all changed files:

vim `git diff --name-only HEAD`

To edit changed files matching glob:

vim `git diff --name-only -- '*.html'`

If the commands needs to support filenames with whitespace, it gets a bit hacky (see http://superuser.com/questions/336016/invoking-vi-through-find-xargs-breaks-my-terminal-why for the reason):

git diff --name-only -z | xargs -0 bash -c '</dev/tty vim "$@"' x

The last part can be put in a script named e.g. vimargs, and used with any command outputting NUL separated filenames:

git grep -lz foobar | vimargs
git branch | grep -v "master" | sed 's/^[ *]*//' | sed 's/^/git branch -D /' | bash
2015-01-31 00:29:32
User: Trindaz
Functions: grep sed
Tags: git
2

Delete all your branches except master (useful after archiving branches)

git branch | grep -v "master" | sed 's/^[ *]*//' | sed 's/.*/& &/' | sed 's/^/git tag archive\//' | bash
2015-01-31 00:26:15
User: Trindaz
Functions: grep sed
Tags: git archive
0

Description by segments delimited by pipe (|)

1. List all git branches

2. Exclude master

3. Trim output and remove display elements such as * next to current branch

4. Repeat branch name after a space (output on each line: branch_name branch_name)

5. Prepend each line with the git tag command

6. Execute the output with bash

git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
2015-01-25 00:30:37
User: krizzo
Functions: grep xargs
3

This checks if the branch has been merged with master and then will delete the ones that have been. Keeps your local git repo nice and clean from all the branches.

find /srv/code -maxdepth 4 -type f -regex ".*\(\(package\|composer|npm\\|bower\)\.json\|Gemfile\|requirements\.txt\\|\.gitmodules\)"
2014-11-28 16:34:35
User: renoirb
Functions: find
Tags: bash git PHP ruby
0

List all dependencies manifests so you can install them.

In a scenario where you want to deploy a number of web applications and run their dependency managers, how could you run all of them in a systematic order.

One of the complexity is to ensure you get only your own top level dependencies. That way, you don recursively call development dependencies of your own dependencies.

Otherwise you might end up discovering dependency management manifests that are already been pulled by your own projects.

# Using this command

This command helps me find them and I can then run what?s required to pull them from their respective sources.

This command assumes the following:

1. Your code checkouts are in a flat repository layout (i.e. not nested).

2. Finds manifests for:

- NPM (nodejs),

- Composer (php),

- bower,

- requirements.txt (Python), and

- git submodules

git reflog --date=local | grep "Oct 2 .* checkout: moving from .* to" | grep -o "[a-zA-Z0-9\-]*$" | sort | uniq
2014-10-03 15:12:22
User: Trindaz
Functions: grep sort
0

Replace "Oct 2" in the first grep pattern to be the date to view branch work from

git stash save --keep-index
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash; mv git-completion.bash ~/.git-completion.bash; echo "source ~/.git-completion.bash" > ~/.bashrc; source ~/.git-completion.bash
2014-07-22 14:21:00
User: servel333
Functions: echo mv wget
0

Downloads this raw script https://github.com/git/git/blob/master/contrib/completion/git-completion.bash from Github, copies it to your home directory, autoloads it in ~/.bashrc and sources it.

git log --name-status --oneline --all | grep -P "^[A|M|D]\s" | awk '{print $2}' | sort | uniq
git status --porcelain | sed -ne 's/^ M //p' | tr '\n' '\0' | tr -d '"' | xargs -0 vim
2014-05-08 08:36:27
User: uschrisf
Functions: sed tr xargs
Tags: vim git
0

Works even with spaces in filenames.

As an alias in .gitconfig:

[alias]

editchanged = "!git status --porcelain | sed -ne 's/^ M //p' | tr '\\n' '\\0' | tr -d '\"' | xargs -0 vim"

git rm $(git ls-files --deleted)
2014-05-01 06:21:56
User: jaapie
Functions: rm
Tags: git
1

You'll probably want to pass in the -rf options if you have to delete a directory or something.

git rev-list --max-parents=0 HEAD
2014-02-10 20:39:08
User: lkraider
Tags: git commit
1

https://git.kernel.org/cgit/git/git.git/commit/?id=ad5aeeded3295589b2573b143f754762a56f8f82

ssh HOST '(cd REPO_DIR && git diff --name-only HEAD | cpio -o -Hnewc --quiet)' | cpio -iduv --quiet -Hnewc
2014-02-01 18:40:31
User: ivan4th
Functions: cd cpio diff ssh
Tags: ssh git cpio
0

Copy changed files from remote git repository, _including binary ones_, staged and unstaged alike. Note that this command doesn't handle deleted files properly.

while true; do rsync -vR $(git ls-files | inotifywait -q -e modify -e attrib -e close_write --fromfile - --format '%w') user@host:dest/dir/; done
2014-01-21 10:31:41
User: leucos
Functions: rsync
0

While edtiing a project under git, it is sometimes nice to sync changes immediately to a test machine.

This command will take care of this if you have inotifywait installed on the developement machine.

Note the -R (relative) in rsync. with

rsync foo/bar/baz user@host:dest/dir/

it will put 'baz' in dest/dir/foo/bar/ which is what we want.

this can be turned into a function for additionnal flexibility :

function gitwatch() {

if [ -z $1 ]; then

echo "You must provide a rsync destination"

return

fi

while true; do

rsync -vR $(git ls-files | inotifywait -q -e modify -e attrib -e close_write --fromfile -

--format '%w') $1

done

}

git log --pretty=oneline --abbrev-commit
git status -s | grep -o ' \S*php$' | while read f; do php -l $f; done
2013-12-14 11:47:54
User: ruslan
Functions: grep read
Tags: git PHP lint
1

Checks for syntax errors in PHP files modified in current working copy of a Git repository.

for file in $(git ls-files | grep old_name_pattern); do git mv $file $(echo $file | sed -e 's/old_name_pattern/new_name_pattern/'); done
alias tasks='git grep -EI "TODO|FIXME"'
2013-10-21 23:54:46
User: lizardo
Functions: alias grep
Tags: bash git todo
0

"git grep" automatically excludes untracked files (e.g. compiler output) and files under .git directory. If no directory or file is given, it will recursively search through the current directory.

git reset --hard <commidId> && git clean -f
git config --global diff.external 'bash -c "meld $2 $5"'
find .git/objects -type f -printf "%P\n" | sed s,/,, | while read object; do echo "=== $obj $(git cat-file -t $object) ==="; git cat-file -p $object; done