Git Tricks

Here is a collection of Git tips that you may find useful (I did). This list will be updated as I find them (when I remember to as well)

Deleting local branches that have already been merged

[code language=”bash”]

git branch –merged | grep -v “*” | xargs -n 1 git branch -d

[/code]

Finding particular commits that have certain content

e.g I knew at some point in time the repository contained a file (somefile) that started with returning and executing an anonymous JavaScript function that had the signature.

[code language=”javascript”]

(function(myvar)

[/code]

Now to find commits that had that code (its easier to infer which one was first) do the following.

[code language=”bash”]

git grep -e ‘(function(myvar)’ $(git rev-list –all) somedirectory/somefile

someshahashhere:somedirectory/somefile: return (function(myvar) {

someshahashhere2:somedirectory/somefile: return (function(myvar) {

[/code]

Then its a matter of checking out the file from those commits and inspecting.

[code language=”bash”]

git checkout someshahashhere somedirectory/somefile

[/code]

The above could would check out the first commit with a sha hash of {someshahashhere}. You now have that file as it was originally with that anonymous JavaScript function.