very helpful lioness

Stupid Git Tricks

A list of stupid Git tricks so I don't have to keep remembering them.

Oops, I messed up.

This will remove anything staged but not committed:

git reset --hard

If you really messed up, this will remove all untracked files/directories as well:

git clean -df .

If you already committed but haven't pushed yet, this will remove the latest commit, but keep the changes in your local working directory:

git reset --soft HEAD~1

If you already committed and pushed:

git revert [commithash]

Git is giving me permission denied errors on settings.php. What now?

sudo git checkout -- sites/default


chmod ug+w sites/default

/HT @timplunkett and berdir in #drupal-contribute

I have an anonymous clone, but I have commit access to the project. How do I switch to my authenticated URL?

So you were originally just a mere end-user of a project, but then you filed one too many patches and went and got made a maintainer, eh? ;) Don't panic, here's a simple command to bless your clone with commit access:

git remote set-url origin webchick@git.drupal.org:sandbox/greggles/1481160.git

/HT @wereHamster in #git

I have the opposite problem of that; I have an authenticated clone but now want to switch it to anonymous

git remote add readonly http://git.drupal.org/project/drupal.git;
git fetch readonly;
git checkout -bt mybranch readonly/8.x

/HT @sun and berdir in #drupal-contribute

I have clumsy fingers and keep mis-typing commands like "git pusj"

There's a config option for that!

git config --global help.autocorrect 3

Will give output like this:

$ git pusj
WARNING: You called a Git command named 'pusj', which does not exist.
Continuing under the assumption that you meant 'push'
in 0.3 seconds automatically...

/HT @cyberswat in #drupal-contribute

git apply says it can't apply but I swear it's lying.

Yeah, Git is stupid about context changes that happened elsewhere in the file and thinking that it broke the patch in question. Help it smarten up with:

git apply --index -3way foo.patch

/HT @timplunkett in #drupal-contribute