Advanced Git


More delicate Git commands

Here goes more advanced Git commands not found among the basic ones.

for-each-ref

Display info in .git/refs.

#!/bin/sh

git for-each-ref --shell --format="ref=%(refname)" refs/tags | \
while read entry
do
    eval "$entry"
    echo `dirname $ref`
done

This modified example taken from the one in the official manual pipes

ref=refs/tags/181116
ref=refs/tags/copyBtn0
ref=refs/tags/fa531
ref=refs/tags/solarized

to a while loop. Each line represents an entry. In each iteration, eval set the shell variable ref to the output Git reference name. dirname chops off the tag names (181116, fa531, etc) and returns refs/tags.

ls-files, ls-tree

List files (resp. files in tree).

Examples

$ git ls-files
$ git ls-tree -r -names-only # "-r" means recursive

Applications

For iteration over cached files only if the file name is “regular enough”. These two commands are synonymous to each other.

merge-base

Return the SHA1 hash of the youngest common ancestor of two branches.

$ git merge-base master dev

Applications

merge –squash, rebase -i

merge --squash <src> condenses commits into one on the current branch without touching src.

rebase, meaning “re-base”, is analogous to a pot transplant. The -i flag opens an interactive session for that.

Applications

Both merge --squash and rebase can be used for cleaning the commit history. The form can be used if <src> branch is to be thrown away. In rebase -i, one can also squash Git commits.

reflog

Track the SHA1 hash that HEAD represents.

Applications

Find lost commit.

rev-parse

Return the SHA1 hash that Git ref represents.

$ rev-parse FETCH_HEAD  # head of fetched commits

Applications

Verify repo status.