JMM’s notes on

Git

Because I’ve used Magit so much I’ve forgot the commands.

See also my notes on git-annex.

Remotes

Show remotes

git remote -v show

Add a remote

Say I wanted to add a remote on some server called host1 located at ~/some/path.git. I’d SSH into the host then run:

$ mkdir -p some/path.git
$ cd some/path.git
$ git init --bare

Then, back on the local computer:

$ git remote add host1remote host1:some/path.git
$ git push host1remote

I should also probably mention how to do this in Magit. Remote stuff is done using the M key there.

Corrupted index

See my btrfs page for how I restore from a corrupted index. A corrupted index means (I think) that the file at .git/index is messed up. For some reason I think I try restoring the entire .git directory, and now that I think about it I’m not sure why.

Use git fsck to check the status of Git repository.

Pushing to ░░░░░:repos/░░░░░░.git

Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.97 KiB | 1.97 MiB/s, done.
Total 7 (delta 4), reused 0 (delta 0), pack-reused 0
remote: error: object file ░░░░░░░░░░░░░░░░░░░░░░.git/objects/b3/37░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░e7d is empty        
remote: error: failed to read delta-pack base object b337░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░e7d        
error: remote unpack failed: unpack-objects abnormal exit
To ░░░░:repos/░░░░░░.git
 ! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to '░░░░:repos/░░░░░░.git'

Now getting error:

Pushing to ░░░░:repos/░░░░░░.git

Writing objects: 100% (12/12), 2.43 KiB | 1.21 MiB/s, done.
Total 12 (delta 8), reused 0 (delta 0), pack-reused 0
remote: fatal: unresolved deltas left after unpacking        
error: remote unpack failed: unpack-objects abnormal exit
To ░░░░:repos/░░░░░░.git
 ! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to '░░░░:repos/░░░░░░.git'

I can see that my HEAD and refs/head/master are messed up. The contents of refs/head/master is a SHA1 (or whatever) hash that git cat-file -p 7814fdb59661be8f851c9a7828da805e742b0a61 doesn’t work on.

What I ended up doing was something like git log to find commit hashes (and there’s probably a better way to just output hashes directly) until one hash existed in the remote. Then I did something like

echo 2e1467a6d2054289ac56a313247132a2b440f1fa > refs/heads/master

to set the master to something that existed. Then doing git push -v remote master worked.

Plumbing commands

Here’s how to output some object (which I’m currently using to see if certain objects exist on a corrupted remote):

git cat-file -p 7814fdb59661be8f851c9a7828da805e742b0a61

Show the contents of a tree:

git ls-tree 449de937d465549ed2fa0901776d6f72398b1759

Outputs objects? But not in a remote/bare repository.

git rev-list --objects -g --no-walk --all