Discarding Files

Remove pesky .DS_Store on Mac

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Then add .DS_Store to .gitignore.

Remove files from .gitignore

Add the file to .gitignore then in the top level of the repo:

git rm -r --cached .
git add .
git commit -m "fixed untracked files"

Discarding Changes

Discard all unstaged files

git checkout -- .

git reset --hard
git clean -nd (dry run)
git clean -fd (-f is force, -d is remove directories)

Revert a file

git checkout -- <filename>

or get file from origin

git checkout origin/master -- <filename>

Reset local to origin

git fetch --all
git reset --hard origin/master

– Source: Stack Overflow


See the difference between 2 branches

git diff --name-status master..<branch>


Apply a patch from a url

For the lazy among us…

curl <url.patch> | git apply -
git commit -am "..."


Download and track remote branch

git checkout -t origin/<branch-name>

Push branch to live and track it

git checkout -b <branch-name>
git push -u origin <branch-name>


Merge master into your branch

git checkout <branch>
git merge master

If it pops you into vim then press esc to return to command mode and enter ZZ to save and exit (and commit). You can input text with i. And :wq just quits.

Download remote master and merge with your local branch

git fetch origin
git merge origin/master


Use a different SSH Key for a different server

Inside ~/.ssh/config put:

Host github.com
  HostName github.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/different_rsa

Use your SSH Keys inside a VM

host <ip-address-of-vm>
  ForwardAgent yes

Make Git use your text editor instead of Vim

~ git config --global core.editor "atom --wait"~

Replace atom --wait with your text editor. See list of options.

File System Issues

If you get “error: unable to unlink old ‘' (Permission denied)", on the parent directory do:

chmod g+w <directory>

Fix “modified files” issue after clone

This is git seeing differences in line endings on your file system. In ~/.gitattributes add:


– Source: Stack Overflow