Showing posts with label git. Show all posts
Showing posts with label git. Show all posts

Monday, July 19, 2010

Recover lost commits from a Git repository

This morning I managed to blast away a colleagues local commits. We were running into ``non-fast-forward`` errors when pushing and somehow I lost one of the local commits (which I'm still curious how I screwed that up). Anyway, I use the technique I wrote about almost 3 months ago (to the day!): "Recover a deleted branch from a Git repository"

The main difference was I was not after the dangling commit, but the commit just before for it (which would show after the dangling commit in ``git-log``).

So I use that command I borrowed from StackOverflow:

$> git fsck --full --no-reflogs | grep commit

Then, use ``git log --stat <commit-hash>`` on the identified hashes and look for the following commit that you're after.

I will 100% admit that this is in the realm of MacGyver - but I didn't want to be responsible for blowing away any of my co-workers code.

Tuesday, April 20, 2010

Recover a deleted branch from a Git repository

So I thought I would quickly note this since I managed to reach into a Git repo and pull out code that I thought I had foolishly deleted. I was in a rush during a release two weeks ago and in a fit to clean up the repo I was working in trashed a feature branch for code I was working on.

My colleague Evan said there was a way to grab the branch since it's still in the .git objects (since I had not done a ``git-gc``). But the method for that was left as an exercise for me, the reader.

I used the command suggested by Iamamac on Stack Overflow.

So here's what I did:

$> git fsck --full --no-reflogs | grep commit

This finds the "dangling commits" in the repository. Again, the commit for the HEAD of the branch is still there - but it's just not listed as a ref/head. This will get you a list like so:

dangling commit 0c91cb293..
dangling commit 1896a7b7f..
dangling commit ec9ac3cd..
dangling commit 5c6a8f1af..
dangling commit fd729386..

Then just use ``git-log`` w/ some extra verbosity to find the HEAD commit you're looking for:

$> git log --stat 5c6a8f1af

Which gives you output like:

commit 5c6a8f1af
Author: lenards
Date: Wed Mar 24 08:56:20 2010 -0700

Test new version of gwtupload

pom.xml | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

I hope that helps someone out...

(and thanks to Scott Chacon and everyone in the Git community for sharing tutorials, advice, and wisdom)

Sunday, August 24, 2008

Install Git/Gitosis on a server (Debian) - Part 2

So I ran into some more issues before getting my Git hosting (via Gitosis) all working.

The first hurdle was getting past an error thrown when attempting to do a `git clone` of the gitosis-admin repository. The error was being thrown by os.py (remember that gitosis-admin is python based). The error message looked like this:

Traceback (most recent call last):

File ”/usr/bin/gitosis-serve”, line 7, in ?
sys.exit( File ”/usr/lib/python2.4/site-packages/gitosis-0.2-py2.4.egg/gitosis/app.py”, line 24, in run return app.main()
File ”/usr/lib/python2.4/site-packages/gitosis-0.2-py2.4.egg/gitosis/app.py”, line 38, in main self.handle_args(parser, cfg, options, args)
File ”/usr/lib/python2.4/site-packages/gitosis-0.2-py2.4.egg/gitosis/serve.py”, line 205, in handle_args os.execvp(‘git-shell’, [‘git-shell’, ‘-c’, newcmd])
File ”/usr/lib/python2.4/os.py”, line 341, in execvp _execvpe(file, args) File ”/usr/lib/python2.4/os.py”, line 379, in _execvpe func(fullname, *argrest)
OSError: [Errno 2] No such file or directory
The root cause of this is that the executable `git-shell` is missing from your install directory (be /usr/bin or /usr/local/bin). I've done a install locally and all the 140-some executables were places in my install directory (/usr/bin).

Originally, I just copied `git-shell` into /usr/bin. That's hack-ish, what I ended up doing was creating a symlink to `git-shell` from where I have git (in my case, /opt/git-1.6.0, with a symlink to it named /opt/git).

So now we can do our clone of the repository:

`git clone git@YOUR_SERVER:gitosis-admin.git`

This appears in both tutorials I looked at [urban puddle] [scie.nti.st]

Now we want to create a new repository. That was my next hurdle; and you won't need to overcome it if you follow directions. Notably, the simple directions in either. Which is that you need to modify the gitosis.conf (which is Step 6 in the Urban Puddle tutorial and "Creating new repositories" in the scie.nti.st tutorial).

I attempted to simply do something like initialize a git repository, do some commits, add a remote (origin) to the server, and then push to that remote (origin).

If you have gone awry - like me - you'll run into the following error:

ERROR:gitosis.serve.main:Repository read access denied
fatal: The remote end hung up unexpectedly

If you add the following under [gitosis] in the gitosis.conf:

loglevel = DEBUG

You'll get some helpful debug output.
lenards@deedee:~/devel/cs453/project$ git push origin master:refs/heads/master
DEBUG:gitosis.serve.main:Got command "git-receive-pack 'cs453-project.git'"
DEBUG:gitosis.access.haveAccess:Access check for 'lenards@deedee' as 'writable' on 'cs453-project.git'...
DEBUG:gitosis.access.haveAccess:Stripping .git suffix from 'cs453-project.git', new value 'cs453-project'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'gitosis-admin'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'cs453-project'
DEBUG:gitosis.access.haveAccess:Access check for 'lenards@deedee' as 'writeable' on 'cs453-project.git'...
DEBUG:gitosis.access.haveAccess:Stripping .git suffix from 'cs453-project.git', new value 'cs453-project'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'gitosis-admin'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'cs453-project'
DEBUG:gitosis.access.haveAccess:Access check for 'lenards@deedee' as 'readonly' on 'cs453-project.git'...
DEBUG:gitosis.access.haveAccess:Stripping .git suffix from 'cs453-project.git', new value 'cs453-project'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'gitosis-admin'
DEBUG:gitosis.group.getMembership:found 'lenards@deedee' in 'cs453-project'
ERROR:gitosis.serve.main:Repository read access denied
fatal: The remote end hung up unexpectedly
I missed the step in the directions about adding a new group to the gitosis.conf, which is why when doing your `git push origin master:refs/heads/master` it complains about not have readonly access. There is *no* group to access, so you don't have writable or readonly access.

After doing Step 6 from Urban Puddle tutorial - I was able to push my new repository for my compiler project.

I hope this helps someone.

Andy