git

Hide .git repo with Apache

Snippet

Solution 2.

Don't allow apache to serve anything from the .git repo that might be in the DocumentRoot . This is great because it hides that there even IS a git repo on the webserver.

Add this to the .htaccess in the DocumentRoot after the RewriteEngine On line.

RedirectMatch 404 /\.git

Git remove from repo, keeping on file system

Snippet

git rm will remove a file from the file system and the repository. If you want to keep the file locally (say, because you're adding it to the .gitignore file), remove it from the repository cache:

# for a file
git rm --cached filename
 
# for a directory
git rm --cached -r directoryname

Unstage a file in git

Snippet

Add a file / directory to staging

git add filePath

Now, if you want to remove it from stage, but want to keep the changes you've made locally.

# unstage the file, remove it from the index, don't remove it from the working directory
# a commit will remove it from the repo
$ git rm --cached filePath
 
# unstage a file, keep the local changes
# YOU NORMALLY WANT THIS ONE
$ git reset filePath

Remove deleted files from a git repository

Snippet

When updating a drupal site with drush pm-update, files can be removed. In order to remove them from the git repository, they need to be deleted with git rm. Doing them individually is painful, so do it on the command line with little effort.

Yes, those are backticks.

$ git rm `git st  | grep deleted | cut -f2 -d:`

List git remotes

Snippet

When talking remote repositories, origin is the most well known. We can have other remote repositories. Adding and listing them below.

$ git remote add remotename https://git.repository/path/to/my-repo.git
$ git remote -v
remote1 kitt@some.example.com:sg/my-repo.git (fetch)
remote1 kitt@some.example.com:sg/my-repo.git (push)
remotename https://git.repository/path/to/my-repo.git (fetch)
remotename https://git.repository/path/to/my-repo.git (push)

git merge resolve conflicts by always choosing merged branch

Snippet

There are times when a branch has diverged so far from master or itself that doing a fetch-merge is just impossible without conflicts. Those with changing binary files or gigantic files? The worst.

$ git reset --hard HEAD
HEAD is now at 82a18be this is my last change, no really
$ git pull origin my_branch
From github.com:kitt/this-is-a-fake-repo
 * branch             my_branch -> FETCH_HEAD
Auto-merging some-project/project.xcodeproj/project.pbxproj
CONFLICT (content): Merge conflict in some-project/project.xcodeproj/project.pbxproj
Auto-merging some-project.xcworkspace/contents.xcworkspacedata
Automatic merge failed; fix conflicts and then commit the result.

Solution, just take what the branch has, don't try to merge in changes, in case of conflict.

git merge -s recursive -X theirs branch-name

Applying branch changes to a new branch in git

Blog

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

Blog
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

Snippet

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

Blog

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.

Pages