Essential Git Commands for AI Programming: worktree, diff, and .gitkeep
This guide walks through practical Git workflows—creating parallel worktrees, handling merge conflicts, stashing changes, inspecting diffs, navigating history with log and reflog, amending commits, using reset versus revert, cherry‑picking specific changes, bisecting bugs, and preserving empty directories with .gitkeep—tailored for AI coding tools such as Claude Code, Cursor, and Codex.
Worktree – Parallel AI Tasks
Scenario: run multiple unrelated AI tasks (e.g., bug fix and new feature) without them interfering.
git worktree add ../myproject-feature-a -b feature-a
git worktree add ../myproject-feature-b -b feature-b
cd ../myproject-feature-a && claude
cd ../myproject-feature-b && claude
git worktree list
cd ../myproject && git merge feature-a
git merge feature-b
git worktree remove ../myproject-feature-a
git branch -d feature-aConflict Handling
When a merge produces conflicts, start by checking the conflict status: git status Edit conflicted files manually, remove conflict markers (e.g., <<<<<< HEAD … ======= … >>>>>> feature-b), then stage the resolved files:
git add <em>path/to/conflicted-file</em>
git commitTo abort the merge entirely:
git merge --abortStash – Temporarily Save In‑Progress Changes
Scenario: you need to switch tasks or change direction without losing current modifications.
git stash
git stash list
git stash pop
git stash drop <em>stash@{0}</em>Diff – Review What AI Changed
git diff # unstaged changes
git diff --staged # staged but uncommitted changes
git diff main feature-a # compare two branchesLog / Reflog – History and “Undo”
git log --oneline --graph --all
git reflog
git reset --hard <em><reflog-hash></em>If a git reset --hard mistakenly discards work, git reflog can usually recover the lost state.
Amend – Modify the Last Commit
git add <em>path/to/forgotten-file</em>
git commit --amend --no-edit
git commit --amend -m "New commit message"Only amend commits that have not been pushed; otherwise a forced push is required.
Reset vs. Revert – Two Ways to Undo Changes
Reset keeps changes in the working tree:
git reset --soft HEAD~1 # changes stay staged
git reset --mixed HEAD~1 # changes stay in working directory (default)
git reset --hard HEAD~1 # discard changes completelyRevert creates a new commit that undoes a previous one without rewriting history: git revert <commit-hash> If the problematic commit has already been pushed, prefer git revert over git reset.
Cherry‑Pick – Apply a Single Commit
git log --oneline feature-a # locate commit hash
git cherry-pick <commit-hash>Bisect – Binary Search for Bug‑Introducing Commit
git bisect start
git bisect bad
git bisect good <good-hash>
# test each checkout, then:
git bisect good # or git bisect bad
git bisect resetCommit Message Conventions
feat:Add user login feature fix: Resolve worktree merge conflict docs: Update README refactor: Refactor payment module test: Add unit tests chore: Update dependency versions
.gitkeep – Preserve Empty Directories
mkdir logs
touch logs/.gitkeep
git add logs/.gitkeep
git commit -m "chore: keep empty logs folder"Quick‑Reference Cheat Sheet
Unsure of current state – git status See what AI changed – git diff Merge conflict – git status, edit files, git add, git commit Abort merge – git merge --abort Temporarily switch task – git stash Forgot file / wrong commit – git commit --amend Undo pushed commit – git revert <hash> Recover deleted commit – git reflog Apply a single change – git cherry-pick <hash> Locate bug‑introducing commit –
git bisect startOfficial Documentation
Git official site: https://git-scm.com
Full command reference: https://git-scm.com/docs
Pro Git (Chinese edition): https://git-scm.com/book/zh/v2
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
AI Engineer Programming
In the AI era, defining problems is often more important than solving them; here we explore AI's contradictions, boundaries, and possibilities.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
