Fluent Git

Binjian Xin | 2023-09-18

Table of Contents

Common Scenarios

An Example of Submodule

Porcelain Commands

  • Consult the manual:

            git submodule foreach --recursive 'git log'
    
  • Debugged:

            git submodule foreach --recursive 'git log --oneline'
    
  • More functions:

            git submodule foreach --recursive 'git log --oneline HEAD...HEAD~5'
    

Plumbing Commands

  • Stack overflow:
        git submodule foreach --recursive '
        REV1=HEAD;
        REV2=HEAD~10;
        SHA1=$(cd $toplevel && git ls-tree $REV1 $sm_path | \
            grep -E -o "[0-9a-f]{40}");
        SHA2=$(cd $toplevel && git ls-tree $REV2 $sm_path | \
            grep -E -o "[0-9a-f]{40}");
        git log --oneline $SHA1...$SHA2'
  • Debug:
        git submodule foreach --recursive '
        SHA1=$(cd $toplevel && cd $sm_path && git log | \
            grep -E -o "[0-9a-f]{40}" | head -1);
        SHA2=$(cd $toplevel && cd $sm_path && git log | \
            grep -E -o "[0-9a-f]{40}" | head -10 | tail -n1);
        git log --oneline $SHA1...$SHA2'

Types of Porcelain Commands

  • Git commandline
  • Git lens
  • Gitkraken
  • Emacs magit

Hash Value, Four Types of Objects

Hash (Dark Matter in the Computer World)

  • Efficient
    • Fast hash function calculation, index ~O(1) (array O(N))
    • Very low probability of collision, worst case O(N)
  • sha-1(20byte, 40hex, 160 bits, 8 bits for directory)
    • Higher security -> sha256(32bit), sha512(64bit)
    • $2^{160}\approx 3\times 10^{38}$(Total number of atoms in the universe $\approx 10^{80}$)
  • Wide application of hash tables
    • Programming language data structures: Python dict(json),set
    • Databases: Mongodb object
    • Cloud storage: Object Storage System

Four Objects

  • Tree (spatial/topological relationship)
  • Blob file
  • Commit (time order)
  • Annotated tag
  • Tree object, file object instance: HEAD,
    • git cat-file -p HEAD
    • git ls-tree -r HEAD
    • A file object inside HEAD

Power of Abstraction

  • Content-addressable file system/database
  • All objects are equal, no priority

Version Selection Git Revisions

  • Applied to cherry-pick, reset, log, merge
  • Specific single version
    • head^^/head^2
    • head@{2}
    • head~~/head~2

Version Range

  • master..experiment (from master to experiment): D, C
  • master A ^experiment (from experiment to master or feature): F, E
  • master…experiment (from master to experiment or vice versa): F,E,D,C
  • @: HEAD

Version Relationship Syntax

Figure 1: Multi-branch Version History
Figure 1: Multi-branch Version History
AA^0
BA^A^1A~1
CA^2
DA^^A^1^1A~2B^1
EB^2A^^2
FB^3A^^3
GA^^^A^1^1^1A~3D^1
HD^2B^^2A^^^2A~2^2
IF^B^3^A^^3^
JF^2B^3^2A^^3^2

Range Selection Syntax 1

Figure 2: Multi-branch Version History
Figure 2: Multi-branch Version History

Version range syntax

InputDecompositionResult
DG H D
D FG H I J D F
^G DH D
^D BE I J F B
^D B CE I J F B C
CI J F C
B..C^B CC

Range Selection Syntax 2

Figure 3: Multi-branch Version History
Figure 3: Multi-branch Version History
B…CB ^F CG H D E B C
B^- (merge)B^..B
^B^1 BE I J F B
C^@C^1, FI J F
C^!C ^C^@, C ^FC
F^! DF ^I ^J DG H D F

Tools and Operations in Typical Scenarios

Simulated Scenarios and Countermeasures (Undo Button)

  • Create a feature branch in an existing code repository
  • Add changes (git add)
  • Commit changes (git commit)
  • Undo the most recent commit change (git commit, immediately regret)
  • Undo a long string of changes (undo after multiple commits on a branch, reset)
  • Undo the remote repository (undo after git push to everyone)
  • Change the entire branch (git rebase)
  • Commit changes on the wrong branch (push the relevant changes to the correct branch, cherrypick)
  • Brainstorm!

Conventional Application Commands

  • git branch (off, out): Add changes
  • git commit -a: Undo recent changes, git push -f option.
  • git reset
    • soft, mixed, hard If hard is cleaner, why is mixed the default option?
    • hard is very dangerous (detached/isolated commit, no undo button!)
    • soft, mixed are more conservative, defensive rollback
  • git remote
    • Can it correspond to multiple remote repositories? For example, ros’s github repository and company intranet have special configurations

Complex Commands

  • git rebase
    • git push –force (no undo button!)
    • git merge

Git Reflog Perspective

  • Head branch history (head & branches)