howto 

Send to Kindle
home » snippets » git » howto



Pickaxe searches

See pickaxe

Checking out a specific revision to a different filename.

See stackoverflow.com: git-checkout older revision of a file under a new name

git show HEAD^:dir1/dir2/dir3/main.cpp > old_main.cpp

Patching

git apply =(git format-patch -k --stdout 'commit_A..commit_B')

Where

Flag Description
-k, --keep-subject Do not strip/add [PATCH] from the first line of the commit log message.
--stdout Print all commits to the standard output in mbox format, instead of creating a file for each one.

Splitting Commits

See splitting commits

Resolving a merge conflict

See also Resolving a merge

If you know you want one side or the other, simply

git checkout --theirs -- <path>

Like p4 nothave

# Don't ignore files in .gitignore/excludesfile, etc.
git ls-files . --others

# DO ignore files in .gitignore/excludesfile, etc.
git ls-files . --others --exclude-standard

Any diffs around?

# Have return code state if there are any diffs
git diff --no-ext-diff --quiet
git diff --no-ext-diff --quiet --cached

Keep some untracked changes around without committing

Use git update-index --assume-unchanged

It has some caveats.

What is the current branch?

Shell code:

# From: http://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
if branch=$(git symbolic-ref --short -q HEAD); then
  echo on branch $branch
else
  echo not on any branch  # detached head.
fi

# Misc/related commands.
git symbolic-ref --short -q HEAD  # What happens when head is detached?

# rev-parse handles packed refs.
git rev-parse --symbolic-full-name --abbrev-ref HEAD
git rev-parse --symbolic-full-name --abbrev-ref '@{u}'         # get remote tracking branch
git rev-parse --symbolic-full-name --abbrev-ref '@{upstream}'  # get remote tracking branch

Tags and remotes

Ref: http://stackoverflow.com/a/5496610

[remote "upstream"]
    url = …
    fetch = +refs/heads/*:refs/remotes/upstream/*
    # Ref: http://stackoverflow.com/a/5496610
    tagopt = --no-tags
    fetch = +refs/tags/*:refs/tags/upstream/*

Delete merged branches

git branch --merged | xargs git branch -d

SHAs for refs

# These two are equivalent
git for-each-ref --format '%(objectname)' -- 'refs/remotes/chirayu/presubmit*'
git rev-parse --glob='refs/remotes/chirayu/presubmit*'

# HEAD is special and not a "ref", so you would need to
# use git rev-parse for it
git rev-parse --verify HEAD

Finding all branches / tags that contain a specific commit (by message)

COMMIT_MSG_GREP='fixed foo'
for ref in $(git show-ref | awk '{ print $1 }') ; do
  for sha in $(git log --all --grep=$COMMIT_MSG_GREP) ; do
    if git merge-base --is-ancestor $sha $ref ; then
      print -r -- $ref
    fi
  done
done

Create an annotated tag

git tag -a -f -m 'PendingAsync service / whenStable / PR 1489: version using Future/Completer' pr_1489_pending_async_with_futures

Commit pointed to by an annotated tag

#  <rev>^{}, e.g. v0.99.8^{}
#      A suffix ^ followed by an empty brace pair means the object
#      could be a tag, and dereference the tag recursively until a
#      non-tag object is found.
git rev-parse 'v0.12.0^{}'

# show-ref --dereference
#   -d, --dereference
#       Dereference tags into object IDs as well. They will be shown
#       with "^{}" appended.
git show-ref --dereference  v0.12.0
# 299ed9bb2ce71378b4f29dd10d3b6c93ebb0371f refs/tags/angular/v0.12.0
# 65037f85dbd49d56bb4852be355bb1b9c3a3390d refs/tags/angular/v0.12.0^{}
# 299ed9bb2ce71378b4f29dd10d3b6c93ebb0371f refs/tags/v0.12.0
# 65037f85dbd49d56bb4852be355bb1b9c3a3390d refs/tags/v0.12.0^{}