Every hacker needs their own selection of git commands here’s my list:


  • Remove a commit from your history (dangerous)
git rebase -p --onto SHA^ SHA

Where SHA is the actual commit hash for example: f2fc2d21cd21a58b0788d7d2e97ece967838d098


  • Remove last commit
git reset --hard HEAD^

  • Squash and rebase
git rebase -i HEAD~5

Squash and rebase the last 5 commits into one. Note you can re-order or remove commits by commenting them out.


  • Safe git clean
git clean -i

Never run git clean without the -i flag, why? Because git clean deletes untracked files, it is very un-likely you want that to happen, instead use the -i parameter:


  • Exclude local files

You can also any un-tracked local file or folder by editing the following file:

.git/info/exclude

The rules are exactly the same a standard .gitignore file except only apply locally. I usually have a folder per a project that I use as a scratch pad for files I don not want to commit

To exclude an already tracked file use the following command:

git update-index --assume-unchanged <file/folder>

  • Download a Github patch

If you are using Github they have a great feature that allows you to patch a pull request, you can add .patch or .diff to the end of pull request URLs for Git’s plaintext views. So that means if you would like to test out a pull request you can apply the pull requests patch using the following command:

curl http://github.com/user/repo/pull/88.patch | git am

  • Check out a Github pull request

You can also checkout a pull request into a new branch like this:

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull//head:refs/remotes/origin/pr/ to this section. Obviously, change the github url to match your project’s URL. It ends up looking like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:joyent/node.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Now fetch all the pull requests:

$ git fetch origin

From github.com:joyent/node
[new ref]         refs/pull/1000/head -> origin/pr/1000
[new ref]         refs/pull/1002/head -> origin/pr/1002
[new ref]         refs/pull/1004/head -> origin/pr/1004
[new ref]         refs/pull/1009/head -> origin/pr/1009
...

To check out a particular pull request:

$ git checkout pr/999

Branch pr/999 set up to track remote branch pr/999 from origin.
Switched to a new branch 'pr/999'

Thanks to this gist for the info


  • Undo/Redo

To undo and redo a git commit run the soft reset command:

git reset --soft HEAD~1

Make your changes and then stage the files using add:

git add

Then finally to re-commit the changes

git commit -c ORIG_HEAD

  • Push to an alternate remote branch with different name

bash git push origin local-name:remote-name

 

Ryan is a hacker dad living in Cape Town, South Africa who likes Skate Boarding JavaScript and Functional Programming.

ryanwild @javascript_fn