Applying branch changes to a new branch in git


I am unsure how other people do this, but this is how I apply changes from one branch to a new branch when the original branch can't be rebased to master (say, when the original branch is thousands, or really, maybe just hundreds or tens of conflicting, commits behind master).

$ git co branch-no-longer-loved

If differences with master aren't causing rebase issues, can do this:

$ git rebase master
$ git diff master > ~/some-differences.diff

More likely, I'm doing this because I can't rebase to master. So, taking my changes back to where I diverged from master, say 3 checkins:

$ git diff HEAD~3 > ~/some-differences.diff

Git checkout local repo from N days ago

git checkout @{two.days.ago}

If only there were a way to checkout files from the fuyoocha.

git checkout @{two.days.fromnow}

or maybe

git checkout @{two.days.aftercompleting}

Remove untracked files in git repo


Sometimes, you just want to clean up a git repo, getting rid of the untracked files. "git reset" will reset the repo, but not the untracked files. Use "git clean"

# remove untracked files and directories
$ git clean -f -d 
# do a test run first
$ git clean --dry-run -f -d
# clean ignored files (emacs turds!)
$ git clean -f -X 

Archive and delete a git branch


Head to the top of the git repository.

$ cd path/to/repo/top

Check out the branch you want, pull, update, whatever to put it into the state you want to archive.

$ git co branch-to-archive
$ git up
$ git pull
$ git whatever

Run this to archive the branch

$ git archive branch-to-archive | gzip > ../some/archive/path/repo-name-branch-name.YYMMDD.tar.gz 

Check the repo is what you want:

$ tar tvfz ../some/archive/path/repo-name-branch-name.YYMMDD.tar.gz | less

Delete the branch remotely and locally:

$ git push origin :branch-to-archive
$ git branch -D branch-to-archive

And, of course, I have this in a script.

Search for a string in all git branches


Yay, git grep!

git grep "string/regexp" $(git rev-list --all)

I love that it works with regular expressions, too.

Ignore .git directories in Apache DOCROOT


Put this in a VirtualHost config block to make sure git files aren't served by the Apache process:

<Directorymatch "^/.*/\.git/">
Deny from all

Won't work in an .htaccess file.