webchick.net

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

or


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 [email protected]: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

When applying a patch, I'm getting an error that a file already exists, but I triple-checked and my working directory is clean. WAT?

For example:


error: core/vendor/phpunit/php-timer/tests/TimerTest.php: already exists in working directory

Welcome to the special hell that is case insensitive file systems! :D This will happen on OS X if, for example, you currently have a file like:

core/vendor/phpunit/php-timer/Tests/TimerTest.php

and the patch is trying to change it to:

core/vendor/phpunit/php-timer/tests/TimerTest.php

(note the case of the directory "Tests")

Luckily, there is a very slick workaround outlined in https://coderwall.com/p/mgi8ja/case-sensitive-git-in-mac-os-x-like-a-pro which I will copy/paste here for posterity:

Here's what you do:

  • Launch Disk Utility
  • Choose "New Image"
  • Enter a nice Name for your Volume, e.g "Workspace"
  • Set the size to something that will most likely fit your needs (resizing is a whole another story)
  • Select "Mac OS Extended (Case-sensitive, Journaled)" in "Format".
  • Select "Single Partition - Apple Partition Map" in "Partitions"
  • Ensure "sparse bundle disk image" is set in "Image Format".
  • Save it somewhere on your hard drive

Now, when you mount this disk image you can move all your git repos over to this Volume to enjoy a git that doesn't get confused by those pesky case-changes!

(PS: You can choose to add the Disk Image created to your Login Items in the OS X System Preferences to have it automatically mount on boot)

/HT @grevenx