Topic 5 of 5 · 5 min read
Many committers in one checkout
Goal: Commit only your own changes in a working tree shared with other agents, handling an index lock and a rejected push without losing anyone's work.
When several agents and people share one working tree, commit only the paths you changed, never sweep with git add -A, and treat locks and rejected pushes as signals to wait and re-check, not to force. Separate worktrees are better, but sometimes a shared checkout is what you have.
Check yourself · 1 of 3
Several agents edit one working tree. How should you commit your change?
Why it matters
In a shared folder, git status shows everyone's edits. A broad git add . or git commit -a commits another agent's half-finished file under your name and message. That breaks the other task, and it is hard to untangle.
How to do it
- Know your files. Note which paths you changed. Check with
git status --shortandgit diff --statbefore staging. - Stage explicit paths and commit with the same paths:
git add src/export/csv.ts tests/csv.test.ts
git diff --cached --stat # confirm only your files
git commit -m "Add CSV export" -- src/export/csv.ts tests/csv.test.ts
- Handle a lock.
fatal: Unable to create '.git/index.lock': File existsmeans another git command is running, or one crashed. First check whether git is running:ps -ef | grep '[g]it '. If another process is truly active, wait a few seconds and retry. Remove the lock file only when you have confirmed no git process is using it. Never delete a lock while another command is writing. - Handle a rejected push.
! [rejected] main -> main (fetch first)means someone pushed since you last fetched. Fetch, then rebase your commit or merge, re-run tests, and push again. Do not use--force. - Leave strangers alone. Untracked files you did not create are someone else's. Do not delete them with
git clean, and do not add them.
Before and after
Before: an agent finishes, runs git add -A && git commit -m "done", and the commit contains a colleague's unfinished migration and a .env.local. The push is rejected, so it force-pushes.
After: the agent stages two named files, commits them, fetches, rebases onto the new main, re-runs its tests and pushes normally. The migration stays uncommitted in the folder, owned by its author.
What goes wrong
- Committing stray files. Logs, build output and secrets sneak in. Keep
.gitignorecurrent and check--cached. - Stale lock removal. Deleting
index.lockduring a live operation can corrupt the index. - Force-push after a rejection. It erases the other committer's work.
- Pulling with local edits. An agent that runs
git pullwhile others have uncommitted changes can hit conflicts in files it does not own. Usegit fetchfirst and look.
Examples
Rejected push, handled
git push origin main
# ! [rejected] main -> main (fetch first)
git fetch origin
git rebase origin/main
npm test -- tests/csv.test.ts
git push origin main
The agent re-tests after rebasing because the new base may change behaviour. No force flag appears.
Checking the lock
ls .git/index.lock
ps -o pid,etime,args -C git
If a git process has run for a few seconds, wait. If the lock is minutes old and no git process exists, the earlier command crashed and removing the file is reasonable.
Key points
- In a shared tree, stage and commit explicit paths, never git add -A or git commit -a.
- Check git diff --cached before every commit to confirm only your files are in it.
- An index.lock error means another git command is running or crashed; confirm that before removing the file.
- A rejected push means someone pushed first: fetch, rebase or merge, re-test and push again, never force.
- Files you did not create belong to someone else, so do not stage, clean or delete them.
Common mistakes
- Using git add -A or git commit -a in a folder other agents are editing.
- Deleting index.lock without checking for a running git process.
- Force-pushing to get past a rejected push.
- Running git clean or checkout . and wiping files someone else was working on.
Check yourself · 2 of 3
Your push is rejected with 'fetch first'. What should you do?
Check yourself · 3 of 3
Git reports that .git/index.lock exists. What is the safe first step?
Practice items, not counted towards your mastery.
Start here
- Pro Git book (opens in a new tab)BookFree
The free reference book for branching, rebasing, reset and recovery, written by Git's maintainers' community.
- git-worktree documentation (opens in a new tab)DocsFree
How to give each task its own checkout of one repository, and how to list, lock and remove them.
- git-bisect documentation (opens in a new tab)DocsFree
Exact syntax for bisect start, good, bad, run and reset, with exit-code rules for scripts.
More for this topic
- git-push documentation (opens in a new tab)DocsFree
Explains rejected pushes, --force-with-lease and why a plain force push is dangerous.