Understanding Git: A Comprehensive Guide from Beginner to Advanced – Part 2

Let’s continue with part 2.


Git Cheat Sheet for Debugging 🐛


1️⃣ git bisect – Binary Search for Bug Source

  • What it does: Uses a binary search algorithm to find which commit introduced a bug.
  • Example: To start bisecting, you usually mark the current version as bad and then mark a known-good version:
  git bisect start
  git bisect bad                  # Current version is bad
  git bisect good abcdef1         # abcdef1 is known to be goodCode language: PHP (php)

Then Git will check out a commit in between, waiting for you to mark it as good or bad.

  • Sample Output:
  Bisecting: 12 revisions left to test after this (roughly 4 steps)
  [a1234567890b] commit message hereCode language: JavaScript (javascript)

2️⃣ git blame – Who Changed What?

  • What it does: Shows what revision and author last modified each line of a file.
  • Example: To blame a file:
  git blame file.txtCode language: CSS (css)
  • Sample Output:
  a1b2c3d4 (Author Name 2023-09-12 12:34:56 -0400 1) First line text
  e5f6g7h8 (Author Name 2023-09-12 12:34:56 -0400 2) Second line textCode language: CSS (css)

3️⃣ git grep – Search for Text Patterns

  • What it does: Lets you search through your Git repository like grep.
  • Example: To search for “Hello” in all files:
  git grep "Hello"Code language: JavaScript (javascript)
  • Sample Output:
  file.txt:1:Hello, World!
  another-file.txt:10:Hello again!Code language: CSS (css)


Git Cheat Sheet for Guides & Concepts 📚


1️⃣ .gitattributes – Customizing Git Attributes

  • What it does: Defines attributes per path for a Git repository.
  • Example: To set all .txt files to use LF line endings:
  echo "*.txt text eol=lf" > .gitattributesCode language: PHP (php)

2️⃣ Command-line Interface Conventions

  • What it does: Guidelines for how Git commands and options should be formatted and used in the CLI.
  • Example:
  git commit -m "Your commit message"Code language: JavaScript (javascript)

3️⃣ Everyday Git

  • What it does: A set of common Git commands and workflows that you’ll use on a daily basis.
  • Example:
  git add .
  git commit -m "Update"
  git pushCode language: JavaScript (javascript)

4️⃣ Frequently Asked Questions (FAQ)

  • What it is: A compilation of common questions and issues that users may encounter while using Git.

5️⃣ Glossary

  • What it is: Definitions and explanations of Git-specific terms like “commit,” “branch,” “merge,” etc.

6️⃣ Hooks

  • What it does: Custom scripts that can be run automatically during specific Git operations.
  • Example:
  # Sample pre-commit hook script in `.git/hooks/pre-commit`
  echo "Running pre-commit hook"Code language: PHP (php)

7️⃣ .gitignore – Ignoring Unwanted Files

  • What it does: Specifies intentionally untracked files to ignore.
  • Example:
  echo "node_modules/" > .gitignoreCode language: PHP (php)

8️⃣ .gitmodules – Defining Submodules

  • What it does: Stores the mapping between a repository and its submodules.
  • Example:
  git submodule add https://github.com/username/repo.git path/to/submoduleCode language: JavaScript (javascript)

9️⃣ Revisions

  • What it does: Specifies versions of a repository or file in Git.
  • Example:
  git show HEAD~1

🔟 Submodules

  • What it does: Allows you to include or embed one or more repositories as a sub-folder inside another repository.
  • Example:
  git submodule update --init --recursive

1️⃣1️⃣ Tutorial

  • What it is: Guides and tutorials aimed at helping you learn Git.

1️⃣2️⃣ Workflows

  • What it is: Common practices and recommended workflows for using Git effectively.


Git Cheat Sheet for Email Workflows 📧


1️⃣ git am – Apply Mailbox Patches

  • What it does: Applies a series of patches from a mailbox format, often generated by git format-patch.
  • Example: To apply a mailbox patch:
  git am < patch-file
  • Sample Output:
  Applying: Your commit message here

2️⃣ git apply – Apply a Patch

  • What it does: Applies a patch created using git diff.
  • Example: To apply a patch from a file:
  git apply patch-file.patchCode language: CSS (css)
  • Sample Output:
  // No output means the command was successful.Code language: JSON / JSON with Comments (json)

3️⃣ git format-patch – Create Mailbox Patch

  • What it does: Creates a patch in mailbox format that can be emailed.
  • Example: To create a patch for the last commit:
  git format-patch -1 HEAD
  • Sample Output:
  0001-Your-commit-message-here.patchCode language: CSS (css)

4️⃣ git send-email – Send a Collection of Patches

  • What it does: Sends a collection of patches as emails, which is often used with patches generated by git format-patch.
  • Example: To send an email with patches:
  git send-email *.patchCode language: CSS (css)
  • Sample Output:
  Sending initial info
  Result: OK

5️⃣ git request-pull – Generate Request for Pull

  • What it does: Generates a summary of changes between two commits, which you can email to someone as a pull request.
  • Example: To request a pull for changes:
  git request-pull origin/master my-feature-branch
  • Sample Output:
  The following changes since commit abcdef1 are available in the Git repository at:

  Your commit message and diff stats here...


Git Cheat Sheet for External Systems 🔄


1️⃣ git svn – Bidirectional Operation Between a Subversion Repository and Git

  • What it does: Allows operations between a Subversion (SVN) repository and Git. Useful for migrating from SVN to Git or working with an SVN repository using Git.
  • Example: To clone an SVN repository:
  git svn clone http://svn.example.com/projectCode language: PHP (php)
  • Sample Output:
  Initialized empty Git repository in /path/to/project/.git/
  Checked out HEAD:
  http://svn.example.com/project r1234Code language: PHP (php)

2️⃣ git fast-import – Backend for Fast Git Data Import

  • What it does: Provides a fast way to import revision history into Git from another VCS. Typically used with a custom script or export commands from other systems.
  • Example: This command is generally not used directly. Instead, it often receives data through pipes from export commands or custom scripts. For instance:
  some_export_command | git fast-importCode language: JavaScript (javascript)
  • Sample Output:
  fast-import statistics:
    commits: 5000
    trees: 3000
    blobs: 2000
    tags: 100
    resets: 50Code language: JavaScript (javascript)


Git Cheat Sheet for Administration 🛠️


1️⃣ git clean – Remove Untracked Files and Directories

  • What it does: Removes untracked files and directories.
  • Example: To remove all untracked files:
  git clean -f
  • Sample Output:
  Removing untracked-file.txtCode language: CSS (css)

2️⃣ git gc – Garbage Collection

  • What it does: Cleans up unnecessary files and optimizes the local repository.
  • Example: To run garbage collection:
  git gc
  • Sample Output:
  Counting objects: 25, done.

3️⃣ git fsck – File System Check

  • What it does: Verifies the integrity of objects in a Git repository.
  • Example: To run a filesystem check:
  git fsck
  • Sample Output:
  Checking object directories: 100% (256/256), done.

4️⃣ git reflog – Reference Logs

  • What it does: Shows history of local operations, like commits and checkouts.
  • Example: To view the reflog:
  git reflog
  • Sample Output:
  HEAD@{0}: checkout: moving from feature-branch to mainCode language: JavaScript (javascript)

5️⃣ git filter-branch – Rewrite Branch History

  • What it does: Allows you to rewrite Git history. Use with caution as this can overwrite commits.
  • Example: To change the email address in past commits:
  git filter-branch --env-filter 'export GIT_AUTHOR_EMAIL=new-email@example.com' -- --allCode language: JavaScript (javascript)
  • Sample Output:
  Rewrite a1b2c3d4 (19/30) (2 seconds passed, remaining 3 predicted)

6️⃣ git instaweb – Instant Web Server for a Git Repo

  • What it does: Launches a web server to browse your Git repository.
  • Example: To start an instaweb server:
  git instaweb
  • Sample Output:
  [server] startedCode language: CSS (css)

7️⃣ git archive – Create an Archive of Files from a Named Tree

  • What it does: Creates an archive (e.g., tar or zip file) of files from a named tree like a branch or tag.
  • Example: To create a tar archive of the main branch:
  git archive --format=tar --output=/path/to/archive.tar mainCode language: JavaScript (javascript)

8️⃣ git bundle – Move Objects and Refs by Archive

  • What it does: Packages objects and references in an archive to transfer them to another repository.
  • Example: To create a bundle of all branches:
  git bundle create repo.bundle --allCode language: CSS (css)
  • Sample Output:
  Enumerating objects: 16, done.


Git Cheat Sheet for Server Admin 🖥️


1️⃣ git daemon – Export Repositories Over Git Protocol

  • What it does: Starts a simple server to expose repositories via the Git protocol. Generally used for read-only, anonymous access.
  • Example: To run a git daemon exposing a repository:
  git daemon --base-path=/path/to/repo --export-allCode language: JavaScript (javascript)
  • Sample Output:
  Ready to rumble at http://[::]:9418/Code language: JavaScript (javascript)

2️⃣ git update-server-info – Update Auxiliary Info File to Help Dumb Servers

  • What it does: Updates the auxiliary information for serving repositories over dumb protocols. Often used with HTTP or FTP servers.
  • Example: To update server information:
  git update-server-info
  • Sample Output:
  // Usually, this command doesn’t produce any output if it succeeds.Code language: JSON / JSON with Comments (json)


Git Cheat Sheet for Plumbing Commands 🛠️


1️⃣ git cat-file – Display Object Content

  • What it does: Examines the content, type, and size of an object.
  • Example: Show the type of an object:
  git cat-file -t [SHA-1]Code language: CSS (css)
  • Sample Output:
  commit (or blob, tree, tag)

2️⃣ git check-ignore – Debug Ignored Files

  • What it does: Debugs why a particular file is being ignored by Git.
  • Example:
  git check-ignore path/to/file
  • Sample Output:
  .gitignore:3:path/to/file

3️⃣ git checkout-index – Checkout Files from the Index

  • What it does: Checks out files from the index to the working directory.
  • Example:
  git checkout-index --all
  • Sample Output:
  // No output if successfulCode language: JSON / JSON with Comments (json)

4️⃣ git commit-tree – Create a New Commit Object

  • What it does: Creates a new commit object from a given tree object.
  • Example:
  git commit-tree [TREE_SHA-1] -p [PARENT_SHA-1] -m "Message"Code language: CSS (css)
  • Sample Output:
  [NEW_COMMIT_SHA-1]Code language: JSON / JSON with Comments (json)

5️⃣ git count-objects – Count Unpacked Objects

  • What it does: Counts and provides information about unpacked object files.
  • Example:
  git count-objects -v
  • Sample Output:
  count: 248
  size: 960

6️⃣ git diff-index – Compare Content between Index and Repository

  • What it does: Compares the content in the index with the content in the repository.
  • Example:
  git diff-index HEAD
  • Sample Output:
  // Diff outputCode language: JSON / JSON with Comments (json)

7️⃣ git for-each-ref – List References

  • What it does: Lists each reference in a local repository.
  • Example:
  git for-each-ref
  • Sample Output:
  SHA-1 refname

8️⃣ git hash-object – Compute Object ID and Optionally Creates Blob

  • What it does: Computes the object ID value for an object with an optional type.
  • Example:
  git hash-object path/to/file
  • Sample Output:
  SHA-1

9️⃣ git ls-files – Show Information about Files in the Index

  • What it does: Shows information about files in the index and the working directory.
  • Example:
  git ls-files
  • Sample Output:
  file1.txt
  file2.txtCode language: CSS (css)

🔟 git ls-tree – List the Contents of a Tree Object

  • What it does: Lists the contents of a tree object.
  • Example:
  git ls-tree HEAD
  • Sample Output:
  100644 blob SHA-1    file1.txtCode language: CSS (css)

1️⃣1️⃣ git merge-base – Find Common Ancestor

  • What it does: Finds a common ancestor between two commits.
  • Example:
  git merge-base master feature-branch
  • Sample Output:
  SHA-1

1️⃣2️⃣ git read-tree – Read Tree Information into Directory Index

  • What it does: Reads tree information into the directory index.
  • Example:
  git read-tree SHA-1
  • Sample Output:
  // No output if successfulCode language: JSON / JSON with Comments (json)

1️⃣3️⃣ git rev-list – Lists Commit Objects

  • What it does: Lists commit objects in reverse chronological order.
  • Example:
  git rev-list HEADCode language: PHP (php)
  • Sample Output:
  SHA-1
  SHA-1
  ...

1️⃣4️⃣ git rev-parse – Parsing Revisions

  • What it does: Parses revision (or other objects) identifiers.
  • Example:
  git rev-parse HEAD
  • Sample Output:
  SHA-1

1️⃣5️⃣ git show-ref – List References

  • What it does: Lists all references.
  • Example:
  git show-ref
  • Sample Output:
  SHA-1 refs/heads/master

1️⃣6️⃣ git symbolic-ref – Read, Modify and Delete Symbolic Refs

  • What it does: Manages symbolic references.
  • Example:
  git symbolic-ref HEAD refs/heads/main
  • Sample Output:
  // No output if successfulCode language: JSON / JSON with Comments (json)

1️⃣7️⃣ git update-index – Register Changes in the Index

  • What it does: Manages the index.
  • Example:
  git update-index --add new-file.txtCode language: JavaScript (javascript)
  • Sample Output:
  // No output if successfulCode language: JSON / JSON with Comments (json)

1️⃣8️⃣ git update-ref – Update Reference

  • What it does: Updates a reference with a new value.
  • Example:
  git update-ref refs/heads/main NEW_SHA-1
  • Sample Output:
  // No output if successfulCode language: JSON / JSON with Comments (json)

1️⃣9️⃣ git verify-pack – Validate Pack Files

  • What it does: Validates pack files.
  • Example:
  git verify-pack -v /path/to/pack
  • Sample Output:
  ok

2️⃣0️⃣ git write-tree – Create a Tree Object

  • What it does: Writes a tree object from the index.
  • Example:
  git write-tree
  • Sample Output:
  SHA-1

I hope you find this cheat sheet helpful! It includes what each plumbing command does, an example of how to use it, and sample output.

In the next article I will add some advanced concepts.

Leave a Reply

Scroll to Top

Discover more from DevOps AI/ML

Subscribe now to keep reading and get access to the full archive.

Continue reading