SaltyCrane Blog — Notes on JavaScript and web development

git notes

How to get the tag associated with a specific git revision

$ git describe --tags 5c70d1a15a4637a2057b1464c54820629e78cd05
staging/1.5

How to show the tracked branches from the remote "origin"

$ git remote show origin 

How to find renames

$ git log --name-only --follow --all -- filename 

How to remove all local tags

From http://stackoverflow.com/questions/1841341/remove-local-tags-that-are-no-longer-on-the-remote-repository/5373319#5373319

$ git tag -l | xargs git tag -d 

How to detach a subdirectory into separate Git repository

Note I did not want any tags or branches in the new repository. From http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository/359759#359759

$ git clone --no-hardlinks /XYZ /ABC
$ cd /ABC
$ git remote rm origin
$ git tag -l | xargs git tag -d
$ git filter-branch --subdirectory-filter ABC HEAD
$ git reset --hard
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --aggressive --prune=now 

How to convert a Mercurial repo to git

From http://hedonismbot.wordpress.com/2008/10/16/hg-fast-export-convert-mercurial-repositories-to-git-repositories/

$ git clone git://repo.or.cz/fast-export.git
$ mkdir new_git_repo
$ cd new_git_repo
$ git init
$ /path/to/hg-fast-export.sh -r /path/to/hg_repo
$ git checkout HEAD

How to create a patch for a single commit

References: http://stackoverflow.com/questions/6658313/generate-a-git-patch-for-a-specific-commit, http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/.

$ git format-patch -1 01434ee4fe056e384decbf2d398cccafe53120f2 --stdout > mypatch 

How to break up a previous commit into more than one commit

http://stackoverflow.com/questions/6217156/how-to-break-a-previous-commit-into-multiple-commits

git rebase -i will do it.

To split apart your most recent commit, then:

$ git reset HEAD~

Now commit the pieces individually in the usual way, producing as many commits as you need.

If it was farther back in the tree, then

$ git rebase -i HEAD~3

where 3 is how many commits back it is.

When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace "pick" with "edit". Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:

$ git reset HEAD~

Commit the pieces individually in the usual way, producing as many commits as you need, then

$ git rebase --continue

How to replace the master branch with another branch

http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

Commit message when I did this at work:

Replace old "master" with "dp.2013.r21" using

http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch

    git checkout dp.2013.r21
    git merge -e -s ours master (that's where I am right now)
    git checkout master
    git merge dp.2013.r21
    git push origin master

Cleanup:
    git branch -D dp.2013.r21
    git checkout dp.2013.r21

The automatically generated merge message:
Merge branch 'master' into dp.2013.r21

How to exclude files from "git diff"

There are 2 options:

  1. Use .gitattributes as described here: http://stackoverflow.com/a/1017676/101911
  2. Use new pathspec magic described here: http://stackoverflow.com/a/29374503/101911 Example:
    $ git diff --stat=120,120 HEAD~10 | wc -l
    68
    $ git diff --stat=120,120 HEAD~10 -- '*/_assets/built/*' 'tests/robot/*' | wc -l
    49
    $ git diff --stat=120,120 HEAD~10 -- . ':!*/_assets/built/*' ':!tests/robot/*' | wc -l
    20
        
    
    (The count is off by one because there is an extra line in the output.) I will use this option because I can use it with icdiff which shows whitespace changes.

Comments